Search in sources :

Example 1 with SmartValidator

use of org.springframework.validation.SmartValidator in project spring-framework by spring-projects.

the class PayloadArgumentResolver method validate.

/**
	 * Validate the payload if applicable.
	 * <p>The default implementation checks for {@code @javax.validation.Valid},
	 * Spring's {@link org.springframework.validation.annotation.Validated},
	 * and custom annotations whose name starts with "Valid".
	 * @param message the currently processed message
	 * @param parameter the method parameter
	 * @param target the target payload object
	 * @throws MethodArgumentNotValidException in case of binding errors
	 */
protected void validate(Message<?> message, MethodParameter parameter, Object target) {
    if (this.validator == null) {
        return;
    }
    for (Annotation ann : parameter.getParameterAnnotations()) {
        Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
            Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target, getParameterName(parameter));
            if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
                ((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
            } else {
                this.validator.validate(target, bindingResult);
            }
            if (bindingResult.hasErrors()) {
                throw new MethodArgumentNotValidException(message, parameter, bindingResult);
            }
            break;
        }
    }
}
Also used : Validated(org.springframework.validation.annotation.Validated) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SmartValidator(org.springframework.validation.SmartValidator) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 SmartValidator (org.springframework.validation.SmartValidator)1 Validated (org.springframework.validation.annotation.Validated)1