Nutan
Nutan

Reputation: 1375

Can List<String> values be validated using JSR 303 field validations

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;

Upvotes: 2

Views: 792

Answers (1)

Alexander Pranko
Alexander Pranko

Reputation: 1959

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>

Upvotes: 3

Related Questions