java - JSR-303 类型 java 字段验证是否始终/隐式在字段验证之前运行?

标签 java validation object field bean-validation

如果我使用javax.validation.Validator来验证用约束注释的对象,它是否总是在对象之前评估字段级约束?

例如,如果我有:

@DummyClassValidation
public static class DummyClassToValidate {

    private Integer myNum;

    @Min(value = 50)
    @Max(value = 100)
    public Integer getMyNum() {
        return myNum;
    }

    public void setMyNum(Integer myNum) {
        this.myNum = myNum;
    }
}

我验证了它,是否保证 @DummyClassValidation 仅在 @Min@Max 之后进行评估?我知道我可以使用分组来做到这一点,但如果不需要的话我宁愿不这样做(即字段验证隐式分组以在对象验证之前进行验证)。

最佳答案

the specification ,第 3.5 节:

For a given group to validate, the validation routine applied on a given bean instance is expected to execute the following constraint validations in no particular order:

  • for all reachable fields, execute all field level validations (including the ones expressed on superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

  • for all reachable getters, execute all getter level validations (including the ones expressed on interfaces and superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

  • execute all class level validations (including the ones expressed on interfaces and superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

  • for all reachable and cascadable associations, execute all cascading validations (see Section 3.5.1) including the ones expressed on interfaces and superclasses (see Section 3.4.5)

简而言之,除非您想找到一个能保证它的实现并坚持下去,否则您不能依赖它。

关于java - JSR-303 类型 java 字段验证是否始终/隐式在字段验证之前运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800512/

相关文章:

django - 如何从 django 中的左表数据中删除验证

java - 由于 JLabel 首选大小,GridBagLayout 的列大小不相等

java - Android更改错误验证文本字段的颜色

javascript - 获取 javascript 来警告用户此正则表达式测试

javascript - 根据 Javascript 中的另一个对象过滤对象数组

javascript - 使用 lodash 获取嵌套对象属性

c++ - 如何禁止在 C++ 中使用默认构造函数?

Java异常crshes玩法框架

Java 函数签名接受单个枚举值

java - 让 Spring Boot 重新创建测试数据库