Search in sources :

Example 1 with Validator

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);
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Validator(cn.taketoday.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 2 with Validator

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);
}
Also used : MapPropertySource(cn.taketoday.core.env.MapPropertySource) Validator(cn.taketoday.validation.Validator) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 3 with Validator

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));
}
Also used : ValidatorAdapter(cn.taketoday.validation.beanvalidation.ValidatorAdapter) Validator(cn.taketoday.validation.Validator) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler) MapPropertySource(cn.taketoday.core.env.MapPropertySource) PropertySource(cn.taketoday.core.env.PropertySource) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 4 with 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();
}
Also used : Errors(cn.taketoday.validation.Errors) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Validator(cn.taketoday.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 5 with Validator

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");
}
Also used : Errors(cn.taketoday.validation.Errors) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Validator(cn.taketoday.validation.Validator) Test(org.junit.jupiter.api.Test)

Aggregations

Validator (cn.taketoday.validation.Validator)13 Test (org.junit.jupiter.api.Test)11 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)5 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)5 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)5 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)5 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)4 MapPropertySource (cn.taketoday.core.env.MapPropertySource)4 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)4 Errors (cn.taketoday.validation.Errors)4 ValidationBindHandler (cn.taketoday.context.properties.bind.validation.ValidationBindHandler)3 ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)2 PropertySource (cn.taketoday.core.env.PropertySource)2 ValidatorAdapter (cn.taketoday.validation.beanvalidation.ValidatorAdapter)2 LinkedHashMap (java.util.LinkedHashMap)2 PropertyValues (cn.taketoday.beans.PropertyValues)1 AbstractBindHandler (cn.taketoday.context.properties.bind.AbstractBindHandler)1 BindHandler (cn.taketoday.context.properties.bind.BindHandler)1 BoundPropertiesTrackingBindHandler (cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler)1 IgnoreErrorsBindHandler (cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler)1