Search in sources :

Example 1 with Validator

use of javax.validation.Validator in project druid by druid-io.

the class S3DataSegmentPusherConfigTest method testSerializationValidatingMaxListingLength.

@Test
public void testSerializationValidatingMaxListingLength() throws IOException {
    String jsonConfig = "{\"bucket\":\"bucket1\",\"baseKey\":\"dataSource1\"," + "\"disableAcl\":false,\"maxListingLength\":-1}";
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    S3DataSegmentPusherConfig config = jsonMapper.readValue(jsonConfig, S3DataSegmentPusherConfig.class);
    Set<ConstraintViolation<S3DataSegmentPusherConfig>> violations = validator.validate(config);
    Assert.assertEquals(1, violations.size());
    ConstraintViolation violation = Iterators.getOnlyElement(violations.iterator());
    Assert.assertEquals("must be greater than or equal to 0", violation.getMessage());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 2 with Validator

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

the class ConstraintViolationBenchmark method prepare.

@Setup
public void prepare() {
    final Validator validator = Validators.newValidator();
    final ExecutableValidator execValidator = validator.forExecutables();
    final Set<ConstraintViolation<ConstraintViolationBenchmark.Resource>> paramViolations = execValidator.validateParameters(new Resource(), getAccessibleMethod(ConstraintViolationBenchmark.Resource.class, "paramFunc", String.class), // the parameter value
    new Object[] { "" });
    paramViolation = paramViolations.iterator().next();
    final Set<ConstraintViolation<ConstraintViolationBenchmark.Resource>> objViolations = execValidator.validateParameters(new Resource(), getAccessibleMethod(ConstraintViolationBenchmark.Resource.class, "objectFunc", Foo.class), // the parameter value
    new Object[] { new Foo() });
    objViolation = objViolations.iterator().next();
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) ExecutableValidator(javax.validation.executable.ExecutableValidator) Setup(org.openjdk.jmh.annotations.Setup)

Example 3 with Validator

use of javax.validation.Validator in project platformlayer by platformlayer.

the class PlatformlayerValidationModule method configure.

@Override
protected void configure() {
    Configuration<?> config = Validation.byDefaultProvider().configure();
    // config.messageInterpolator(new MyMessageInterpolator())
    // .traversableResolver( new MyTraversableResolver())
    // .constraintValidatorFactory(new MyConstraintValidatorFactory());
    config.messageInterpolator(new ValidationMessageInterpolator());
    ValidatorFactory factory = config.buildValidatorFactory();
    // ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    bind(Validator.class).toInstance(validator);
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) Validator(javax.validation.Validator)

Example 4 with Validator

use of javax.validation.Validator 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 5 with Validator

use of javax.validation.Validator in project torodb by torodb.

the class ConfigUtils method validateBean.

public static <T> void validateBean(T config) {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(config);
    if (!constraintViolations.isEmpty()) {
        IllegalArgumentException illegalArgumentException = transformConstraintsValidation(constraintViolations);
        throw illegalArgumentException;
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator)

Aggregations

Validator (javax.validation.Validator)42 Test (org.junit.Test)25 ConstraintViolation (javax.validation.ConstraintViolation)19 ValidatorFactory (javax.validation.ValidatorFactory)13 InitialContext (javax.naming.InitialContext)8 ArrayList (java.util.ArrayList)5 HibernateValidator (org.hibernate.validator.HibernateValidator)5 File (java.io.File)4 NamingException (javax.naming.NamingException)4 ConstraintViolationException (javax.validation.ConstraintViolationException)4 Title (net.thucydides.core.annotations.Title)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 PodamFactory (uk.co.jemos.podam.api.PodamFactory)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashSet (java.util.HashSet)3 Context (javax.naming.Context)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2