休眠验证器中字段的短路约束 [英] Short circuiting constraints on a field in hibernate validator

查看:44
本文介绍了休眠验证器中字段的短路约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

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

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)

我希望 name 上的第一个约束和 noOfDependants 上的第二个约束失败.但是 GroupSequence 的工作方式,First.class 组会失败并且 Second.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;

遇到熟悉的问题包含其他注释的注释成员?

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

有人遇到过这个用例吗?

Has anyone encountered this use case?

谢谢

推荐答案

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

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;

}

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

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.

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

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

这篇关于休眠验证器中字段的短路约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆