Search in sources :

Example 21 with SizeDef

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

the class XmlMappingTest method testConstraintsFromXmlAndProgrammaticApiAddUp.

@Test
@TestForIssue(jiraKey = "HV-480")
public void testConstraintsFromXmlAndProgrammaticApiAddUp() {
    final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
    // given
    final ConstraintMapping programmaticMapping = configuration.createConstraintMapping();
    programmaticMapping.type(Customer.class).field("firstName").constraint(new SizeDef().min(2).max(10));
    final InputStream xmlMapping = XmlMappingTest.class.getResourceAsStream("hv-480-mapping.xml");
    configuration.addMapping(programmaticMapping);
    configuration.addMapping(xmlMapping);
    final Customer customer = new Customer();
    customer.setFirstName("");
    // when
    final Set<ConstraintViolation<Customer>> violations = configuration.buildValidatorFactory().getValidator().validate(customer);
    // then
    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 : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) InputStream(java.io.InputStream) ConstraintViolation(jakarta.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 22 with SizeDef

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

the class ConstraintApiTest method constraintMapping.

@Test
public void constraintMapping() {
    // tag::constraintMapping[]
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    ConstraintMapping constraintMapping = configuration.createConstraintMapping();
    constraintMapping.type(Car.class).field("manufacturer").constraint(new NotNullDef()).field("licensePlate").ignoreAnnotations(true).constraint(new NotNullDef()).constraint(new SizeDef().min(2).max(14)).type(RentalCar.class).getter("rentalStation").constraint(new NotNullDef());
    Validator validator = configuration.addMapping(constraintMapping).buildValidatorFactory().getValidator();
// end::constraintMapping[]
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) RegexpURLValidator(org.hibernate.validator.constraintvalidators.RegexpURLValidator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Example 23 with SizeDef

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

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

the class ConstraintApiTest method nestedContainerElementConstraint.

@Test
public void nestedContainerElementConstraint() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    // tag::nestedContainerElementConstraint[]
    ConstraintMapping constraintMapping = configuration.createConstraintMapping();
    constraintMapping.type(Car.class).field("manufacturer").constraint(new NotNullDef()).field("licensePlate").ignoreAnnotations(true).constraint(new NotNullDef()).constraint(new SizeDef().min(2).max(14)).field("partManufacturers").containerElementType(0).constraint(new NotNullDef()).containerElementType(1, 0).constraint(new NotNullDef()).type(RentalCar.class).getter("rentalStation").constraint(new NotNullDef());
// end::nestedContainerElementConstraint[]
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.junit.Test)

Example 25 with SizeDef

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

the class MethodConstraintMappingTest method testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod.

@Test
public void testOverridingMethodMayDefineSameConstraintsAsOverriddenMethod() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10)).type(GreetingServiceImpl.class).method("greet", String.class).parameter(0).constraint(new SizeDef().min(5).max(10));
    config.addMapping(mapping);
    GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
    try {
        service.greet("Hi");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 5 and 10").withPropertyPath(pathWith().method("greet").parameter("string", 0)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) 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