使用 Java Bean 验证验证字符串数组的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32069126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:35:03  来源:igfitidea点击:

Validate elements of a String array with Java Bean Validation

javabean-validationindexed-properties

提问by Ironluca

I have a simple class that has one of its properties as a String array. As per thisdocument, using @Valid on an array, collection etc. will recursively validate each element of the array/collection.

我有一个简单的类,它的一个属性是字符串数组。根据文档,在数组、集合等上使用 @Valid 将递归验证数组/集合的每个元素。

@Valid
@Pattern(regexp="^[_ A-Za-z0-9]+$")
public String[] defaultAppAdminRoles;

the above annotation on the property generates the following exception:

属性上的上述注释生成以下异常:

Exception in thread "main" javax.validation.UnexpectedTypeException: No validator could be found for type java.lang.String[]. See: @Pattern at public java.lang.String[] com.hm.vigil.platform.ops.model.Application.defaultAppAdminRoles
at org.apache.bval.jsr303.AnnotationProcessor.checkOneType(AnnotationProcessor.java:326)
at org.apache.bval.jsr303.AnnotationProcessor.getConstraintValidator(AnnotationProcessor.java:301)
at org.apache.bval.jsr303.AnnotationProcessor.applyConstraint(AnnotationProcessor.java:241)
at org.apache.bval.jsr303.AnnotationProcessor.processAnnotation(AnnotationProcessor.java:149)
at org.apache.bval.jsr303.AnnotationProcessor.processAnnotations(AnnotationProcessor.java:90)
at org.apache.bval.jsr303.Jsr303MetaBeanFactory.processClass(Jsr303MetaBeanFactory.java:134)
at org.apache.bval.jsr303.Jsr303MetaBeanFactory.buildMetaBean(Jsr303MetaBeanFactory.java:95)
at org.apache.bval.MetaBeanBuilder.buildForClass(MetaBeanBuilder.java:131)
at org.apache.bval.MetaBeanManager.findForClass(MetaBeanManager.java:102)
at org.apache.bval.jsr303.ClassValidator.validate(ClassValidator.java:140)
at com.hm.vigil.platform.commons.AbstractValidatable.isValid(AbstractValidatable.java:33)
at com.hm.vigil.platform.ops.model.Application.main(Application.java:54)

I am using Apache BVal as validation provider.

我使用 Apache BVal 作为验证提供程序。

The question, is the above method correct?

问题是,上面的方法对吗?

If it is not correct, what is the correct way to validate an array/collection with bean validation?

如果不正确,使用 bean 验证来验证数组/集合的正确方法是什么?

If it is correct, then is it some limitation of Apache BVal?

如果它是正确的,那么它是 Apache BVal 的一些限制吗?

采纳答案by Tunaki

By adding the @Validannotation like you've done, the validation algorithm is applied on each element (validation of the element constraints).

通过@Valid像您所做的那样添加注释,验证算法将应用于每个元素(元素约束的验证)。

In your case the String class has no constraints. The @Patternconstraint you've added is applied to the array and not on each element of it. Since @Patternconstraint cannot be applied on a array, you are getting an error message.

在您的情况下, String 类没有约束。@Pattern您添加的约束应用于数组,而不是应用于数组的每个元素。由于@Pattern无法对数组应用约束,因此您会收到一条错误消息。

You can create a custom validation constraint for your array (see Hibernate docsfor more info) or you can use a wrapper class like @Jordi Castilla mentioned.

您可以为数组创建自定义验证约束(有关更多信息,请参阅Hibernate 文档),或者您可以使用像 @Jordi Castilla 提到的包装类。

回答by Franck

Another thing worth mentioning is the introduction of type annotation in Java 8 which lets you annotate parameterized type

另一件值得一提的事情是 Java 8 中引入了类型注释,它可以让您注释参数化类型

private List<@MyPattern String> defaultAppAdminRoles;

It's not yet in the bean-validation standard (surely in next version) but already available in hibernate-validator 5.2.1. Blog entryhere for further information.

它尚未包含在 bean 验证标准中(肯定在下一版本中),但已在 hibernate-validator 5.2.1 中可用。此处的博客条目以获取更多信息。

回答by Jordi Castilla

First... i'm not sure... but @Patternonly accepts regex, right? Correct sintax is not:

首先...我不确定...但@Pattern只接受regex,对吗?正确的语法不是:

@Pattern("^[_ A-Za-z0-9]+$")   // delete 'regexp='

If this is not the problem you can create a wrapper classwith validators in the attributes:

如果这不是问题,您可以在属性中创建一个带有验证器的包装类

public class Role {

    @Pattern(regexp="^[_ A-Za-z0-9]+$")
    String adminRole;

    //getters and setters
}

Then just need to mark the field @Validin your existing object:

然后只需要标记@Valid现有对象中的字段:

@Valid
Role[] defaultAppAdminRoles;