Search in sources :

Example 1 with ValidationBindHandler

use of cn.taketoday.context.properties.bind.validation.ValidationBindHandler 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 2 with ValidationBindHandler

use of cn.taketoday.context.properties.bind.validation.ValidationBindHandler in project today-framework by TAKETODAY.

the class ConfigurationPropertiesBinder method getBindHandler.

private <T> BindHandler getBindHandler(Bindable<T> target, ConfigurationProperties annotation) {
    List<Validator> validators = getValidators(target);
    BindHandler handler = getHandler();
    handler = new ConfigurationPropertiesBindHandler(handler);
    if (annotation.ignoreInvalidFields()) {
        handler = new IgnoreErrorsBindHandler(handler);
    }
    if (!annotation.ignoreUnknownFields()) {
        UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
        handler = new NoUnboundElementsBindHandler(handler, filter);
    }
    if (!validators.isEmpty()) {
        handler = new ValidationBindHandler(handler, validators.toArray(new Validator[0]));
    }
    for (ConfigurationPropertiesBindHandlerAdvisor advisor : getBindHandlerAdvisors()) {
        handler = advisor.apply(handler);
    }
    return handler;
}
Also used : IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler) UnboundElementsSourceFilter(cn.taketoday.context.properties.source.UnboundElementsSourceFilter) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler) BoundPropertiesTrackingBindHandler(cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(cn.taketoday.context.properties.bind.BindHandler) IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) AbstractBindHandler(cn.taketoday.context.properties.bind.AbstractBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler) Validator(cn.taketoday.validation.Validator) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler)

Example 3 with ValidationBindHandler

use of cn.taketoday.context.properties.bind.validation.ValidationBindHandler in project today-framework 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)

Aggregations

ValidationBindHandler (cn.taketoday.context.properties.bind.validation.ValidationBindHandler)3 Validator (cn.taketoday.validation.Validator)3 ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)2 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)2 MapPropertySource (cn.taketoday.core.env.MapPropertySource)2 PropertySource (cn.taketoday.core.env.PropertySource)2 ValidatorAdapter (cn.taketoday.validation.beanvalidation.ValidatorAdapter)2 Test (org.junit.jupiter.api.Test)2 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 IgnoreTopLevelConverterNotFoundBindHandler (cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)1 NoUnboundElementsBindHandler (cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler)1 UnboundElementsSourceFilter (cn.taketoday.context.properties.source.UnboundElementsSourceFilter)1