JSR303 验证:两个(或多个)验证约束:第一个返回错误后,不评估第二个?

发布于 2024-12-29 20:22:32 字数 232 浏览 5 评论 0 原文

给定以下两个约束

@NotNull
@Pattern (pattern="foobar")
private String myFooBarMember;

,如果第一个约束(=返回错误)(@NotNull)是否有可能不评估第二个约束(@Pattern)?或者 JSR303 总是评估所有约束?如果有可能在第一场比赛后不评估第二场比赛,请说明如何做到这一点。

非常感谢!

given the following two Constraints

@NotNull
@Pattern (pattern="foobar")
private String myFooBarMember;

Is there a possibility, if the first one matched (=returned an error) (@NotNull) to not evaluate the second one (@Pattern)? or does JSR303 always evaluate all Constraints? If there is a possibility to not evaluate the second after a first match, please state how this can be done.

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

破晓 2025-01-05 20:22:32

好吧,我不是 JSR 303 专家,但我从 1.0 FR

2.3。约束组合

注意:

如果组合约束失败并且组合约束是
标记为@ReportAsSingleViolation,Bean验证提供者是
可以自由地不处理该组合的其他组合约束
约束。

和:

3.5。验证例程

  • 对于所有可到达的字段,执行与目标匹配的所有字段级别验证(包括超类中表达的验证)
    组,除非给定的验证约束已经被
    在此验证例程期间针对给定导航路径进行处理
    (参见第 3.5.1 节)作为之前小组比赛的一部分。

因此,从后者来看,我想说默认情况下所有验证器都会被执行。

从前者来看,我想说这(约束组合)将是实现你想要的目标的一种方法。尽管它可能(而且很可能)依赖于 Bean Validation 实现者,所以您需要阅读一些专有文档。

Well, I'm not a JSR 303 specialist, but I've found the following excerpts from the 1.0 FR:

2.3. Constraint composition

Note:

If a composing constraint fails and if the composed constraint is
marked as @ReportAsSingleViolation, the Bean Validation provider is
free to not process the other composing constraints for this composed
constraint.

and:

3.5. Validation routine

  • 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.

So from the latter, I'd say that by default all validators will be executed.

From the former, I'd say that this (constraint composition) would be one way to achieve what you want. Although it might (and probably is) dependent on the Bean Validation implementor, so you'd need to read some proprietary docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文