-
Notifications
You must be signed in to change notification settings - Fork 721
Closed
Labels
Description
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()?