Search in sources :

Example 61 with ValidatorFactory

use of javax.validation.ValidatorFactory in project java-chassis by ServiceComb.

the class ParameterValidator method beforeMethodInvoke.

@Override
public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation, Object[] args) throws ConstraintViolationException {
    if (paramValidationEnabled.get()) {
        if (null == executableValidator) {
            ValidatorFactory factory = Validation.byDefaultProvider().configure().parameterNameProvider(new DefaultParameterNameProvider()).messageInterpolator(messageInterpolator()).buildValidatorFactory();
            executableValidator = factory.getValidator().forExecutables();
        }
        Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(producerOperation.getProducerInstance(), producerOperation.getProducerMethod(), args, Default.class);
        if (violations.size() > 0) {
            LOGGER.warn("Parameter validation failed : " + violations.toString());
            throw new ConstraintViolationException(violations);
        }
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException)

Example 62 with ValidatorFactory

use of javax.validation.ValidatorFactory in project dropwizard by dropwizard.

the class BootstrapTest method canUseCustomValidatorFactory.

@Test
void canUseCustomValidatorFactory() throws Exception {
    ValidatorFactory factory = Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory();
    bootstrap.setValidatorFactory(factory);
    assertThat(bootstrap.getValidatorFactory()).isSameAs(factory);
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) Test(org.junit.jupiter.api.Test)

Example 63 with ValidatorFactory

use of javax.validation.ValidatorFactory in project c4sg-services by Code4SocialGood.

the class CategoryCustomListTest method setUp.

@BeforeClass
public static void setUp() {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) BeforeClass(org.junit.BeforeClass)

Example 64 with ValidatorFactory

use of javax.validation.ValidatorFactory in project AngularBeans by bessemHmidi.

the class BeanValidator method validate.

public Set validate(Object bean) {
    ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
    Validator validator = vf.getValidator();
    Set<ConstraintViolation<Object>> errors = validator.validate(bean);
    for (ConstraintViolation<Object> violation : errors) {
        System.out.println((violation.getInvalidValue()) + ":" + violation.getMessage());
    // System.out.println(violation.getClass());
    }
    return errors;
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator)

Example 65 with ValidatorFactory

use of javax.validation.ValidatorFactory in project chassis by Kixeye.

the class TransportConfiguration method messageValidator.

@Bean
public Validator messageValidator() {
    // force hibernate validator to log to slf4j
    System.setProperty("org.jboss.logging.provider", "slf4j");
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    return factory.getValidator();
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) Bean(org.springframework.context.annotation.Bean)

Aggregations

ValidatorFactory (javax.validation.ValidatorFactory)106 Validator (javax.validation.Validator)31 Before (org.junit.Before)28 ConstraintViolation (javax.validation.ConstraintViolation)25 Test (org.junit.Test)17 BeforeClass (org.junit.BeforeClass)13 ConstraintViolationException (javax.validation.ConstraintViolationException)7 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)6 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)6 PlatformResourceBundleLocator (org.hibernate.validator.resourceloading.PlatformResourceBundleLocator)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 BeanManager (javax.enterprise.inject.spi.BeanManager)5 InitialContext (javax.naming.InitialContext)5 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)5 HashSet (java.util.HashSet)4 NamingException (javax.naming.NamingException)4 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)4 ValidatorContext (javax.validation.ValidatorContext)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4