休眠验证器中某个字段的短路约束 [英] Short circuiting constraints on a field in hibernate validator
问题描述
我希望对类的字段进行约束并使其短路,例如
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
约束,因此甚至不必费心检查下一个约束.我经历了组,GroupSequence
和复合约束,但似乎没有用.具体来说,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?
Thx
推荐答案
如您所链接的问题所述,只能有具体注释类型的注释成员,因此您无法在预期的方式.
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 Validator 参考指南.
You can learn more about validation groups and group sequences in the Bean Validation specification and the Hibernate Validator reference guide.
这篇关于休眠验证器中某个字段的短路约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!