/** * 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); } } }
/** * 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); }
/** * 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(); }
Map<?, ?> currentBean = (Map<?, ?>) context.getBean(); MetaBean metaBean = context.getMetaBean(); final DynamicMetaBean dyn = getDynamicMetaBean(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))); } } }
/** * {@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)); }
if (access == null && prop.getMetaBean() != null) { // single property access strategy final Object bean = context.getBean(); final MetaBean mbean = context.getMetaBean(); } else if (access != null) { // different accesses to relation final Object bean = context.getBean(); final MetaBean mbean = context.getMetaBean(); for (AccessStrategy each : access) {
if (propPath == null) propPath = gcontext.getPropertyPath(); } else { if (context.getMetaProperty() == null) value = context.getBean(); else value = context.getPropertyValue(); message = messageTemplate; message, rootBean, context.getBean(), propPath, value, descriptor, rootBeanType, elementType); constraintViolations.add(ic);
int index = 0; DynamicMetaBean dyn = getDynamicMetaBean(context); Object[] array = (Object[]) context.getBean(); MetaBean metaBean = context.getMetaBean(); context.setCurrentIndex(null);
/** * {@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()); }