Search in sources :

Example 1 with Validated

use of org.springframework.validation.annotation.Validated 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)

Example 2 with Validated

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

the class ModelAttributeMethodProcessor method validateIfApplicable.

/**
	 * Validate the model attribute 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 binder the DataBinder to be used
	 * @param methodParam the method parameter
	 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter methodParam) {
    Annotation[] annotations = methodParam.getParameterAnnotations();
    for (Annotation ann : annotations) {
        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 });
            binder.validate(validationHints);
            break;
        }
    }
}
Also used : Validated(org.springframework.validation.annotation.Validated) Annotation(java.lang.annotation.Annotation)

Example 3 with Validated

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

the class ModelAttributeMethodArgumentResolver method validateIfApplicable.

private void validateIfApplicable(WebExchangeDataBinder binder, MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation ann : annotations) {
        Validated validAnnot = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validAnnot != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = (validAnnot != null ? validAnnot.value() : AnnotationUtils.getValue(ann));
            Object hintArray = (hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            binder.validate(hintArray);
        }
    }
}
Also used : Validated(org.springframework.validation.annotation.Validated) Annotation(java.lang.annotation.Annotation)

Example 4 with Validated

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

the class AbstractMessageConverterMethodArgumentResolver method validateIfApplicable.

/**
	 * Validate the binding target 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 binder the DataBinder to be used
	 * @param parameter the method parameter descriptor
	 * @since 4.1.5
	 * @see #isBindExceptionRequired
	 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation ann : annotations) {
        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 });
            binder.validate(validationHints);
            break;
        }
    }
}
Also used : Validated(org.springframework.validation.annotation.Validated) Annotation(java.lang.annotation.Annotation)

Aggregations

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