I want to validate a JSON array against JSR 303 annotations with Hibernate Validator. While the validation works for JSON object and properties of array type (with @Valid), the validation is skipped for elements of a top-level JSON array.
For example:
public class ValidationTest {
public static void main(String[] args) throws IOException {
ObjectMapper m = new ObjectMapper();
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
System.out.println(validator.validate(m.readValue("{}", Person.class)));
System.out.println(validator.validate(m.readValue("[{}]", Person[].class)));
System.out.println(validator.validate(m.readValue("{\"array\":[{}]}", PersonArray.class)));
}
}
class Person {
@NotNull
private String name;
public String getName() {
return name;
}
public Person setName(String name) {
this.name = name;
return this;
}
}
class PersonArray {
@Valid
private Person[] array;
public Person[] getArray() {
return array;
}
public PersonArray setArray(Person[] array) {
this.array = array;
return this;
}
}
Output:
[ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=name, rootBeanClass=class com.radius.Person, messageTemplate='{javax.validation.constraints.NotNull.message}'}]
[]
[ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=array[0].name, rootBeanClass=class com.radius.PersonArray, messageTemplate='{javax.validation.constraints.NotNull.message}'}]
As you can see, the required name property is validated for Person and recursively for array inside PersonArray, but not for Person[]. Is there a way to have recursive validation for a top-level JSON array?