Search in sources :

Example 16 with ObjectError

use of cn.taketoday.validation.ObjectError in project today-framework 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");
}
Also used : ObjectError(cn.taketoday.validation.ObjectError) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 17 with ObjectError

use of cn.taketoday.validation.ObjectError in project today-framework by TAKETODAY.

the class ValidationErrorsTests method getErrorsShouldReturnErrors.

@Test
void getErrorsShouldReturnErrors() {
    List<ObjectError> allErrors = new ArrayList<>();
    allErrors.add(new ObjectError("foo", "bar"));
    ValidationErrors errors = new ValidationErrors(NAME, Collections.emptySet(), allErrors);
    assertThat(errors.getAllErrors()).isEqualTo(allErrors);
}
Also used : ObjectError(cn.taketoday.validation.ObjectError) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 18 with ObjectError

use of cn.taketoday.validation.ObjectError in project today-framework 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();
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) ObjectError(cn.taketoday.validation.ObjectError) Test(org.junit.jupiter.api.Test)

Example 19 with ObjectError

use of cn.taketoday.validation.ObjectError in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSpringValidationWithAutowiredValidator.

@Test
public void testSpringValidationWithAutowiredValidator() {
    ConfigurableApplicationContext ctx = new StandardApplicationContext(LocalValidatorFactoryBean.class);
    LocalValidatorFactoryBean validator = ctx.getBean(LocalValidatorFactoryBean.class);
    ValidPerson person = new ValidPerson();
    person.expectsAutowiredValidator = true;
    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();
    ctx.close();
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) ObjectError(cn.taketoday.validation.ObjectError) Test(org.junit.jupiter.api.Test)

Example 20 with ObjectError

use of cn.taketoday.validation.ObjectError in project today-infrastructure by TAKETODAY.

the class DefaultErrorAttributesTests method withMethodArgumentNotValidExceptionBindingErrors.

@Test
void withMethodArgumentNotValidExceptionBindingErrors() {
    Method method = ReflectionUtils.findMethod(String.class, "substring", int.class);
    MethodParameter parameter = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new MethodArgumentNotValidException(parameter, bindingResult);
    testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) MapBindingResult(cn.taketoday.validation.MapBindingResult) ObjectError(cn.taketoday.validation.ObjectError) MapBindingResult(cn.taketoday.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(cn.taketoday.core.MethodParameter) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectError (cn.taketoday.validation.ObjectError)22 Test (org.junit.jupiter.api.Test)18 BindingResult (cn.taketoday.validation.BindingResult)8 BindException (cn.taketoday.validation.BindException)6 MapBindingResult (cn.taketoday.validation.MapBindingResult)6 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)6 ServletException (jakarta.servlet.ServletException)6 ArrayList (java.util.ArrayList)6 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)4 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)2 BindException (cn.taketoday.context.properties.bind.BindException)2 BindValidationException (cn.taketoday.context.properties.bind.validation.BindValidationException)2 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)2 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)2 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)2 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)2 MethodParameter (cn.taketoday.core.MethodParameter)2 MockOrigin (cn.taketoday.origin.MockOrigin)2 Origin (cn.taketoday.origin.Origin)2 FieldError (cn.taketoday.validation.FieldError)2