Search in sources :

Example 6 with HibernateValidatorConfiguration

use of org.hibernate.validator.HibernateValidatorConfiguration in project cuba by cuba-platform.

the class BeanValidationImpl method getValidatorFactoryConfiguration.

protected HibernateValidatorConfiguration getValidatorFactoryConfiguration(Supplier<Locale> localeSupplier) {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure().clockProvider(new CubaValidationTimeProvider(timeSource)).traversableResolver(new CubaValidationTraversableResolver(metadata, entityStates)).messageInterpolator(new CubaValidationMessagesInterpolator(messages, localeSupplier));
    ConstraintMapping constraintMapping = configuration.createConstraintMapping();
    // Hibernate validators doesn't support java.sql.Date.
    // Replace standard validators for java.util.Date with support java.sql.Date
    registerPastValidators(constraintMapping.constraintDefinition(Past.class));
    registerPastOrPresentValidators(constraintMapping.constraintDefinition(PastOrPresent.class));
    registerFutureValidators(constraintMapping.constraintDefinition(Future.class));
    registerFutureOrPresentValidators(constraintMapping.constraintDefinition(FutureOrPresent.class));
    configuration.addMapping(constraintMapping);
    return configuration;
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) FutureOrPresent(javax.validation.constraints.FutureOrPresent) Past(javax.validation.constraints.Past) Future(javax.validation.constraints.Future) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) CubaValidationTimeProvider(com.haulmont.cuba.core.sys.validation.CubaValidationTimeProvider) CubaValidationTraversableResolver(com.haulmont.cuba.core.sys.validation.CubaValidationTraversableResolver) CubaValidationMessagesInterpolator(com.haulmont.cuba.core.sys.validation.CubaValidationMessagesInterpolator) PastOrPresent(javax.validation.constraints.PastOrPresent)

Example 7 with HibernateValidatorConfiguration

use of org.hibernate.validator.HibernateValidatorConfiguration in project cuba by cuba-platform.

the class BeanValidationImpl method getValidator.

@Override
public Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options) {
    checkNotNullArgument(options);
    if (constraintMapping == null && options.getFailFast() == null && options.getLocale() != null) {
        HibernateValidatorConfiguration configuration = getValidatorFactoryConfiguration(options::getLocale);
        ValidatorFactory factory = configuration.buildValidatorFactory();
        return factory.getValidator();
    }
    Locale locale;
    if (options.getLocale() != null) {
        locale = options.getLocale();
    } else {
        locale = getCurrentLocale();
    }
    HibernateValidatorConfiguration configuration = getValidatorFactoryConfiguration(() -> locale);
    if (options.getFailFast() != null) {
        configuration.failFast(options.getFailFast());
    }
    if (constraintMapping != null) {
        configuration.addMapping(constraintMapping);
    }
    ValidatorFactory factory = configuration.buildValidatorFactory();
    return factory.getValidator();
}
Also used : Locale(java.util.Locale) ValidatorFactory(javax.validation.ValidatorFactory) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration)

Example 8 with HibernateValidatorConfiguration

use of org.hibernate.validator.HibernateValidatorConfiguration in project JessMA by ldcsaa.

the class HibernateBeanValidator method tryCreateValidator.

private Validator tryCreateValidator(final String bundle, final Locale locale, ValidatorKey key) {
    HibernateValidatorConfiguration configure = Validation.byProvider(HibernateValidator.class).configure();
    configure.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(bundle)) {

        @Override
        public String interpolate(String message, Context context) {
            return super.interpolate(message, context, locale);
        }
    });
    ValidatorFactory vf = configure.buildValidatorFactory();
    Validator validator = vf.getValidator();
    GeneralHelper.syncTryPut(validatorMap, key, validator);
    return validatorMap.get(key);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ValidatorFactory(javax.validation.ValidatorFactory) PlatformResourceBundleLocator(org.hibernate.validator.resourceloading.PlatformResourceBundleLocator) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) BeanValidator(org.jessma.mvc.BeanValidator)

Example 9 with HibernateValidatorConfiguration

use of org.hibernate.validator.HibernateValidatorConfiguration in project wildfly by wildfly.

the class BootStrapValidationTestCase method testSafeHTML.

/**
     * Ensure that including a cross-site script attack triggers the @SafeHtml validation rule
     */
@Test
public void testSafeHTML() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Employee emp = new Employee();
    // create employee
    emp.setFirstName("Joe");
    emp.setLastName("Cocker");
    emp.setEmail("none@jboss.org");
    emp.setWebsite("<script> Cross-site scripting http://en.wikipedia.org/wiki/Joe_Cocker <script/>.");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ConstraintValidator(javax.validation.ConstraintValidator) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Example 10 with HibernateValidatorConfiguration

use of org.hibernate.validator.HibernateValidatorConfiguration in project wildfly by wildfly.

the class BootStrapValidationTestCase method testCustomConstraintValidatorFactory.

@Test
public void testCustomConstraintValidatorFactory() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Employee emp = new Employee();
    // create employee
    emp.setEmpId("M1234");
    emp.setFirstName("MADHUMITA");
    emp.setLastName("SADHUKHAN");
    emp.setEmail("madhu@redhat.com");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals("Created by default factory", constraintViolations.iterator().next().getMessage());
    // get a new factory using a custom configuration
    configuration.constraintValidatorFactory(new CustomConstraintValidatorFactory(configuration.getDefaultConstraintValidatorFactory()));
    factory = configuration.buildValidatorFactory();
    validator = factory.getValidator();
    constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals("Created by custom factory", constraintViolations.iterator().next().getMessage());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ConstraintValidator(javax.validation.ConstraintValidator) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Aggregations

HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)10 ValidatorFactory (javax.validation.ValidatorFactory)7 HibernateValidator (org.hibernate.validator.HibernateValidator)6 Validator (javax.validation.Validator)5 ConstraintViolation (javax.validation.ConstraintViolation)4 Test (org.junit.Test)4 ConstraintValidator (javax.validation.ConstraintValidator)2 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)2 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)2 CubaValidationMessagesInterpolator (com.haulmont.cuba.core.sys.validation.CubaValidationMessagesInterpolator)1 CubaValidationTimeProvider (com.haulmont.cuba.core.sys.validation.CubaValidationTimeProvider)1 CubaValidationTraversableResolver (com.haulmont.cuba.core.sys.validation.CubaValidationTraversableResolver)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1 PostConstruct (javax.annotation.PostConstruct)1 MessageInterpolator (javax.validation.MessageInterpolator)1 ValidationProviderResolver (javax.validation.ValidationProviderResolver)1 Future (javax.validation.constraints.Future)1 FutureOrPresent (javax.validation.constraints.FutureOrPresent)1 Past (javax.validation.constraints.Past)1