Search code examples
javaspring-boothibernate-validator

Hibernate length validator for List<String>


For the address field, instead of creating 3 parameters

String addr1;
String addr2;
String addr3;

I have listed it as,

List<String> addressLines;

Now the issue is how I can add min, max length of string fields. If it's one at a time, I can add @Max(40)

One way I can think is by adding String to a class, and make a list of that class and there validate. But, it's just a workaround.


Solution

  • You can annotate the type parameter:

    List<@Max(40) String> addressLines;