protected <T extends ValidationListener> void validateMandatory(ValidationContext<T> context) { if (context.getMetaProperty().isMandatory()) { if (context.getPropertyValue() == null) { context.getListener().addError(MANDATORY, context); } } }
int index = 0; DynamicMetaBean dyn = getDynamicMetaBean(context); Object[] array = (Object[]) context.getBean(); MetaBean metaBean = context.getMetaBean(); context.setCurrentIndex(null); context.setCurrentIndex(index++); if (each == null) { continue; // Null values are not validated context.setBean(each); } else { context.setBean(each, dyn.resolveMetaBean(each)); context.moveUp(array, metaBean);
/** * API to add an error to the validation results. * * @param reason - Features from {@link org.apache.bval.model.Features.Property} * or custom reason of validation error * @param context - context information (bean, propertyName, value, ...) */ @Override public <T extends ValidationListener> void addError(String reason, ValidationContext<T> context) { Error error = createError(reason, context.getBean(), context.getPropertyName()); addError(error, context); }
/** * {@inheritDoc} */ @Override protected void complete() { super.complete(); if (validationContext.getMetaProperty() != null) { return; } if (validationContext.getMetaBean() instanceof DynamicMetaBean) { validationContext.setMetaBean(validationContext.getMetaBean().resolveMetaBean( ObjectUtils.defaultIfNull(validationContext.getBean(), rawType))); } } }
/** * Convenience API. validate a single property. * * @param bean * - the root object * @param metaProperty * - metadata for the property * @return validation results */ public T validateProperty(Object bean, MetaProperty metaProperty) { ValidationContext<T> context = createContext(); context.setBean(bean); context.setMetaProperty(metaProperty); ValidationHelper.validateProperty(context); return context.getListener(); }
if (propPath == null) propPath = gcontext.getPropertyPath(); } else { if (context.getMetaProperty() == null) value = context.getBean(); else value = context.getPropertyValue(); message = messageTemplate; if (propPath == null) propPath = PathImpl.createPathFromString(context.getPropertyName()); descriptor = null; ElementType elementType = (context.getAccess() != null) ? context.getAccess().getElementType() : null; ConstraintViolationImpl<T> ic = new ConstraintViolationImpl<T>(messageTemplate, message, rootBean, context.getBean(), propPath, value, descriptor, rootBeanType, elementType); constraintViolations.add(ic);
/** * {@inheritDoc} */ public void handleIndexOrKey(String token) { moveDownIfNecessary(); AccessStrategy access; if (IndexedAccess.getJavaElementType(type) != null) { try { Integer index = token == null ? null : Integer.valueOf(token); access = new IndexedAccess(type, index); validationContext.setCurrentIndex(index); } catch (NumberFormatException e) { throw new UnknownPropertyException(String.format("Cannot parse %s as an array/iterable index", token), e); } } else if (KeyedAccess.getJavaElementType(type) != null) { access = new KeyedAccess(type, token); validationContext.setCurrentKey(token); } else { throw new UnknownPropertyException(String.format("Cannot determine index/key type for %s", type)); } Object value = validationContext.getBean(); Object child = value == null ? null : access.get(value); setType(child == null ? access.getJavaType() : child.getClass()); validationContext.setBean(child, validationContext.getMetaBean().resolveMetaBean(child == null ? rawType : child)); }
Map<?, ?> currentBean = (Map<?, ?>) context.getBean(); MetaBean metaBean = context.getMetaBean(); final DynamicMetaBean dyn = getDynamicMetaBean(context); context.setCurrentKey(null); try { for (Map.Entry<?, ?> entry : currentBean.entrySet()) { continue; context.setCurrentKey(entry.getKey()); if (dyn == null) { context.setBean(value); } else { context.setBean(value, dyn.resolveMetaBean(value)); context.moveUp(currentBean, metaBean);
/** * {@inheritDoc} */ public void handleProperty(String token) { moveDownIfNecessary(); MetaBean metaBean = validationContext.getMetaBean(); if (metaBean instanceof DynamicMetaBean) { metaBean = metaBean.resolveMetaBean(ObjectUtils.defaultIfNull(validationContext.getBean(), rawType)); validationContext.setMetaBean(metaBean); } MetaProperty mp = metaBean.getProperty(token); if (mp == null) { // TODO this could indicate a property hosted on a superclass; should we shunt the context traversal down a path based on that type? PropertyAccess access = new PropertyAccess(rawType, token); if (access.isKnown()) { // add heretofore unknown, but valid, property on the fly: mp = Jsr303MetaBeanFactory.addMetaProperty(metaBean, access); } else { throw new UnknownPropertyException("unknown property '" + token + "' in " + metaBean.getId()); } } validationContext.setMetaProperty(mp); setType(mp.getType()); }
if (access == null && prop.getMetaBean() != null) { // single property access strategy final Object bean = context.getBean(); final MetaBean mbean = context.getMetaBean(); context.moveDown(prop, PropertyAccess.getInstance(bean.getClass(), prop.getName())); ValidationHelper.validateContext(context, new BeanValidatorCallback(context), treatMapsLikeBeans); context.moveUp(bean, mbean); } else if (access != null) { // different accesses to relation final Object bean = context.getBean(); final MetaBean mbean = context.getMetaBean(); for (AccessStrategy each : access) { context.moveDown(prop, each); ValidationHelper.validateContext(context, new BeanValidatorCallback(context), treatMapsLikeBeans); context.moveUp(bean, mbean);
/** * If we currently have a property, navigate the context such that the property becomes the bean, in preparation for * another property. * * @param validationContext */ public void moveDownIfNecessary() { MetaProperty mp = validationContext.getMetaProperty(); if (mp != null) { if (mp.getMetaBean() == null) { throw new UnknownPropertyException(String.format("Property %s.%s is not cascaded", mp .getParentMetaBean().getId(), mp.getName())); } validationContext.moveDown(mp, new NullSafePropertyAccess(validationContext.getMetaBean().getBeanClass(), mp.getName())); } }
@Override public <T extends ValidationListener> void validate(ValidationContext<T> context) { if (context.getPropertyValue() == null) return; if (!EMailValidationUtils.isValid(context.getPropertyValue(), getPattern())) { context.getListener().addError(Reasons.EMAIL_ADDRESS, context); } }
/** * Validate a single bean only, no related beans will be validated. */ public static <VL extends ValidationListener> void validateBean(ValidationContext<VL> context) { // execute all property level validations for (MetaProperty prop : context.getMetaBean().getProperties()) { context.setMetaProperty(prop); validateProperty(context); } // execute all bean level validations context.setMetaProperty(null); for (Validation validation : context.getMetaBean().getValidations()) { validation.validate(context); } }
/** * Convenience API. validate a root object with all related objects * according to the metaBean. * * @param bean * - a single bean or a collection of beans (that share the same * metaBean!) * @param metaBean * @return results - validation results found */ public T validate(Object bean, MetaBean metaBean) { ValidationContext<T> context = createContext(); context.setBean(bean, metaBean); ValidationHelper.validateContext(context, new BeanValidatorCallback(context), treatMapsLikeBeans); return context.getListener(); }
/** * Validates a single object. * * @param <VL> * @param context * The validation context, its current bean must be a single * object. * @param s */ protected static <VL extends ValidationListener> void validateBeanInContext(ValidationContext<VL> context, ValidateCallback s) { if (getDynamicMetaBean(context) != null) { context.setMetaBean(getDynamicMetaBean(context).resolveMetaBean(context.getBean())); } s.validate(); }
/** * {@inheritDoc} internal validate a bean (=not a collection of beans) and * its related beans */ protected <VL extends ValidationListener> void validateBeanNet(ValidationContext<VL> context) { if (context.collectValidated()) { ValidationHelper.validateBean(context); for (MetaProperty prop : context.getMetaBean().getProperties()) { validateRelatedBean(context, prop); } } }
/** * Validate a single property only. Performs all validations * for this property. */ public static <VL extends ValidationListener> void validateProperty(ValidationContext<VL> context) { for (Validation validation : context.getMetaProperty().getValidations()) { validation.validate(context); } } }
/** * @param <VL> * @param context * The current validation context. * @return the current {@link DynamicMetaBean} in context, or * <code>null</code> if the current meta bean is not dynamic. */ private static <VL extends ValidationListener> DynamicMetaBean getDynamicMetaBean(ValidationContext<VL> context) { return context.getMetaBean() instanceof DynamicMetaBean ? (DynamicMetaBean) context.getMetaBean() : null; }
private <T extends ValidationListener> int compare(ValidationContext<T> context, Comparable<Object> constraintValue, Object currentValue) { int r; if (constraintValue.getClass().isAssignableFrom(currentValue.getClass())) { r = constraintValue.compareTo(context.getPropertyValue()); } else if (currentValue instanceof Number) { double dv = ((Number) currentValue).doubleValue(); double mdv = ((Number) constraintValue).doubleValue(); r = mdv > dv ? 1 : -1; } else { r = String.valueOf(constraintValue).compareTo(String.valueOf(currentValue)); } return r; }
/** * validate a complex 'bean' with related beans according to * validation rules in 'metaBean' * * @param context * - the context is initialized with: <br> * bean - the root object start validation at * or a collection of root objects <br> * metaBean - the meta information for the root * object(s) * @param context * The current validation context. */ public static void validateContext(ValidationContext<?> context, ValidateCallback s, boolean treatMapsLikeBeans) { if (context.getBean() != null) { if (!treatMapsLikeBeans && context.getBean() instanceof Map<?, ?>) { validateMapInContext(context, s); } else if (context.getBean() instanceof Iterable<?>) { validateIterableInContext(context, s); } else if (context.getBean() instanceof Object[]) { validateArrayInContext(context, s); } else { // to One Bean (or Map like Bean) validateBeanInContext(context, s); } } }