Search in sources :

Example 16 with Validator

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

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeFieldProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("fishNamesByMonthAsArray").containerElementType(0, 0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) 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) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 17 with Validator

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

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareNestedContainerElementConstraintsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareNestedContainerElementConstraintsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("fishOfTheMonth").containerElementType(1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) 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) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 18 with Validator

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

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareDeeplyNestedContainerElementConstraintsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) 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) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 19 with Validator

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

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareContainerElementConstraintsForListContainingArrayTypeFieldProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForListContainingArrayTypeFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("fishNamesByMonth").containerElementType(0, 0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) 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) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 20 with Validator

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

the class ProgrammaticContainerElementConstraintsForGetterTest method canDeclareContainerElementCascadesForGetterProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementCascadesForGetterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).getter("boss").containerElementType().valid().type(Fish.class).getter("name").constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) 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) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

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