Search code examples
hibernate-validator

How can the javax validator return a not null, but still empty, list of constraints?


I can see the javax validator returning a non null list of constraints. But that list contains 0 elements.

How can that be ?

It flies in the face of my test coverage.


Solution

  • Assuming you mean any of the various validate(), validateValue() etc. methods on Validator, returning an empty set of ConstraintViolation is the indicator that the requested validation was successful, i.e. no constraints were violated.

    Returning an empty collection instead of null is a common best practice to avoid NullPointerExceptions on the caller's side. Check isEmpty() on the returned set to find out whether the validation was successful or not.