1

I am validating a bean with JSR 303 specifications, it has a parameter that contains list of string. I want to validate each string in this list as this is the request object. I already have a pattern for matching each string, but i am not able to apply if recursively for each entry in the list.

@NotEmpty(message = "phoneNumber must not be null or empty")
@Valid @Pattern(regexp = REGEX_PHONENUMBER, message = "Not a valid phoneNumber")
private List<String> phoneNumber;
Nutan
  • 1,287
  • 11
  • 15
  • Yes you can validate `List` but you need to create custom validator. Please see https://stackoverflow.com/questions/39504205/spring-validate-list-of-strings-for-non-empty-elements – Sudhir Ojha Jan 21 '19 at 12:57

1 Answers1

3

A custom validator need to be implemented to make it possible using JSR 303

@PhoneNumbers
private List<String> phoneNumber;

It's supported out of the box in Bean Validation 2.0 / JSR 380:

List<@NotEmpty @Pattern(regexp = REGEX_PHONENUMBER) String>
Alexander Pranko
  • 1,859
  • 17
  • 20
  • 1
    Thanks used a custom validator, could not try second method as it is not supported by project. – Nutan Jan 22 '19 at 08:18