Search in sources :

Example 11 with ValidationException

use of jakarta.validation.ValidationException in project morphia by mongodb.

the class TestMorphiaValidation method testValidation.

/**
 * Test method for {@link ValidationExtension#prePersist(Object, Document, Mapper)}.
 */
public void testValidation() {
    final Data data = new Data();
    data.email = "not an email";
    getDs().getMapper().addInterceptor(new ValidationExtension());
    try {
        getDs().save(data);
        Assert.fail("Should have failed validation");
    } catch (ValidationException exception) {
        assertTrue(exception.getMessage().contains("Data.email:not a well-formed email address ('not an email')"));
    }
    data.email = "foo@bar.com";
    getDs().save(data);
}
Also used : ValidationException(jakarta.validation.ValidationException) ValidationExtension(dev.morphia.validation.ValidationExtension)

Example 12 with ValidationException

use of jakarta.validation.ValidationException in project mojarra by eclipse-ee4j.

the class BeanValidator method getValidatorFactory.

private static ValidatorFactory getValidatorFactory(FacesContext context) {
    ValidatorFactory validatorFactory = null;
    Object cachedObject = context.getExternalContext().getApplicationMap().get(VALIDATOR_FACTORY_KEY);
    if (cachedObject instanceof ValidatorFactory) {
        validatorFactory = (ValidatorFactory) cachedObject;
    } else {
        try {
            validatorFactory = Validation.buildDefaultValidatorFactory();
        } catch (ValidationException e) {
            throw new FacesException("Could not build a default Bean Validator factory", e);
        }
        context.getExternalContext().getApplicationMap().put(VALIDATOR_FACTORY_KEY, validatorFactory);
    }
    return validatorFactory;
}
Also used : ValidationException(jakarta.validation.ValidationException) ValidatorFactory(jakarta.validation.ValidatorFactory) FacesException(jakarta.faces.FacesException)

Example 13 with ValidationException

use of jakarta.validation.ValidationException in project morphia by MorphiaOrg.

the class TestMorphiaValidation method testValidation.

/**
 * Test method for {@link ValidationExtension#prePersist(Object, Document, Mapper)}.
 */
public void testValidation() {
    final Data data = new Data();
    data.email = "not an email";
    getDs().getMapper().addInterceptor(new ValidationExtension());
    try {
        getDs().save(data);
        Assert.fail("Should have failed validation");
    } catch (ValidationException exception) {
        assertTrue(exception.getMessage().contains("Data.email:not a well-formed email address ('not an email')"));
    }
    data.email = "foo@bar.com";
    getDs().save(data);
}
Also used : ValidationException(jakarta.validation.ValidationException) ValidationExtension(dev.morphia.validation.ValidationExtension)

Example 14 with ValidationException

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

the class InvalidConfiguredClassInheritanceMethodValidationTest method testInvalidConfigurationThrowsException.

@Test
public void testInvalidConfigurationThrowsException() {
    ValidationExtension extension = beanManager.getExtension(ValidationExtension.class);
    AnnotatedType<CIA> annotatedType = beanManager.createAnnotatedType(CIA.class);
    try {
        extension.processAnnotatedType(new ProcessAnnotatedTypeImpl<CIA>(annotatedType));
        fail("ValidationExtension should throw an exception, because the validated method overrides another " + "method and adds @ValidateOnExecution ");
    } catch (ValidationException e) {
        assertThat(e.getMessage()).startsWith("HV000166");
    }
}
Also used : ValidationException(jakarta.validation.ValidationException) ValidationExtension(org.hibernate.validator.cdi.ValidationExtension) Test(org.testng.annotations.Test)

Example 15 with ValidationException

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

the class PredefinedScopeValidatorFactoryTest method validatorSpecificTraversableResolver.

@Test
public void validatorSpecificTraversableResolver() {
    Set<Class<?>> beanMetaDataToInitialize = new HashSet<>();
    beanMetaDataToInitialize.add(Bean.class);
    beanMetaDataToInitialize.add(AnotherBean.class);
    beanMetaDataToInitialize.add(SomeEnum.class);
    ValidatorFactory validatorFactory = Validation.byProvider(PredefinedScopeHibernateValidator.class).configure().builtinConstraints(new HashSet<>(Arrays.asList(Email.class.getName(), NotNull.class.getName()))).initializeBeanMetaData(beanMetaDataToInitialize).buildValidatorFactory();
    try {
        Validator validator = validatorFactory.usingContext().traversableResolver(new ThrowExceptionTraversableResolver()).getValidator();
        validator.validate(new Bean());
        fail();
    } catch (ValidationException e) {
        Assertions.assertThat(e).hasCauseExactlyInstanceOf(ValidatorSpecificTraversableResolverUsedException.class);
    }
}
Also used : Email(jakarta.validation.constraints.Email) ValidationException(jakarta.validation.ValidationException) ValidatorFactory(jakarta.validation.ValidatorFactory) NotNull(jakarta.validation.constraints.NotNull) Validator(jakarta.validation.Validator) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

ValidationException (jakarta.validation.ValidationException)18 ValidatorFactory (jakarta.validation.ValidatorFactory)6 Test (org.testng.annotations.Test)5 Validator (jakarta.validation.Validator)3 NotNull (jakarta.validation.constraints.NotNull)3 ValidationExtension (dev.morphia.validation.ValidationExtension)2 FacesException (jakarta.faces.FacesException)2 Constraint (jakarta.validation.Constraint)2 TraversableResolver (jakarta.validation.TraversableResolver)2 ExecutableType (jakarta.validation.executable.ExecutableType)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 LoadClass (org.hibernate.validator.internal.util.privilegedactions.LoadClass)2 BranchContext (cd.go.contrib.plugins.configrepo.groovy.dsl.BranchContext)1 BootstrapConfiguration (jakarta.validation.BootstrapConfiguration)1 ConstraintDeclarationException (jakarta.validation.ConstraintDeclarationException)1 ConstraintDefinitionException (jakarta.validation.ConstraintDefinitionException)1 GroupDefinitionException (jakarta.validation.GroupDefinitionException)1 Payload (jakarta.validation.Payload)1 Validation.buildDefaultValidatorFactory (jakarta.validation.Validation.buildDefaultValidatorFactory)1 AssertFalse (jakarta.validation.constraints.AssertFalse)1