Search in sources :

Example 1 with AssertTrueDef

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

Example 2 with AssertTrueDef

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

the class ConstraintMappingTest method testValidWithSeveralGroupConversions.

@Test
public void testValidWithSeveralGroupConversions() {
    mapping.type(Marathon.class).getter("runners").valid().convertGroup(Default.class).to(Foo.class).convertGroup(Bar.class).to(Default.class).type(Runner.class).field("paidEntryFee").constraint(new AssertTrueDef().groups(Foo.class)).constraint(new AssertTrueDef().message("really, it must be true"));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("New York Marathon");
    marathon.addRunner(new Runner());
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon, Default.class, Bar.class);
    assertThat(violations).containsOnlyViolations(violationOf(AssertTrue.class).withMessage("must be true"), violationOf(AssertTrue.class).withMessage("really, it must be true"));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) AssertTrueDef(org.hibernate.validator.cfg.defs.AssertTrueDef) Default(jakarta.validation.groups.Default) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 3 with AssertTrueDef

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

the class ConstraintMappingTest method testValid.

@Test
public void testValid() {
    mapping.type(Marathon.class).getter("runners").valid().type(Runner.class).field("paidEntryFee").constraint(new AssertTrueDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("New York Marathon");
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertNoViolations(violations);
    marathon.addRunner(new Runner());
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(AssertTrue.class).withMessage("must be true"));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) AssertTrueDef(org.hibernate.validator.cfg.defs.AssertTrueDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 4 with AssertTrueDef

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

the class ConstraintMappingTest method testValidWithGroupConversion.

@Test
public void testValidWithGroupConversion() {
    mapping.type(Marathon.class).getter("runners").valid().convertGroup(Default.class).to(Foo.class).type(Runner.class).field("paidEntryFee").constraint(new AssertTrueDef().groups(Foo.class));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    marathon.setName("New York Marathon");
    marathon.addRunner(new Runner());
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(AssertTrue.class).withMessage("must be true"));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) AssertTrueDef(org.hibernate.validator.cfg.defs.AssertTrueDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Aggregations

AssertTrueDef (org.hibernate.validator.cfg.defs.AssertTrueDef)4 ConstraintViolation (jakarta.validation.ConstraintViolation)3 Validator (jakarta.validation.Validator)3 HibernateValidator (org.hibernate.validator.HibernateValidator)3 Test (org.testng.annotations.Test)3 Default (jakarta.validation.groups.Default)1 MinDef (org.hibernate.validator.cfg.defs.MinDef)1 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)1