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);
}
}
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;
}
}
}
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;
}
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SpringValidatorAdapterTests method testNoStringArgumentValue.
// SPR-13406
@Test
public void testNoStringArgumentValue() {
TestBean testBean = new TestBean();
testBean.setPassword("pass");
testBean.setConfirmPassword("pass");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("password"), is(1));
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), is("Size of Password is must be between 8 and 128"));
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SpringValidatorAdapterTests method testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName.
// SPR-13406
@Test
public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() {
TestBean testBean = new TestBean();
testBean.setPassword("password");
testBean.setConfirmPassword("PASSWORD");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("password"), is(1));
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), is("Password must be same value with Password(Confirm)"));
}
Aggregations