如何使用流畅验证来验证包含多个相同类型对象的对象?
我有一个 Action 类,其中包含更多 Action 对象的集合。像这样的东西:
public class Action
{
ICollection<Action> SubActions;
}
这基本上形成了一个树结构(我确保没有循环)。我使用 Fluent Validation 为此类编写了一个验证器。这是我的验证器尝试:
public class ActionValidator : AbstractValidator<Action>
{
public ActionValidator()
{
RuleFor(x => x.SubActions).SetCollectionValidator(new ActionValidator());
}
}
当我尝试解决任何依赖于 ActionValidator 的问题时,Unity 就会崩溃。更具体地说,当 LINQPad 尝试解析依赖于 ActionValidator 的服务时,可能会由于堆栈溢出而崩溃。
我正在验证我的 Action 类中的其他成员,但为了简洁起见,我只是放置了重要的部分。如果我注释掉此处列出的规则,它就可以正常工作(除了它不再验证子操作)。
我的方法有问题。我正在递归地构建验证器,直到有东西消失。但我只是不确定如何告诉 Fluent Validation 以这种方式验证子对象。
I have a class Action which has a collection of more Action objects. Something like this:
public class Action
{
ICollection<Action> SubActions;
}
This basically forms a tree structure (I make sure there are no cycles). I used Fluent Validation to write a validator for this class. Here is my Validator attempt:
public class ActionValidator : AbstractValidator<Action>
{
public ActionValidator()
{
RuleFor(x => x.SubActions).SetCollectionValidator(new ActionValidator());
}
}
Unity blows up when I try to resolve anything which depends on ActionValidator. More specifically, LINQPad crashes when it tries to resolve a service which depends on ActionValidator, presumably from a stack overflow.
There are other members in my Action class that I'm validating, but I've just put the important part for brevity. If I comment out the rule I've listed here, it works fine (except it's not validating subactions anymore).
I get the problem with my approach. I'm recursively constructing validators until something dies. But I'm just not sure how I'm to tell Fluent Validation to validate sub-objects this way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将验证相同类型的规则更改为:
Change the rule that validates the same type to: