use of cn.taketoday.validation.ObjectError in project today-infrastructure by TAKETODAY.
the class ValidationBindHandlerTests method bindShouldFailWithAccessToOrigin.
@Test
void bindShouldFailWithAccessToOrigin() {
this.sources.add(new MockConfigurationPropertySource("foo.age", 4, "file"));
BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleValidatedBean.class), this.handler));
ObjectError objectError = cause.getValidationErrors().getAllErrors().get(0);
assertThat(Origin.from(objectError).toString()).isEqualTo("file");
}
use of cn.taketoday.validation.ObjectError in project today-infrastructure by TAKETODAY.
the class MethodArgumentNotValidException method getMessage.
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder("Validation failed for argument [").append(this.parameter.getParameterIndex()).append("] in ").append(this.parameter.getExecutable().toGenericString());
BindingResult bindingResult = getBindingResult();
if (bindingResult.getErrorCount() > 1) {
sb.append(" with ").append(bindingResult.getErrorCount()).append(" errors");
}
sb.append(": ");
for (ObjectError error : bindingResult.getAllErrors()) {
sb.append('[').append(error).append("] ");
}
return sb.toString();
}
use of cn.taketoday.validation.ObjectError in project today-infrastructure by TAKETODAY.
the class ValidatorFactoryTests method testSpringValidationWithClassLevel.
@Test
public void testSpringValidationWithClassLevel() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Juergen");
person.getAddress().setStreet("Juergen's Street");
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertThat(result.getErrorCount()).isEqualTo(1);
ObjectError globalError = result.getGlobalError();
List<String> errorCodes = Arrays.asList(globalError.getCodes());
assertThat(errorCodes.size()).isEqualTo(2);
assertThat(errorCodes.contains("NameAddressValid.person")).isTrue();
assertThat(errorCodes.contains("NameAddressValid")).isTrue();
}
use of cn.taketoday.validation.ObjectError in project today-framework by TAKETODAY.
the class ValidationErrorsTests method getErrorsShouldAdaptFieldErrorsToBeOriginProviders.
@Test
void getErrorsShouldAdaptFieldErrorsToBeOriginProviders() {
Set<ConfigurationProperty> boundProperties = new LinkedHashSet<>();
ConfigurationPropertyName name1 = ConfigurationPropertyName.of("foo.bar");
Origin origin1 = MockOrigin.of("line1");
boundProperties.add(new ConfigurationProperty(name1, "boot", origin1));
ConfigurationPropertyName name2 = ConfigurationPropertyName.of("foo.baz.bar");
Origin origin2 = MockOrigin.of("line2");
boundProperties.add(new ConfigurationProperty(name2, "boot", origin2));
List<ObjectError> allErrors = new ArrayList<>();
allErrors.add(new FieldError("objectname", "bar", "message"));
ValidationErrors errors = new ValidationErrors(ConfigurationPropertyName.of("foo.baz"), boundProperties, allErrors);
assertThat(Origin.from(errors.getAllErrors().get(0))).isEqualTo(origin2);
}
use of cn.taketoday.validation.ObjectError in project today-framework by TAKETODAY.
the class ValidationErrorsTests method iteratorShouldIterateErrors.
@Test
void iteratorShouldIterateErrors() {
List<ObjectError> allErrors = new ArrayList<>();
allErrors.add(new ObjectError("foo", "bar"));
ValidationErrors errors = new ValidationErrors(NAME, Collections.emptySet(), allErrors);
assertThat(errors.iterator()).toIterable().containsExactlyElementsOf(allErrors);
}
Aggregations