use of cn.taketoday.validation.Validator in project today-infrastructure by TAKETODAY.
the class ValidationBindHandlerTests method validateMapValues.
@Test
void validateMapValues() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("test.items.[itemOne].number", "one");
source.put("test.items.[ITEM2].number", "two");
this.sources.add(source);
Validator validator = getMapValidator();
this.handler = new ValidationBindHandler(validator);
this.binder.bind(ConfigurationPropertyName.of("test"), Bindable.of(ExampleWithMap.class), this.handler);
}
use of cn.taketoday.validation.Validator in project today-infrastructure by TAKETODAY.
the class ValidationBindHandlerTests method validateMapValuesWithNonUniformSource.
@Test
void validateMapValuesWithNonUniformSource() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("test.items.itemOne.number", "one");
map.put("test.items.ITEM2.number", "two");
this.sources.add(ConfigurationPropertySources.from(new MapPropertySource("test", map)).iterator().next());
Validator validator = getMapValidator();
this.handler = new ValidationBindHandler(validator);
this.binder.bind(ConfigurationPropertyName.of("test"), Bindable.of(ExampleWithMap.class), this.handler);
}
use of cn.taketoday.validation.Validator in project today-infrastructure by TAKETODAY.
the class BinderTests method bindToValidatedBeanWithResourceAndNonEnumerablePropertySource.
@Test
void bindToValidatedBeanWithResourceAndNonEnumerablePropertySource() {
ConfigurationPropertySources.from(new PropertySource<String>("test") {
@Override
public Object getProperty(String name) {
return null;
}
}).forEach(this.sources::add);
Validator validator = new ValidatorAdapter(Validation.byDefaultProvider().configure().buildValidatorFactory().getValidator());
this.binder.bind("foo", Bindable.of(ResourceBean.class), new ValidationBindHandler(validator));
}
use of cn.taketoday.validation.Validator in project today-framework by TAKETODAY.
the class DataBinderTests method testValidatorWithNestedObjectNull.
@Test
void testValidatorWithNestedObjectNull() {
TestBean tb = new TestBean();
Errors errors = new BeanPropertyBindingResult(tb, "tb");
Validator testValidator = new TestBeanValidator();
testValidator.validate(tb, errors);
errors.setNestedPath("spouse.");
assertThat(errors.getNestedPath()).isEqualTo("spouse.");
Validator spouseValidator = new SpouseValidator();
spouseValidator.validate(tb.getSpouse(), errors);
errors.setNestedPath("");
assertThat(errors.hasFieldErrors("spouse")).isTrue();
assertThat(errors.getFieldErrorCount("spouse")).isEqualTo(1);
assertThat(errors.getFieldError("spouse").getCode()).isEqualTo("SPOUSE_NOT_AVAILABLE");
assertThat((errors.getFieldErrors("spouse").get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getFieldErrors("spouse").get(0)).getRejectedValue()).isNull();
}
use of cn.taketoday.validation.Validator in project today-framework by TAKETODAY.
the class DataBinderTests method testNestedValidatorWithoutNestedPath.
@Test
void testNestedValidatorWithoutNestedPath() {
TestBean tb = new TestBean();
tb.setName("XXX");
Errors errors = new BeanPropertyBindingResult(tb, "tb");
Validator spouseValidator = new SpouseValidator();
spouseValidator.validate(tb, errors);
assertThat(errors.hasGlobalErrors()).isTrue();
assertThat(errors.getGlobalErrorCount()).isEqualTo(1);
assertThat(errors.getGlobalError().getCode()).isEqualTo("SPOUSE_NOT_AVAILABLE");
assertThat((errors.getGlobalErrors().get(0)).getObjectName()).isEqualTo("tb");
}
Aggregations