Search in sources :

Example 26 with SizeDef

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

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareContainerElementConstraintsForArrayTypedFieldProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForArrayTypedFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("fishNames").containerElementType().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 27 with SizeDef

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

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareContainerElementConstraintsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("model").containerElementType().constraint(new SizeDef().max(5)).field("fishCountByType").containerElementType(0).constraint(new SizeDef().min(3).max(10)).containerElementType(1).constraint(new MinDef().value(1));
    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"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Min.class).withMessage("must be greater than or equal to 1"), violationOf(Min.class).withMessage("must be greater than or equal to 1"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) MinDef(org.hibernate.validator.cfg.defs.MinDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 28 with SizeDef

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

the class ProgrammaticContainerElementConstraintsForGetterTest method canDeclareContainerElementConstraintsForArrayTypedGetterProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForArrayTypedGetterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).getter("fishNames").containerElementType().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 29 with SizeDef

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

the class ProgrammaticContainerElementConstraintsForGetterTest method canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeGetterProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeGetterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).getter("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 30 with SizeDef

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

the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForListContainingArrayTypeParameterProgrammatically.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForListContainingArrayTypeParameterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test6", List.class).parameter(0).containerElementType(0, 0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        List<String[]> fishNamesByMonth = new ArrayList<>();
        fishNamesByMonth.add(new String[] { "Too Long" });
        fishTank.test6(fishNamesByMonth);
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Size(jakarta.validation.constraints.Size) ArrayList(java.util.ArrayList) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Aggregations

SizeDef (org.hibernate.validator.cfg.defs.SizeDef)40 Test (org.testng.annotations.Test)37 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)34 HibernateValidator (org.hibernate.validator.HibernateValidator)29 Validator (jakarta.validation.Validator)27 TestForIssue (org.hibernate.validator.testutil.TestForIssue)21 ConstraintViolation (jakarta.validation.ConstraintViolation)18 ConstraintViolationException (jakarta.validation.ConstraintViolationException)15 Size (jakarta.validation.constraints.Size)11 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 MinDef (org.hibernate.validator.cfg.defs.MinDef)4 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)3 Test (org.junit.Test)3 Min (jakarta.validation.constraints.Min)2 GenericConstraintDef (org.hibernate.validator.cfg.GenericConstraintDef)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1