Search in sources :

Example 11 with SizeDef

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

the class ConstraintMappingTest method testMultipleConstraintOfTheSameType.

@Test
public void testMultipleConstraintOfTheSameType() {
    mapping.type(Marathon.class).getter("name").constraint(new SizeDef().min(5)).constraint(new SizeDef().min(10));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("Foo");
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 10 and 2147483647"), violationOf(Size.class).withMessage("size must be between 5 and 2147483647"));
    marathon.setName("Foobar");
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 10 and 2147483647"));
    marathon.setName("Stockholm Marathon");
    violations = validator.validate(marathon);
    assertNoViolations(violations);
}
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)

Example 12 with SizeDef

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

the class ConstraintMappingTest method testDeprecatedPropertyMethodForFieldAndGetterProgrammaticConstraintDefinition.

@Test
@SuppressWarnings("deprecation")
public void testDeprecatedPropertyMethodForFieldAndGetterProgrammaticConstraintDefinition() {
    mapping.type(Marathon.class).property("name", ElementType.METHOD).constraint(new SizeDef().min(5)).constraint(new SizeDef().min(10)).property("runners", ElementType.FIELD).constraint(new SizeDef().max(10).min(1));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("Foo");
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 10 and 2147483647"), violationOf(Size.class).withMessage("size must be between 5 and 2147483647"), violationOf(Size.class).withMessage("size must be between 1 and 10"));
}
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)

Example 13 with SizeDef

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

the class ConstraintMappingTest method testThatSpecificParameterCanBeSetAfterAddingGenericConstraintDef.

@Test(description = "HV-404: Introducing ConstraintsForType#genericConstraint(Class) allows to set specific parameters on following specific constraints.")
public void testThatSpecificParameterCanBeSetAfterAddingGenericConstraintDef() {
    mapping.type(Marathon.class).constraint(new GenericConstraintDef<>(MarathonConstraint.class).param("minRunner", 1)).getter("name").constraint(new SizeDef().message("name too short").min(3));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("NY");
    marathon.addRunner(new Runner());
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("name too short"));
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) 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)

Example 14 with SizeDef

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

the class ConstraintMappingTest method testProgrammaticAndAnnotationFieldConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationFieldConstraintsAddUp() {
    mapping.type(User.class).field("firstName").constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<User>> violations = validator.validateProperty(new User("", ""), "firstName");
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10"), violationOf(Size.class).withMessage("size must be between 2 and 10"));
}
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)

Example 15 with SizeDef

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

the class ConstructorConstraintMappingTest method testMultipleParameterConstraintsAtSameParameter.

@Test
public void testMultipleParameterConstraintsAtSameParameter() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(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 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)

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