3

How to validate the length of elements inside List using javax.validation.constraints in Spring. Right now @Size is validating on the list size, not on the inside elements.

class RequestInputParamaters {

        @NotNull
        @NotEmpty
        @Size(min = 1, max=4)
        List documentIdentifier_value

    }
java
  • 33
  • 1
  • 1
  • 4

1 Answers1

8

Try:

List<@NotNull @NotEmpty @Size(min = 1, max=4) String> documentIdentifier_value;

If using hibernate-validator, you'll need version 6+.

Legacy solution:

@Valid List<StringWrapper> documentIdentifier_value;

where StringWrapper is defined as:

public class StringWrapper {
    @NotNull @NotEmpty @Size(min = 1, max=4)
    private String wrapped;
    ...
}
crizzis
  • 9,978
  • 2
  • 28
  • 47