Search in sources :

Example 26 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-validator by hibernate.

the class PropertyNodeNameProviderTest method hibernateValidatorFallsBackToDefaultPropertyNodeNameProvider.

@Test
public void hibernateValidatorFallsBackToDefaultPropertyNodeNameProvider() {
    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure().propertyNodeNameProvider(null).buildValidatorFactory();
    Validator val = validatorFactory.getValidator();
    Car testInstance = new Car(INVALID_BRAND_NAME);
    Set<ConstraintViolation<Car>> violations = val.validate(testInstance);
    ConstraintViolation<Car> violation = violations.iterator().next();
    assertEquals(violation.getPropertyPath().toString(), "brand.name");
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 27 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-validator by hibernate.

the class JacksonAnnotationPropertyNodeNameProviderTest method setUp.

@BeforeMethod
public void setUp() {
    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure().propertyNodeNameProvider(new JacksonAnnotationPropertyNodeNameProvider()).buildValidatorFactory();
    validator = validatorFactory.getValidator();
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(jakarta.validation.ValidatorFactory) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 28 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-validator by hibernate.

the class LocaleResolverTest method testLanguageRangeSupport.

@Test
public void testLanguageRangeSupport() throws NoSuchMethodException, SecurityException {
    ValidatorFactory validatorFactory = getValidatorFactoryWithInitializedLocales(Locale.FRANCE, new Locale("es", "ES"));
    MessageInterpolator messageInterpolator = validatorFactory.getMessageInterpolator();
    StaticFieldLocaleResolver.acceptLanguage = "fr-FR,fr;q=0.9";
    assertThat(messageInterpolator.interpolate("{jakarta.validation.constraints.AssertFalse.message}", new TestContext())).isEqualTo("doit avoir la valeur faux");
}
Also used : Locale(java.util.Locale) ValidatorFactory(jakarta.validation.ValidatorFactory) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Example 29 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-validator by hibernate.

the class PredefinedScopeConstraintDefinitionContributorTest method setUp.

@BeforeMethod
public void setUp() {
    validator = getValidator();
    Set<Class<?>> beanMetaDataToInitialize = new HashSet<>();
    beanMetaDataToInitialize.add(Foo.class);
    beanMetaDataToInitialize.add(Quz.class);
    ValidatorFactory validatorFactory = Validation.byProvider(PredefinedScopeHibernateValidator.class).configure().initializeBeanMetaData(beanMetaDataToInitialize).buildValidatorFactory();
    validator = validatorFactory.getValidator();
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) HashSet(java.util.HashSet) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 30 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-validator by hibernate.

the class PredefinedScopeValidatorFactoryTest method testBeanMetaDataClassNormalizerNoNormalizer.

@Test
public void testBeanMetaDataClassNormalizerNoNormalizer() {
    // In this case, as we haven't registered any metadata for the hierarchy, even if we have constraints,
    // we won't have any violations.
    Set<ConstraintViolation<Bean>> violations = getValidator().validate(new BeanProxy());
    assertNoViolations(violations);
    // Now let's register the metadata for Bean and see how it goes
    Set<Class<?>> beanMetaDataToInitialize = new HashSet<>();
    beanMetaDataToInitialize.add(Bean.class);
    ValidatorFactory validatorFactory = Validation.byProvider(PredefinedScopeHibernateValidator.class).configure().initializeBeanMetaData(beanMetaDataToInitialize).locales(Collections.singleton(Locale.getDefault())).buildValidatorFactory();
    // As we don't have any metadata for BeanProxy, we consider it is not constrained at all.
    violations = validatorFactory.getValidator().validate(new BeanProxy());
    assertNoViolations(violations);
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

ValidatorFactory (jakarta.validation.ValidatorFactory)161 Validator (jakarta.validation.Validator)79 Test (org.testng.annotations.Test)76 HibernateValidator (org.hibernate.validator.HibernateValidator)74 ConstraintViolation (jakarta.validation.ConstraintViolation)41 TestForIssue (org.hibernate.validator.testutil.TestForIssue)30 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)24 ConstraintValidator (jakarta.validation.ConstraintValidator)23 BeforeClass (org.junit.BeforeClass)23 Test (org.junit.Test)17 MessageInterpolator (jakarta.validation.MessageInterpolator)14 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)14 NotNull (jakarta.validation.constraints.NotNull)13 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)9 Locale (java.util.Locale)7 BeforeTest (org.testng.annotations.BeforeTest)7 ValidationException (jakarta.validation.ValidationException)6 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)6 BeforeAll (org.junit.jupiter.api.BeforeAll)6 BeforeMethod (org.testng.annotations.BeforeMethod)6