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;
}
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();
}
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);
}
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);
}
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());
}
Aggregations