嵌套 Jsr 303 验证注释
我想使用类级别注释约束。但是我无法自动验证内部约束。我想帮助一部分,将验证组合并到这项技术中。
@ConstraintA({
@ConstraintB(stuff),
@ConstraintB(stuff, groups=SomeGroup.class)
})
public class Form{
}
我目前像这样触发约束。
if(constraint instanceof ConstraintB){
new ConstraintBValidator().isValid(target, context);
}
然而,这显然很糟糕。我最终将重构以通过调用 AnnotationIn VocationHandler.invoke() 方法来触发 isValid 方法,但我离那还有一点距离。
我的问题是所有 ConstraintB 实例都传递到我的 ConstraintA 中。我希望只将具有适当组的那些传递给 ConstraintA。我怀疑这种能力是否存在,那么我如何确定哪些组需要被触发,哪些组不需要?
我在调试中没有看到任何指定应触发哪些组的对象?
有什么想法吗?
I would like to use a class level annotation constraint. However I cannot get the inner constraints to validate automatically. I'd like to help with one part, to incorporate validation groups into this technique.
@ConstraintA({
@ConstraintB(stuff),
@ConstraintB(stuff, groups=SomeGroup.class)
})
public class Form{
}
I currentily trigger the constraints like so.
if(constraint instanceof ConstraintB){
new ConstraintBValidator().isValid(target, context);
}
However this sucks obviously.I will eventually refactor to trigger the isValid methods through a call to the AnnotationInvocationHandler.invoke() method, but im a little way from that still.
My issue is that all ConstraintB instances are passed into my ConstraintA. I wish only the ones with the appropriate groups to be passed to ConstraintA. I doubt this ability exists, so how can i identify which groups need to be triggered and which dont?
I dont see in my debug, any objects which specify which groups should be triggered?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 JSR 303 规范中找到了允许此类验证的模式。它是一种递归模式,不会执行父验证规则,仅满足嵌套验证。这非常方便。我的嵌套验证规则有条件地基于其他属性值,因此它允许使用嵌套 jsr303 注释进行条件验证。
我自己的验证更像是这样的:
I've found a pattern in the JSR 303 spec that allows this type of validation. Its a recursive pattern that wont execute the parent validation rule, only fulfill the nested validations. This is VERY handy. My nested validation rules are conditionally based on other properties values so it allows conditional validation with nested jsr303 annotations.
my own validation is more like this: