Skip to content

BeanValidationBinder / BeanValidator / getJavaxBeanValidator().validateValue() does not honor @Valid #9520

@mlarue

Description

@mlarue

Can't get my @Valid constraints working with binder.writeBean()

public class Person {
    
    @Valid
    private Address address;

}

public class Address {
    
    @NotNull
    private String city;

}

Found this code in BeanValidator:

    @Override
    public ValidationResult apply(final Object value, ValueContext context) {
        Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator()
                .validateValue(beanType, propertyName, value);

Hibernate Validation docs state:

@Valid is not honored by validateProperty() or validateValue().

Here is a simple unit test with the plain validation api:

        Person person = new Person();
        Address address = new Address();
        person.setAddress(address);
        
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        // this would work
        //Set<ConstraintViolation<Depot>> constraintViolations = validator.validate(person);

        Set<ConstraintViolation<Depot>> constraintViolations = validator.validateValue(Depot.class, "address", depot.getAddress());
        
        constraintViolations.forEach((cv) -> {
            System.err.println(cv.getPropertyPath() + " " + cv.getMessage());
        });

How to deal with it?
Is it a bug or a necessity to use validateValue() in favour of validate()?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions