Hibernate 验证器中字段的短路约束

发布于 2024-12-10 05:47:45 字数 1592 浏览 0 评论 0原文

我希望对类字段的约束进行排序和短路,例如

@Size(min = 2, max = 10, message = "Name length improper")
@Pattern(regexp = "T.*", message = "Name doesn't start with T")
private String name;

使用 name="S" ,应该使 @Size 约束失败,因此甚至不用打扰检查下一个。我浏览了 Groups、GroupSequence 和 Composite Constraints,但似乎没有什么用处。具体来说,GroupSequence 不适用于我的情况。考虑一下:

public class Bean {

  public interface First{}
  public interface Second {}

  @GroupSequence({First.class, Second.class})
  public interface Sequence {}

  public Bean(String name, int noOfDependants) {
  ...
  }

  @Size(min = 2, max = 10, groups = {First.class})
  @Pattern(regexp = "T.*", groups = {Second.class})
  private String name;

  @Min(value = 0, groups = {First.class})
  @Max(value = 4, groups = {Second.class})
  private int noOfDependants;
}
validator.validate(new Bean("S", 5), Sequence.class)

我预计对 name 的第一个约束和对 noOfDependants 的第二个约束会失败。但按照 GroupSequence 的工作方式,First.class 组将会失败,Second.class 甚至不会被执行。

最后,我决定像这样编写自己的约束:

@LazySequence({
    @Size(min = 2, max = 10, message = "Name length improper"),
    @Pattern(regexp = "T.*", message = "Name doesn't start with T")
})
private String name;

并遇到熟悉的问题 Annotation member which持有其他注释?

public @interface LazySequence {
    ???[] values();
    String message() default "";
    ...
 }

有人遇到过这个用例吗?

谢谢

I want the constraints on fields of a class to be ordered and short-circuit, e.g.

@Size(min = 2, max = 10, message = "Name length improper")
@Pattern(regexp = "T.*", message = "Name doesn't start with T")
private String name;

with name="S", should fail the @Size constraint and hence not even bother checking the next one. I went through Groups, GroupSequence and Composite Constraints but nothing seems to be of use. Specifically, GroupSequence will not work for my case. Consider this:

public class Bean {

  public interface First{}
  public interface Second {}

  @GroupSequence({First.class, Second.class})
  public interface Sequence {}

  public Bean(String name, int noOfDependants) {
  ...
  }

  @Size(min = 2, max = 10, groups = {First.class})
  @Pattern(regexp = "T.*", groups = {Second.class})
  private String name;

  @Min(value = 0, groups = {First.class})
  @Max(value = 4, groups = {Second.class})
  private int noOfDependants;
}
validator.validate(new Bean("S", 5), Sequence.class)

I expect the first constraint on name and second constraint on noOfDependants to fail. But the way GroupSequence works, the First.class group would fail and Second.class wouldn't even be executed.

Finally, I decided to write my own constraint like so:

@LazySequence({
    @Size(min = 2, max = 10, message = "Name length improper"),
    @Pattern(regexp = "T.*", message = "Name doesn't start with T")
})
private String name;

and hit the familiar problem Annotation member which holds other annotations?

public @interface LazySequence {
    ???[] values();
    String message() default "";
    ...
 }

Has anyone encountered this use case?

Thx

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

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

发布评论

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

评论(1

回忆那么伤 2024-12-17 05:47:45

正如您链接的问题中所述,只能有具体注释类型的注释成员,因此您无法以预期的方式实现 @LazySequence

我不太确定你所说的“短路”是什么意思,但根据你的描述,我认为使用组序列仍然应该有效:

public class Bean {

    public interface First {}

    public interface Second {}

    @GroupSequence({First.class, Second.class})
    public interface Sequence {}

    @Size(min = 2, max = 10, message = "Name length improper", groups = { First.class })
    @Pattern(regexp = "T.*", message = "Name doesn't start with T" , groups = { Second.class })
    private String name;

}

当现在使用定义的序列验证 Bean 实例时(validator.validate(bean, Sequence.class)),首先,@Size 约束将被验证,并且仅当它成功于 @Pattern 约束时。

您可以在 Bean 验证 规范 和 Hibernate 中了解有关验证组和组序列的更多信息验证器参考指南

As outlined in the question you linked, there can only be annotation members of a concrete annotation type, so there is no way you could implement @LazySequence in the intended way.

I'm not exactly sure what you mean by "short-circuit", but based on your description I think using group sequences still should work:

public class Bean {

    public interface First {}

    public interface Second {}

    @GroupSequence({First.class, Second.class})
    public interface Sequence {}

    @Size(min = 2, max = 10, message = "Name length improper", groups = { First.class })
    @Pattern(regexp = "T.*", message = "Name doesn't start with T" , groups = { Second.class })
    private String name;

}

When now validating a Bean instance using the defined sequence (validator.validate(bean, Sequence.class)), at first the @Size constraint will be validated and only if that succeeds the @Pattern constraint.

You can learn more about validation groups and group sequences in the Bean Validation specification and the Hibernate Validator reference guide.

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