javax.validation验证值列表? [英] javax.validation to validate list of values?
问题描述
有没有办法使用javax.validation来验证一个名为color的字符串类型的变量,它需要使用注释只有这些值(红色,蓝色,绿色,粉红色)?
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?
我见过 @size(min = 1,max = 25)
和 @notnull
但是有这样的东西 @In(红色,蓝色,绿色,粉红色)
i have seen @size(min=1, max=25)
and @notnull
but is there something like this @In(red, blue, green, pink)
或多或少类似于 In-keyword
用于 mysql
推荐答案
在这种情况下,我认为使用@Pattern注释会更简单,就像下面的代码片段一样。如果你想要一个不区分大小写的评估,只需添加适当的标志:
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)
这篇关于javax.validation验证值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!