Search in sources :

Example 1 with LengthDef

use of org.hibernate.validator.cfg.defs.LengthDef in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1614")
public void canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 0).constraint(new LengthDef().min(10).max(20)).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"), violationOf(Length.class).withMessage("length must be between 10 and 20"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) LengthDef(org.hibernate.validator.cfg.defs.LengthDef) 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

ConstraintViolation (jakarta.validation.ConstraintViolation)1 Validator (jakarta.validation.Validator)1 HibernateValidator (org.hibernate.validator.HibernateValidator)1 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)1 LengthDef (org.hibernate.validator.cfg.defs.LengthDef)1 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)1 TestForIssue (org.hibernate.validator.testutil.TestForIssue)1 Test (org.testng.annotations.Test)1