javaspringvalidationjavax

javax.validation to validate list of values?


is there a way to use javax.validation to validate a variable of type string called colour that needs to have these values only(red, blue, green, pink) using annotations?

i have seen @size(min=1, max=25) and @notnull but is there something like this @In(red, blue, green, pink)

more or less similar to the In-keyword used in mysql


Solution

  • In that case I think it would be simpler to use the @Pattern annotation, like the snippet below. If you want a case insensitive evaluation, just add the appropriate flag:

    @Pattern(regexp = "red|blue|green|pink", flags = Pattern.Flag.CASE_INSENSITIVE)