Search in sources :

Example 1 with BeanDescriptor

use of javax.validation.metadata.BeanDescriptor in project jersey by jersey.

the class DefaultConfiguredValidator method validateResult.

@Override
public void validateResult(final Object resource, final Invocable resourceMethod, final Object result) {
    if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
        final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
        final Method handlingMethod = resourceMethod.getHandlingMethod();
        final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
        final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes());
        final Method definitionMethod = resourceMethod.getDefinitionMethod();
        if (methodDescriptor != null && methodDescriptor.hasConstrainedReturnValue() && validateOnExecutionHandler.validateMethod(resource.getClass(), definitionMethod, handlingMethod)) {
            constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, result));
            if (result instanceof Response) {
                constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, ((Response) result).getEntity()));
            }
        }
        if (!constraintViolations.isEmpty()) {
            throw new ConstraintViolationException(constraintViolations);
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method) MethodDescriptor(javax.validation.metadata.MethodDescriptor) HashSet(java.util.HashSet)

Example 2 with BeanDescriptor

use of javax.validation.metadata.BeanDescriptor in project cuba by cuba-platform.

the class WebAbstractField method initBeanValidator.

protected void initBeanValidator() {
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
    Class enclosingJavaClass = propertyEnclosingMetaClass.getJavaClass();
    if (enclosingJavaClass != KeyValueEntity.class && !DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
        BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
        javax.validation.Validator validator = beanValidation.getValidator();
        BeanDescriptor beanDescriptor = validator.getConstraintsForClass(enclosingJavaClass);
        if (beanDescriptor.isBeanConstrained()) {
            addValidator(new BeanValidator(enclosingJavaClass, metaProperty.getName()));
        }
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) BeanValidator(com.haulmont.cuba.gui.components.validators.BeanValidator) MetaClass(com.haulmont.chile.core.model.MetaClass) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) BeanDescriptor(javax.validation.metadata.BeanDescriptor) MetaClass(com.haulmont.chile.core.model.MetaClass) KeyValueEntity(com.haulmont.cuba.core.entity.KeyValueEntity)

Example 3 with BeanDescriptor

use of javax.validation.metadata.BeanDescriptor in project minijax by minijax.

the class MinijaxValidator method validate.

@Override
public <T> Set<ConstraintViolation<T>> validate(final T object, final Class<?>... groups) {
    final MinijaxConstraintValidatorContext<T> context = new MinijaxConstraintValidatorContext<>(object);
    final BeanDescriptor descriptor = getConstraintsForClass(object.getClass());
    for (final PropertyDescriptor propertyDescriptor : descriptor.getConstrainedProperties()) {
        final Object value = ((MinijaxPropertyDescriptor) propertyDescriptor).getValue(object);
        validateProperty(context, propertyDescriptor, value);
    }
    return context.getResult();
}
Also used : MinijaxPropertyDescriptor(org.minijax.validator.metadata.MinijaxPropertyDescriptor) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) MinijaxPropertyDescriptor(org.minijax.validator.metadata.MinijaxPropertyDescriptor) MinijaxBeanDescriptor(org.minijax.validator.metadata.MinijaxBeanDescriptor) BeanDescriptor(javax.validation.metadata.BeanDescriptor)

Example 4 with BeanDescriptor

use of javax.validation.metadata.BeanDescriptor in project minijax by minijax.

the class MinijaxValidator method validateProperty.

@Override
public <T> Set<ConstraintViolation<T>> validateProperty(final T object, final String propertyName, final Class<?>... groups) {
    final MinijaxConstraintValidatorContext<T> context = new MinijaxConstraintValidatorContext<>(object);
    final BeanDescriptor descriptor = getConstraintsForClass(object.getClass());
    final PropertyDescriptor propertyDescriptor = descriptor.getConstraintsForProperty(propertyName);
    final Object value = ((MinijaxPropertyDescriptor) propertyDescriptor).getValue(object);
    validateProperty(context, propertyDescriptor, value);
    return context.getResult();
}
Also used : MinijaxPropertyDescriptor(org.minijax.validator.metadata.MinijaxPropertyDescriptor) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) MinijaxPropertyDescriptor(org.minijax.validator.metadata.MinijaxPropertyDescriptor) MinijaxBeanDescriptor(org.minijax.validator.metadata.MinijaxBeanDescriptor) BeanDescriptor(javax.validation.metadata.BeanDescriptor)

Example 5 with BeanDescriptor

use of javax.validation.metadata.BeanDescriptor in project dropwizard by dropwizard.

the class DropwizardConfiguredValidator method validateResourceAndInputParams.

@Override
public void validateResourceAndInputParams(Object resource, final Invocable invocable, Object[] objects) throws ConstraintViolationException {
    final Class<?>[] groups = getGroup(invocable);
    final Set<ConstraintViolation<Object>> violations = new HashSet<>();
    final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
    if (beanDescriptor.isBeanConstrained()) {
        violations.addAll(validate(resource, groups));
    }
    violations.addAll(forExecutables().validateParameters(resource, invocable.getHandlingMethod(), objects, groups));
    if (!violations.isEmpty()) {
        throw new JerseyViolationException(violations, invocable);
    }
}
Also used : BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) HashSet(java.util.HashSet)

Aggregations

BeanDescriptor (javax.validation.metadata.BeanDescriptor)12 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)5 MetaClass (com.haulmont.chile.core.model.MetaClass)3 KeyValueEntity (com.haulmont.cuba.core.entity.KeyValueEntity)3 HashSet (java.util.HashSet)3 ConstraintViolation (javax.validation.ConstraintViolation)3 BeanValidation (com.haulmont.cuba.core.global.BeanValidation)2 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)2 BeanValidator (com.haulmont.cuba.gui.components.validators.BeanValidator)2 Method (java.lang.reflect.Method)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 MethodDescriptor (javax.validation.metadata.MethodDescriptor)2 MinijaxBeanDescriptor (org.minijax.validator.metadata.MinijaxBeanDescriptor)2 MinijaxPropertyDescriptor (org.minijax.validator.metadata.MinijaxPropertyDescriptor)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Response (javax.ws.rs.core.Response)1 Invocable (org.glassfish.jersey.server.model.Invocable)1