Search in sources :

Example 96 with Validator

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

the class ConstraintMappingTest method testThatSpecificParameterCanBeSetAfterInvokingMethodFromBaseType.

@Test
public void testThatSpecificParameterCanBeSetAfterInvokingMethodFromBaseType() {
    mapping.type(Marathon.class).getter("name").constraint(new SizeDef().message("too short").min(3));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("NY");
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("too short"));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 97 with Validator

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

the class ConstraintMappingTest method testDefaultGroupSequenceProvider.

@Test
public void testDefaultGroupSequenceProvider() {
    mapping.type(Marathon.class).defaultGroupSequenceProviderClass(MarathonDefaultGroupSequenceProvider.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    marathon.setName("Stockholm Marathon");
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 98 with Validator

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

the class ConstraintMappingTest method testRangeDef.

@Test(description = "HV-355 (parameter names of RangeDef wrong)")
public void testRangeDef() {
    mapping.type(Runner.class).getter("age").constraint(new RangeDef().min(12).max(99));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<Runner>> violations = validator.validate(new Runner());
    assertThat(violations).containsOnlyViolations(violationOf(Range.class).withMessage("must be between 12 and 99"));
}
Also used : RangeDef(org.hibernate.validator.cfg.defs.RangeDef) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 99 with Validator

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

the class ConstraintMappingTest method testInheritedConstraint.

@Test
public void testInheritedConstraint() {
    mapping.type(Marathon.class).getter("name").constraint(new NotNullDef()).type(Tournament.class).getter("tournamentDate").constraint(new FutureDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("New York Marathon");
    Calendar cal = GregorianCalendar.getInstance();
    cal.set(Calendar.YEAR, -1);
    marathon.setTournamentDate(cal.getTime());
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Future.class).withMessage("must be a future date"));
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) FutureDef(org.hibernate.validator.cfg.defs.FutureDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 100 with Validator

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

the class ConstraintMappingTest method testCustomConstraintType.

@Test
public void testCustomConstraintType() {
    mapping.type(Marathon.class).constraint(new GenericConstraintDef<>(MarathonConstraint.class).param("minRunner", 100).message("Needs more runners"));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("Stockholm Marathon");
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(MarathonConstraint.class).withMessage("Needs more runners"));
    for (int i = 0; i < 100; i++) {
        marathon.addRunner(new Runner());
    }
    violations = validator.validate(marathon);
    assertNoViolations(violations);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Aggregations

Validator (jakarta.validation.Validator)413 Test (org.testng.annotations.Test)340 ConstraintViolation (jakarta.validation.ConstraintViolation)258 HibernateValidator (org.hibernate.validator.HibernateValidator)215 TestForIssue (org.hibernate.validator.testutil.TestForIssue)181 ConstraintValidator (jakarta.validation.ConstraintValidator)114 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)88 ValidatorFactory (jakarta.validation.ValidatorFactory)79 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)71 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)43 Test (org.junit.Test)38 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)33 ConstraintViolationException (jakarta.validation.ConstraintViolationException)32 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)28 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)27 NotNull (jakarta.validation.constraints.NotNull)26 Path (jakarta.validation.Path)19 Size (jakarta.validation.constraints.Size)18 CountValidationCallsValidator (org.hibernate.validator.testutil.CountValidationCallsValidator)17 List (java.util.List)13