Search in sources :

Example 1 with SpringValidatorAdapter

use of org.springframework.validation.beanvalidation.SpringValidatorAdapter in project spring-boot by spring-projects.

the class YamlConfigurationFactoryTests method missingPropertyCausesValidationError.

@Test(expected = BindException.class)
public void missingPropertyCausesValidationError() throws Exception {
    this.validator = new SpringValidatorAdapter(Validation.buildDefaultValidatorFactory().getValidator());
    createFoo("bar: blah");
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) Test(org.junit.Test)

Example 2 with SpringValidatorAdapter

use of org.springframework.validation.beanvalidation.SpringValidatorAdapter 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 3 with SpringValidatorAdapter

use of org.springframework.validation.beanvalidation.SpringValidatorAdapter in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method validationJsr303ValidatorExposedAsSpringValidator.

@Test
public void validationJsr303ValidatorExposedAsSpringValidator() {
    load(Jsr303Validator.class);
    assertThat(this.context.getBeansOfType(ValidatorFactory.class)).isEmpty();
    assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).hasSize(1);
    assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
    Validator validator = this.context.getBean(Validator.class);
    assertThat(validator).isInstanceOf(SpringValidator.class);
    SpringValidatorAdapter target = ((SpringValidator) validator).getTarget();
    assertThat(new DirectFieldAccessor(target).getPropertyValue("targetValidator")).isSameAs(this.context.getBean(javax.validation.Validator.class));
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 4 with SpringValidatorAdapter

use of org.springframework.validation.beanvalidation.SpringValidatorAdapter in project spring-boot by spring-projects.

the class WebFluxAnnotationAutoConfigurationTests method validationJsr303ValidatorExposedAsSpringValidator.

@Test
public void validationJsr303ValidatorExposedAsSpringValidator() {
    load(Jsr303Validator.class);
    assertThat(this.context.getBeansOfType(ValidatorFactory.class)).isEmpty();
    assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).hasSize(1);
    assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
    Validator validator = this.context.getBean(Validator.class);
    assertThat(validator).isInstanceOf(SpringValidator.class);
    SpringValidatorAdapter target = ((SpringValidator) validator).getTarget();
    assertThat(new DirectFieldAccessor(target).getPropertyValue("targetValidator")).isSameAs(this.context.getBean(javax.validation.Validator.class));
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 5 with SpringValidatorAdapter

use of org.springframework.validation.beanvalidation.SpringValidatorAdapter in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryTests method testMissingPropertyCausesValidationError.

@Test(expected = BindException.class)
public void testMissingPropertyCausesValidationError() throws Exception {
    this.validator = new SpringValidatorAdapter(Validation.buildDefaultValidatorFactory().getValidator());
    createFoo("bar: blah");
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) Test(org.junit.Test)

Aggregations

SpringValidatorAdapter (org.springframework.validation.beanvalidation.SpringValidatorAdapter)6 Test (org.junit.Test)4 Validator (org.springframework.validation.Validator)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 SpringValidator (org.springframework.boot.autoconfigure.validation.SpringValidator)2 ByteBufferBackedInputStream (com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)1 Test (org.junit.jupiter.api.Test)1 ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)1 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)1 MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)1 MethodParameter (org.springframework.core.MethodParameter)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1