Cascading validation on list items is not invoked
Description
Trying to validate a POJO with nested collection that requires item validation
class TestTopLevel {
public List<@Valid TestDetails> data = new ArrayList<>();
}
class TestDetails {
@NotEmpty
public String large = "";
}
TestTopLevel topLevelValue = new TestTopLevel();
TestDetails details = new TestDetails();
topLevelValue.data = new ArrayList<>();
topLevelValue.data.add(details);
Both validateProperty and validateValue fail to validate the list items.
violations = validator.validateProperty(topLevelValue, "data");
violations = validator.validateValue(TestTopLevel.class, "data", topLevelValue);
It appears that the issue is in the validationContext.appliesTo( metaConstraint ) call.
metaConstraint is correctly pointing to the "large" property of the item in the list when it gets there.
However "validatedProperty" value at this point is "data" from the top level class.
@Override
public boolean appliesTo(MetaConstraint<?> metaConstraint) {
return Objects.equals( validatedProperty, getPropertyName( metaConstraint.getLocation() ) );
}
private boolean validateMetaConstraint(BaseBeanValidationContext<?> validationContext, ValueContext<?, Object> valueContext, Object parent, MetaConstraint<?> metaConstraint) {
BeanValueContext.ValueState<Object> originalValueState = valueContext.getCurrentValueState();
valueContext.appendNode( metaConstraint.getLocation() );
boolean success = true;
if ( isValidationRequired( validationContext, valueContext, metaConstraint ) ) {
The equals check fails and the validation of the item is skipped because isValidationRequired returns false.
Environment
Activity
Ah OK, you're using validateValue(). Then it's normal.
As stated in the documentation:
@Valid is not honored by validateProperty() or validateValue().
Â
We have tons of tests checking that so if you think there's an issue, please create a small reproducer.
You can use: https://github.com/hibernate/hibernate-test-case-templates/tree/master/validator to do so.
Thanks!