Search in sources :

Example 1 with PropertyDescriptor

use of javax.validation.metadata.PropertyDescriptor 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 2 with PropertyDescriptor

use of javax.validation.metadata.PropertyDescriptor 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 3 with PropertyDescriptor

use of javax.validation.metadata.PropertyDescriptor in project flow by vaadin.

the class BeanValidationBinder method configureRequired.

private void configureRequired(BindingBuilder<BEAN, ?> binding, PropertyDefinition<BEAN, ?> definition, BeanValidator validator) {
    assert requiredConfigurator != null;
    Class<?> propertyHolderType = definition.getPropertyHolderType();
    BeanDescriptor descriptor = validator.getJavaxBeanValidator().getConstraintsForClass(propertyHolderType);
    PropertyDescriptor propertyDescriptor = descriptor.getConstraintsForProperty(definition.getTopLevelName());
    if (propertyDescriptor == null) {
        return;
    }
    if (propertyDescriptor.getConstraintDescriptors().stream().map(ConstraintDescriptor::getAnnotation).anyMatch(constraint -> requiredConfigurator.test(constraint, binding))) {
        binding.getField().setRequiredIndicatorVisible(true);
    }
}
Also used : PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) BeanDescriptor(javax.validation.metadata.BeanDescriptor)

Example 4 with PropertyDescriptor

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

the class MinijaxValidator method validateValue.

@Override
public <T> Set<ConstraintViolation<T>> validateValue(final Class<T> beanType, final String propertyName, final Object value, final Class<?>... groups) {
    final MinijaxConstraintValidatorContext<T> context = new MinijaxConstraintValidatorContext<>(null);
    final PropertyDescriptor property = getConstraintsForClass(beanType).getConstraintsForProperty(propertyName);
    validateProperty(context, property, value);
    return context.getResult();
}
Also used : MinijaxPropertyDescriptor(org.minijax.validator.metadata.MinijaxPropertyDescriptor) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor)

Example 5 with PropertyDescriptor

use of javax.validation.metadata.PropertyDescriptor in project kie-wb-common by kiegroup.

the class ContextModelConstraintsExtractorImpl method readModelConstraints.

@Override
public void readModelConstraints(MapModelRenderingContext clientRenderingContext, ClassLoader classLoader) {
    if (clientRenderingContext == null) {
        throw new IllegalArgumentException("Context cannot be null");
    }
    if (classLoader == null) {
        throw new IllegalArgumentException("ClassLoader cannot be null");
    }
    clientRenderingContext.getAvailableForms().values().forEach(formDefinition -> {
        if (formDefinition.getModel() instanceof JavaFormModel) {
            JavaFormModel javaModel = (JavaFormModel) formDefinition.getModel();
            if (clientRenderingContext.getModelConstraints().containsKey(javaModel)) {
                return;
            }
            Class clazz = null;
            try {
                clazz = classLoader.loadClass(javaModel.getType());
                if (clazz == null) {
                    clazz = getClass().forName(javaModel.getType());
                }
            } catch (ClassNotFoundException e) {
                // maybe Class is not on the project ClassLoader, let's try on the main ClassLoader
                try {
                    clazz = getClass().forName(javaModel.getType());
                } catch (ClassNotFoundException e1) {
                // ops! class not available on the main classLoader
                }
            }
            if (clazz == null) {
                logger.warn("Unable to find class for type {} on any classLoader. Skipping annotation processing", javaModel.getType());
            } else {
                BeanDescriptor descriptor = Validation.buildDefaultValidatorFactory().getValidator().getConstraintsForClass(clazz);
                Set<PropertyDescriptor> properties = descriptor.getConstrainedProperties();
                DynamicModelConstraints constraints = new DynamicModelConstraints(javaModel.getType());
                clientRenderingContext.getModelConstraints().put(javaModel.getType(), constraints);
                properties.forEach(property -> {
                    property.getConstraintDescriptors().forEach(constraintDescriptor -> {
                        Map<String, Object> attributes = new HashMap<>();
                        constraintDescriptor.getAttributes().forEach((key, value) -> {
                            if (key.equals("payload") || key.equals("groups")) {
                                return;
                            }
                            Object portableValue;
                            if (EnvUtil.isPortableType(value.getClass())) {
                                portableValue = value;
                            } else {
                                portableValue = value.toString();
                            }
                            attributes.put(key, portableValue);
                        });
                        constraints.addConstraintForField(property.getPropertyName(), new FieldConstraint(constraintDescriptor.getAnnotation().annotationType().getName(), attributes));
                    });
                });
            }
        }
    });
}
Also used : DynamicModelConstraints(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) HashMap(java.util.HashMap) BeanDescriptor(javax.validation.metadata.BeanDescriptor) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) FieldConstraint(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.FieldConstraint)

Aggregations

PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)6 BeanDescriptor (javax.validation.metadata.BeanDescriptor)5 MinijaxPropertyDescriptor (org.minijax.validator.metadata.MinijaxPropertyDescriptor)3 MinijaxBeanDescriptor (org.minijax.validator.metadata.MinijaxBeanDescriptor)2 HashMap (java.util.HashMap)1 Component (org.hibernate.mapping.Component)1 Property (org.hibernate.mapping.Property)1 DynamicModelConstraints (org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints)1 FieldConstraint (org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.FieldConstraint)1 JavaFormModel (org.kie.workbench.common.forms.model.JavaFormModel)1