-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hibernate Validator - Allow customization of the ValidatorFactory
Signed-off-by: Theodor Mihalache <[email protected]>
- Loading branch information
Showing
10 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...us/hibernate/validator/test/validatorfactory/HibernateValidatorFactoryCustomizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.inject.Inject; | ||
import javax.validation.ValidatorFactory; | ||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.Min; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class HibernateValidatorFactoryCustomizerTest { | ||
|
||
@Inject | ||
ValidatorFactory validatorFactory; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
.create(JavaArchive.class) | ||
.addClasses(MyMultipleHibernateValidatorFactoryCustomizer.class, MyEmailValidator.class, | ||
MyNumValidator.class)); | ||
|
||
@Test | ||
public void testOverrideConstraintValidatorConstraint() { | ||
assertThat(validatorFactory.getValidator().validate(new TestBean())).hasSize(2); | ||
} | ||
|
||
static class TestBean { | ||
public String email; | ||
|
||
@Min(-1) | ||
public int num; | ||
|
||
public TestBean() { | ||
this.email = "[email protected]"; | ||
this.num = -1; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...us/hibernate/validator/test/validatorfactory/MultipleHibernateFactoryCustomizersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.inject.Inject; | ||
import javax.validation.ValidatorFactory; | ||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.Min; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class MultipleHibernateFactoryCustomizersTest { | ||
|
||
@Inject | ||
ValidatorFactory validatorFactory; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
.create(JavaArchive.class) | ||
.addClasses(MyEmailHibernateValidatorFactoryCustomizer.class, MyNumberHibernateValidatorFactoryCustomizer.class, | ||
MyEmailValidator.class, | ||
MyNumValidator.class)); | ||
|
||
@Test | ||
public void testOverrideConstraintValidatorConstraint() { | ||
assertThat(validatorFactory.getValidator().validate(new TestBean())).hasSize(2); | ||
} | ||
|
||
static class TestBean { | ||
public String email; | ||
|
||
@Min(-1) | ||
public int num; | ||
|
||
public TestBean() { | ||
this.email = "[email protected]"; | ||
this.num = -1; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...hibernate/validator/test/validatorfactory/MyEmailHibernateValidatorFactoryCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.validation.constraints.Email; | ||
|
||
import org.hibernate.validator.BaseHibernateValidatorConfiguration; | ||
import org.hibernate.validator.cfg.ConstraintMapping; | ||
|
||
import io.quarkus.hibernate.validator.HibernateValidatorFactoryCustomizer; | ||
|
||
@ApplicationScoped | ||
public class MyEmailHibernateValidatorFactoryCustomizer implements HibernateValidatorFactoryCustomizer { | ||
|
||
@Override | ||
public <T extends BaseHibernateValidatorConfiguration<T>> void customize(T configuration) { | ||
ConstraintMapping constraintMapping = configuration.createConstraintMapping(); | ||
|
||
constraintMapping | ||
.constraintDefinition(Email.class) | ||
.includeExistingValidators(false) | ||
.validatedBy(MyEmailValidator.class); | ||
|
||
configuration.addMapping(constraintMapping); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../src/test/java/io/quarkus/hibernate/validator/test/validatorfactory/MyEmailValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import javax.validation.constraints.Email; | ||
|
||
public class MyEmailValidator implements ConstraintValidator<Email, CharSequence> { | ||
@Override | ||
public boolean isValid(CharSequence value, ConstraintValidatorContext context) { | ||
return "[email protected]".contentEquals(value); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ernate/validator/test/validatorfactory/MyMultipleHibernateValidatorFactoryCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.Min; | ||
|
||
import org.hibernate.validator.BaseHibernateValidatorConfiguration; | ||
import org.hibernate.validator.cfg.ConstraintMapping; | ||
|
||
import io.quarkus.hibernate.validator.HibernateValidatorFactoryCustomizer; | ||
|
||
@ApplicationScoped | ||
public class MyMultipleHibernateValidatorFactoryCustomizer implements HibernateValidatorFactoryCustomizer { | ||
|
||
@Override | ||
public <T extends BaseHibernateValidatorConfiguration<T>> void customize(T configuration) { | ||
ConstraintMapping constraintMapping = configuration.createConstraintMapping(); | ||
|
||
constraintMapping | ||
.constraintDefinition(Email.class) | ||
.includeExistingValidators(false) | ||
.validatedBy(MyEmailValidator.class); | ||
|
||
configuration.addMapping(constraintMapping); | ||
|
||
constraintMapping = configuration.createConstraintMapping(); | ||
|
||
constraintMapping | ||
.constraintDefinition(Min.class) | ||
.includeExistingValidators(false) | ||
.validatedBy(MyNumValidator.class); | ||
|
||
configuration.addMapping(constraintMapping); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...nt/src/test/java/io/quarkus/hibernate/validator/test/validatorfactory/MyNumValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import javax.validation.constraints.Min; | ||
|
||
public class MyNumValidator implements ConstraintValidator<Min, Integer> { | ||
@Override | ||
public boolean isValid(Integer value, ConstraintValidatorContext context) { | ||
return value >= 0; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ibernate/validator/test/validatorfactory/MyNumberHibernateValidatorFactoryCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.hibernate.validator.test.validatorfactory; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.validation.constraints.Min; | ||
|
||
import org.hibernate.validator.BaseHibernateValidatorConfiguration; | ||
import org.hibernate.validator.cfg.ConstraintMapping; | ||
|
||
import io.quarkus.hibernate.validator.HibernateValidatorFactoryCustomizer; | ||
|
||
@ApplicationScoped | ||
public class MyNumberHibernateValidatorFactoryCustomizer implements HibernateValidatorFactoryCustomizer { | ||
|
||
@Override | ||
public <T extends BaseHibernateValidatorConfiguration<T>> void customize(T configuration) { | ||
ConstraintMapping constraintMapping = configuration.createConstraintMapping(); | ||
|
||
constraintMapping | ||
.constraintDefinition(Min.class) | ||
.includeExistingValidators(false) | ||
.validatedBy(MyNumValidator.class); | ||
|
||
configuration.addMapping(constraintMapping); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ime/src/main/java/io/quarkus/hibernate/validator/HibernateValidatorFactoryCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.hibernate.validator; | ||
|
||
import org.hibernate.validator.BaseHibernateValidatorConfiguration; | ||
|
||
/** | ||
* Meant to be implemented by a CDI bean that provides arbitrary customization for the default | ||
* {@link javax.validation.ValidatorFactory}. | ||
* <p> | ||
* All implementations that are registered as CDI beans are taken into account when producing the default | ||
* {@link javax.validation.ValidatorFactory}. | ||
* <p> | ||
* Customizers are applied in the order of {@link javax.annotation.Priority}. | ||
*/ | ||
public interface HibernateValidatorFactoryCustomizer { | ||
|
||
<T extends BaseHibernateValidatorConfiguration<T>> void customize(T configuration); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters