Search in sources :

Example 21 with NotNullDef

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

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

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

the class ConstraintApiTest method cascaded.

@Test
public void cascaded() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    // tag::cascaded[]
    ConstraintMapping constraintMapping = configuration.createConstraintMapping();
    constraintMapping.type(Car.class).field("driver").constraint(new NotNullDef()).valid().convertGroup(Default.class).to(PersonDefault.class).field("partManufacturers").containerElementType(0).valid().containerElementType(1, 0).valid().type(Person.class).field("name").constraint(new NotNullDef().groups(PersonDefault.class));
// end::cascaded[]
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Default(jakarta.validation.groups.Default) Test(org.junit.Test)

Example 24 with NotNullDef

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

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

Aggregations

NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)47 Test (org.testng.annotations.Test)42 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)37 HibernateValidator (org.hibernate.validator.HibernateValidator)31 Validator (jakarta.validation.Validator)28 TestForIssue (org.hibernate.validator.testutil.TestForIssue)22 ConstraintViolation (jakarta.validation.ConstraintViolation)19 ConstraintViolationException (jakarta.validation.ConstraintViolationException)10 NotNull (jakarta.validation.constraints.NotNull)9 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 NotEmptyDef (org.hibernate.validator.cfg.defs.NotEmptyDef)4 Test (org.junit.Test)4 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)3 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 MinDef (org.hibernate.validator.cfg.defs.MinDef)2