Search in sources :

Example 11 with Validator

use of jakarta.validation.Validator in project org.openntf.xsp.jakartaee by OpenNTF.

the class TestValidationBasics method testExampleBean.

@Test
public void testExampleBean() {
    ExampleBean bean = new ExampleBean();
    Validator validator = XPagesValidationUtil.constructGenericValidator();
    {
        Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
        assertEquals("Bean should have two violations", 2, violations.size());
    }
    bean.setFoo("");
    {
        Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
        assertEquals("Bean should have two violations", 2, violations.size());
    }
    bean.setFoo("hey");
    {
        Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
        assertEquals("Bean should have one violation", 1, violations.size());
    }
    bean.setBar("h");
    {
        Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
        assertEquals("Bean should have one violation", 1, violations.size());
    }
    bean.setBar("h12345678910");
    {
        Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
        assertEquals("Bean should have one violation", 1, violations.size());
    }
    bean.setBar("hey there");
    {
        Set<ConstraintViolation<ExampleBean>> violations = XPagesValidationUtil.validateBean(bean, validator);
        assertEquals("Bean should have no violations", 0, violations.size());
    }
}
Also used : Set(java.util.Set) Validator(jakarta.validation.Validator) Test(org.junit.Test)

Example 12 with Validator

use of jakarta.validation.Validator in project glassfish-hk2 by eclipse-ee4j.

the class XmlRootHandleImpl method startValidating.

@Override
public void startValidating() {
    if (changeControl == null)
        throw new IllegalStateException();
    Validator validator = changeControl.findOrCreateValidator();
    if (root == null)
        return;
    Set<ConstraintViolation<Object>> violations = validator.<Object>validate(root);
    if (violations == null || violations.isEmpty())
        return;
    throw new ConstraintViolationException(violations);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator)

Example 13 with Validator

use of jakarta.validation.Validator in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSimpleValidationWithCustomProvider.

@Test
public void testSimpleValidationWithCustomProvider() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setProviderClass(HibernateValidator.class);
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
    assertThat(result.size()).isEqualTo(2);
    for (ConstraintViolation<ValidPerson> cv : result) {
        String path = cv.getPropertyPath().toString();
        assertThat(path).matches(actual -> "name".equals(actual) || "address.street".equals(actual));
        assertThat(cv.getConstraintDescriptor().getAnnotation()).isInstanceOf(NotNull.class);
    }
    Validator nativeValidator = validator.unwrap(Validator.class);
    assertThat(nativeValidator.getClass().getName().startsWith("org.hibernate")).isTrue();
    assertThat(validator.unwrap(ValidatorFactory.class)).isInstanceOf(HibernateValidatorFactory.class);
    assertThat(validator.unwrap(HibernateValidatorFactory.class)).isInstanceOf(HibernateValidatorFactory.class);
    validator.destroy();
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.jupiter.api.Test)

Example 14 with Validator

use of jakarta.validation.Validator in project today-framework by TAKETODAY.

the class ValidatorAdapterTests method testUnwrap.

@Test
public void testUnwrap() {
    Validator nativeValidator = validatorAdapter.unwrap(Validator.class);
    assertThat(nativeValidator).isSameAs(this.nativeValidator);
}
Also used : Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) Test(org.junit.jupiter.api.Test)

Example 15 with Validator

use of jakarta.validation.Validator in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForFieldTest method canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1614")
public void canDeclareDeeplyNestedContainerElementConstraintsOnMultipleDifferentTypeArgumentsForFieldProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").containerElementType(0, 0).constraint(new LengthDef().min(10).max(20)).containerElementType(0, 1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<FishTank>> violations = validator.validate(new FishTank());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"), violationOf(Length.class).withMessage("length must be between 10 and 20"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) LengthDef(org.hibernate.validator.cfg.defs.LengthDef) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

Validator (jakarta.validation.Validator)696 Test (org.testng.annotations.Test)600 ConstraintViolation (jakarta.validation.ConstraintViolation)433 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)255 HibernateValidator (org.hibernate.validator.HibernateValidator)217 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)216 TestForIssue (org.hibernate.validator.testutil.TestForIssue)181 ConstraintValidator (jakarta.validation.ConstraintValidator)145 ValidatorFactory (jakarta.validation.ValidatorFactory)106 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)88 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)71 NotNull (jakarta.validation.constraints.NotNull)48 Test (org.junit.Test)45 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)44 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)43 ConstraintViolationException (jakarta.validation.ConstraintViolationException)42 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)40 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)35 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)28 Size (jakarta.validation.constraints.Size)27