Search in sources :

Example 6 with SizeDef

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

the class ProgrammaticContainerElementConstraintsForParameterTest method configuringConstraintsOnGenericTypeArgumentOfListThrowsException.

@Test(expectedExceptions = UnexpectedTypeException.class, expectedExceptionsMessageRegExp = "HV000030:.*")
@TestForIssue(jiraKey = "HV-1279")
public void configuringConstraintsOnGenericTypeArgumentOfListThrowsException() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test8", List.class).parameter(0).containerElementType(0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    fishTank.test8(Arrays.asList("Too long"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) 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)

Example 7 with SizeDef

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

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

the class ConstructorConstraintMappingTest method testMultipleParameterConstraintsAtDifferentParameters.

@Test
public void testMultipleParameterConstraintsAtDifferentParameters() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class, String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).parameter(1).constraint(new SizeDef().min(1).max(10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
    Object[] parameterValues = new Object[] { "", "" };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("anotherMessage", 1)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 9 with SizeDef

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

the class ConstructorConstraintMappingTest method testProgrammaticAndAnnotationParameterConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationParameterConstraintsAddUp() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(CharSequence.class).parameter(0).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(CharSequence.class);
    Object[] parameterValues = new Object[] { "" };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 10 with SizeDef

use of org.hibernate.validator.cfg.defs.SizeDef 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)

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