I am trying to use JSR-303 Bean Validation with Hibernate Validator to ensure that a collection does not contain null values.
I know that I can annotate my collection as follows:
@Valid
@NotEmpty
private Set<EmailAddress> emails = new HashSet<>();
This does the job for ensuring that the collection itself is not null or empty and in the case that I add a non-null EmailAddress element it also validates this correctly. However, it doesn't prevent adding a null element.
Is there a way to prevent a null element being added to the collection? In an ideal world the solution would be declarative (like the rest of the validation) and wouldn't involve programmatically iterating through the collection manually doing null checks.
Thanks in advance!