Search in sources :

Example 1 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-boot by spring-projects.

the class YamlConfigurationFactory method validate.

private void validate() throws BindException {
    BindingResult errors = new BeanPropertyBindingResult(this.configuration, "configuration");
    this.validator.validate(this.configuration, errors);
    if (errors.hasErrors()) {
        logger.error("YAML configuration failed validation");
        for (ObjectError error : errors.getAllErrors()) {
            logger.error(getErrorMessage(error));
        }
        throw new BindException(errors);
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException)

Example 2 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult 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 3 with BeanPropertyBindingResult

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

the class PayloadArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
    Payload ann = parameter.getParameterAnnotation(Payload.class);
    if (ann != null && StringUtils.hasText(ann.expression())) {
        throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
    }
    Object payload = message.getPayload();
    if (isEmptyPayload(payload)) {
        if (ann == null || ann.required()) {
            String paramName = getParameterName(parameter);
            BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName);
            bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty"));
            throw new MethodArgumentNotValidException(message, parameter, bindingResult);
        } else {
            return null;
        }
    }
    Class<?> targetClass = parameter.getParameterType();
    Class<?> payloadClass = payload.getClass();
    if (ClassUtils.isAssignable(targetClass, payloadClass)) {
        validate(message, parameter, payload);
        return payload;
    } else {
        if (this.converter instanceof SmartMessageConverter) {
            SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
            payload = smartConverter.fromMessage(message, targetClass, parameter);
        } else {
            payload = this.converter.fromMessage(message, targetClass);
        }
        if (payload == null) {
            throw new MessageConversionException(message, "Cannot convert from [" + payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message);
        }
        validate(message, parameter, payload);
        return payload;
    }
}
Also used : SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ObjectError(org.springframework.validation.ObjectError) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Payload(org.springframework.messaging.handler.annotation.Payload)

Example 4 with BeanPropertyBindingResult

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

the class OptionTagTests method multiBind.

@Test
public void multiBind() throws Exception {
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(new TestBean(), "testBean");
    result.getPropertyAccessor().registerCustomEditor(TestBean.class, "friends", new FriendEditor());
    exposeBindingResult(result);
    BindStatus bindStatus = new BindStatus(getRequestContext(), "testBean.friends", false);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    this.tag.setValue(new TestBean("foo"));
    this.tag.doStartTag();
    this.tag.doEndTag();
    assertEquals("<option value=\"foo\">foo</option>", getOutput());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.tests.sample.beans.TestBean) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 5 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project entando-core by entando.

the class NoOpWidgetConfigurationValidator method validate.

@Override
public BeanPropertyBindingResult validate(WidgetConfigurationRequest widget, IPage page) {
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(widget, widget.getClass().getSimpleName());
    logger.warn("no WidgetConfigurationValidator implementation found for widget {} ", widget.getCode());
    return bindingResult;
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)142 Test (org.junit.jupiter.api.Test)71 RestServerError (org.entando.entando.aps.system.exception.RestServerError)25 Errors (org.springframework.validation.Errors)24 BindingResult (org.springframework.validation.BindingResult)21 StringReader (java.io.StringReader)20 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)18 List (java.util.List)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 ArrayList (java.util.ArrayList)12 RestRourceNotFoundException (org.entando.entando.aps.system.exception.RestRourceNotFoundException)12 FieldError (org.springframework.validation.FieldError)12 ValidationConflictException (org.entando.entando.web.common.exceptions.ValidationConflictException)11 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)11 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 Test (org.junit.Test)9