Search in sources :

Example 21 with ValidatorFactory

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

the class RelaxedMethodParameterConstraintsTest method allowParameterConstraintsAddedInSubType.

/**
 * The converse of disallowParameterConstraintsAddedInSubType,
 * relaxes constraint.
 */
@Test
public void allowParameterConstraintsAddedInSubType() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    configuration.allowOverridingMethodAlterParameterConstraint(true);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Set<? extends ConstraintViolation<?>> violations = validator.forExecutables().validateParameters(new RealizationWithMethodParameterConstraint(), RealizationWithMethodParameterConstraint.class.getDeclaredMethods()[0], new Object[] { "foo" });
    ConstraintViolationAssert.assertNoViolations(violations);
    configuration.allowOverridingMethodAlterParameterConstraint(false);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(jakarta.validation.ValidatorFactory) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 22 with ValidatorFactory

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

the class IgnoreAnnotationConfiguredConstructorValidationTest method setUp.

@BeforeTest
public void setUp() {
    final Configuration<?> configuration = ValidatorUtil.getConfiguration();
    configuration.addMapping(IgnoreAnnotationConfiguredConstructorValidationTest.class.getResourceAsStream("constructor-validation-ignore-annotations.xml"));
    final ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
    this.validator = validatorFactory.getValidator();
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) BeforeTest(org.testng.annotations.BeforeTest)

Example 23 with ValidatorFactory

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

the class PropertyNodeNameProviderTest method hibernateValidatorCanUseCustomPropertyNodeNameProvider.

@Test
public void hibernateValidatorCanUseCustomPropertyNodeNameProvider() {
    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure().propertyNodeNameProvider(new AnnotationPropertyNodeNameProvider(PropertyName.class)).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.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 24 with ValidatorFactory

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

the class PropertyNodeNameProviderTest method defaultProviderUsesDefaultPropertyNodeNameProvider.

@Test
public void defaultProviderUsesDefaultPropertyNodeNameProvider() {
    ValidatorFactory validatorFactory = Validation.byDefaultProvider().configure().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 : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 25 with ValidatorFactory

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

the class PropertyNodeNameProviderTest method hibernateValidatorUsesDefaultPropertyNodeProvider.

@Test
public void hibernateValidatorUsesDefaultPropertyNodeProvider() {
    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure().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 : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) 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