Search in sources :

Example 1 with ValidationBindHandler

use of org.springframework.boot.context.properties.bind.validation.ValidationBindHandler in project spring-boot by spring-projects.

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 SpringValidatorAdapter(Validation.byDefaultProvider().configure().buildValidatorFactory().getValidator());
    this.binder.bind("foo", Bindable.of(ResourceBean.class), new ValidationBindHandler(validator));
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) Validator(org.springframework.validation.Validator) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) PropertySource(org.springframework.core.env.PropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.jupiter.api.Test)

Example 2 with ValidationBindHandler

use of org.springframework.boot.context.properties.bind.validation.ValidationBindHandler in project spring-boot by spring-projects.

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(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) IgnoreErrorsBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler) BoundPropertiesTrackingBindHandler(org.springframework.boot.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) AbstractBindHandler(org.springframework.boot.context.properties.bind.AbstractBindHandler) Validator(org.springframework.validation.Validator) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)

Aggregations

ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)2 Validator (org.springframework.validation.Validator)2 Test (org.junit.jupiter.api.Test)1 AbstractBindHandler (org.springframework.boot.context.properties.bind.AbstractBindHandler)1 BindHandler (org.springframework.boot.context.properties.bind.BindHandler)1 BoundPropertiesTrackingBindHandler (org.springframework.boot.context.properties.bind.BoundPropertiesTrackingBindHandler)1 IgnoreErrorsBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler)1 IgnoreTopLevelConverterNotFoundBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)1 NoUnboundElementsBindHandler (org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler)1 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)1 MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)1 UnboundElementsSourceFilter (org.springframework.boot.context.properties.source.UnboundElementsSourceFilter)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1 SpringValidatorAdapter (org.springframework.validation.beanvalidation.SpringValidatorAdapter)1