Search in sources :

Example 1 with GenericConstraintDef

use of org.hibernate.validator.cfg.GenericConstraintDef 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 2 with GenericConstraintDef

use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.

the class ConstraintApiTest method executableConfiguration.

@Test
public void executableConfiguration() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    // tag::executableConfiguration[]
    ConstraintMapping constraintMapping = configuration.createConstraintMapping();
    constraintMapping.type(Car.class).constructor(String.class).parameter(0).constraint(new SizeDef().min(3).max(50)).returnValue().valid().method("drive", int.class).parameter(0).constraint(new MaxDef().value(75)).method("load", List.class, List.class).crossParameter().constraint(new GenericConstraintDef<>(LuggageCountMatchesPassengerCount.class).param("piecesOfLuggagePerPassenger", 2)).method("getDriver").returnValue().constraint(new NotNullDef()).valid();
// end::executableConfiguration[]
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) HibernateValidator(org.hibernate.validator.HibernateValidator) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) MaxDef(org.hibernate.validator.cfg.defs.MaxDef) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.junit.Test)

Example 3 with GenericConstraintDef

use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.

the class ConstraintApiTest method genericConstraintDef.

@Test
public void genericConstraintDef() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    // tag::genericConstraintDef[]
    ConstraintMapping constraintMapping = configuration.createConstraintMapping();
    constraintMapping.type(Car.class).field("licensePlate").constraint(new GenericConstraintDef<>(CheckCase.class).param("value", CaseMode.UPPER));
// end::genericConstraintDef[]
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) HibernateValidator(org.hibernate.validator.HibernateValidator) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Test(org.junit.Test)

Example 4 with GenericConstraintDef

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

Example 5 with GenericConstraintDef

use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method testProgrammaticAndAnnotationReturnValueConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationReturnValueConstraintsAddUp() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(CharSequence.class).returnValue().constraint(new GenericConstraintDef<>(ValidGreetingService.class).message("invalid 2"));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(CharSequence.class);
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorReturnValue(constructor, new GreetingService(""));
    assertThat(violations).containsOnlyViolations(violationOf(ValidGreetingService.class).withMessage("invalid 1").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()), violationOf(ValidGreetingService.class).withMessage("invalid 2").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()));
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test)

Aggregations

GenericConstraintDef (org.hibernate.validator.cfg.GenericConstraintDef)7 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)5 Test (org.testng.annotations.Test)5 ConstraintViolation (jakarta.validation.ConstraintViolation)4 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)3 HibernateValidator (org.hibernate.validator.HibernateValidator)3 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)2 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)2 Test (org.junit.Test)2 Validator (jakarta.validation.Validator)1 MaxDef (org.hibernate.validator.cfg.defs.MaxDef)1 MinDef (org.hibernate.validator.cfg.defs.MinDef)1 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)1