Search in sources :

Example 1 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 2 with HibernateValidatorConfiguration

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

the class BeanValidationImpl method getValidatorWithDefaultFactory.

protected Validator getValidatorWithDefaultFactory(Locale locale) {
    ValidatorFactory validatorFactoryFromCache = validatorFactoriesCache.get(locale);
    if (validatorFactoryFromCache != null) {
        return validatorFactoryFromCache.getValidator();
    }
    HibernateValidatorConfiguration configuration = getValidatorFactoryConfiguration(locale);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    validatorFactoriesCache.put(locale, factory);
    return factory.getValidator();
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration)

Example 3 with HibernateValidatorConfiguration

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

the class SafeHtmlValidationTestCase 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) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Example 4 with HibernateValidatorConfiguration

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

the class MessageInterpolationValidationTestCase method testCustomMessageInterpolation.

@Test
public void testCustomMessageInterpolation() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    final MessageInterpolator messageInterpolator = new CustomMessageInterpolator();
    configuration.messageInterpolator(messageInterpolator);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    // create employee
    Employee emp = new Employee();
    emp.setEmail("MADHUMITA");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals(CustomMessageInterpolator.MESSAGE, constraintViolations.iterator().next().getMessage());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) MessageInterpolator(javax.validation.MessageInterpolator) Test(org.junit.Test)

Example 5 with HibernateValidatorConfiguration

use of org.hibernate.validator.HibernateValidatorConfiguration in project jo-client-platform by jo-source.

the class HibernateBeanValidatorFactory method create.

@Override
public Validator create() {
    final ValidationProviderResolver validationProviderResolver = new ValidationProviderResolver() {

        @Override
        public List<ValidationProvider<?>> getValidationProviders() {
            final List<ValidationProvider<?>> result = new LinkedList<ValidationProvider<?>>();
            result.add(new HibernateValidator());
            return result;
        }
    };
    final ProviderSpecificBootstrap<HibernateValidatorConfiguration> bootstrap = Validation.byProvider(HibernateValidator.class);
    bootstrap.providerResolver(validationProviderResolver);
    final HibernateValidatorConfiguration validatorConfiguration = bootstrap.configure();
    return validatorConfiguration.buildValidatorFactory().getValidator();
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidationProvider(javax.validation.spi.ValidationProvider) ValidationProviderResolver(javax.validation.ValidationProviderResolver) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) LinkedList(java.util.LinkedList)

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