Nawa
Nawa

Reputation: 2158

Check that each element in collection is not empty

How to simply check that each element in collection of strings is not empty using some annotation?

List<String> strings;

Upvotes: 0

Views: 408

Answers (1)

Hardy
Hardy

Reputation: 19109

There is no simple way to achieve this with Bean Validation / Hibernate Validator.

You are dealing with the same issue discussed in the Validator HV-296 as well the Bean Validation BVAL-202 issue tracker.

One thing you could try is to implement a

public class NotEmptyValidator implements ConstraintValidator<NotEmpty, Iterable<String>>

You then register this custom constraint validator via validation.xml. However, this is more than just adding an annotation.

Hibernate Validator 5.2 will offer support for Java 8 type annotations. There you will be able to write:

List<@NotEmpty String> strings;

See also HV-877.

Upvotes: 2

Related Questions