Search in sources :

Example 1 with MinDef

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

the class ProgrammaticContainerElementConstraintsForGetterTest method canDeclareContainerElementConstraintsForGetterProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForGetterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).getter("model").containerElementType().constraint(new SizeDef().max(5)).getter("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 2 with MinDef

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

the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForParameterProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForParameterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test1", Optional.class, Map.class).parameter(0).containerElementType().constraint(new SizeDef().max(5)).parameter(1).containerElementType(0).constraint(new SizeDef().min(3).max(10)).containerElementType(1).constraint(new MinDef().value(1));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        HashMap<String, Integer> fishCountByType = new HashMap<>();
        fishCountByType.put("A", -1);
        fishCountByType.put("BB", -2);
        fishTank.test1(Optional.of("Too long"), fishCountByType);
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).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) HashMap(java.util.HashMap) Size(jakarta.validation.constraints.Size) MinDef(org.hibernate.validator.cfg.defs.MinDef) Min(jakarta.validation.constraints.Min) ConstraintViolationException(jakarta.validation.ConstraintViolationException) 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 3 with MinDef

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

the class MyConstraintMappingContributor method createConstraintMappings.

@Override
public void createConstraintMappings(ConstraintMappingBuilder builder) {
    builder.addConstraintMapping().type(Marathon.class).getter("name").constraint(new NotNullDef()).field("numberOfHelpers").constraint(new MinDef().value(1));
    builder.addConstraintMapping().type(Runner.class).field("paidEntryFee").constraint(new AssertTrueDef());
}
Also used : MinDef(org.hibernate.validator.cfg.defs.MinDef) AssertTrueDef(org.hibernate.validator.cfg.defs.AssertTrueDef) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef)

Example 4 with MinDef

use of org.hibernate.validator.cfg.defs.MinDef 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 5 with MinDef

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

the class ConstraintMappingTest method testDefConstraintFollowedByGenericConstraint.

@Test
public void testDefConstraintFollowedByGenericConstraint() {
    mapping.type(Marathon.class).field("numberOfHelpers").constraint(new MinDef().value(1)).constraint(new GenericConstraintDef<>(Min.class).param("value", 2L));
    BeanConfiguration<Marathon> beanConfiguration = getBeanConfiguration(Marathon.class);
    assertNotNull(beanConfiguration);
    assertEquals(getConstrainedField(beanConfiguration, "numberOfHelpers").getConstraints().size(), 2);
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) MinDef(org.hibernate.validator.cfg.defs.MinDef) Test(org.testng.annotations.Test)

Aggregations

MinDef (org.hibernate.validator.cfg.defs.MinDef)7 Test (org.testng.annotations.Test)6 Validator (jakarta.validation.Validator)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)4 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)4 TestForIssue (org.hibernate.validator.testutil.TestForIssue)4 ConstraintViolation (jakarta.validation.ConstraintViolation)2 ConstraintViolationException (jakarta.validation.ConstraintViolationException)2 Min (jakarta.validation.constraints.Min)2 Size (jakarta.validation.constraints.Size)2 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)2 HashMap (java.util.HashMap)1 GenericConstraintDef (org.hibernate.validator.cfg.GenericConstraintDef)1 AssertTrueDef (org.hibernate.validator.cfg.defs.AssertTrueDef)1