简体   繁体   English

RuleSet 中的 FluentValidation 验证器未执行

[英]FluentValidation Validators inside RuleSet does not get executed

I have a validator with a ruleset我有一个带有规则集的验证器

public class ApplicationValidator : AbstractValidator<Application>
{
    public ApplicationValidator()
    {
        RuleSet("CreateApplication", () =>
        {
            RuleFor(a => a.SalesChannelBroker)
                .SetValidator(new BrokerActiveRule());

            RuleFor(ld => ld.LoanDetailSegment)
                .SetValidator(new ProductCodeValidRule());
        });
    }
}

However the rules are not validated when I execute validation as follows, why?但是,当我执行如下验证时,规则没有得到验证,为什么?

// Validate
var validationResult = new ApplicationValidator().Validate(app, ruleSet: "CreateApplication");

What do your BrokerActiveRule and ProductCodeValidRule validators look like?您的BrokerActiveRuleProductCodeValidRule验证器是什么样的? Are the rule/s defined in them within the CreateApplication rule set?它们中定义的规则是否在CreateApplication规则集中?

If they are not within a rule set, or in a rule set other than CreateApplication , they won't be invoked.如果它们不在规则集中,或者不在CreateApplication之外的规则集中,它们将不会被调用。

The child validator behaviour I still find a bit weird, but basically if you invoke a parent validator with a rule set, and you want your child validators to be invoked, the inclusion ( SetValidator in this case) must be within the rule set, and the child validator rules you want to apply must also be within that rule set.子验证器的行为我仍然觉得有点奇怪,但基本上,如果您使用规则集调用父验证器,并且希望调用子验证器,则包含(在本例中为SetValidator )必须在规则集内,并且您要应用的子验证器规则也必须在该规则集中。

See this for more info.有关更多信息,请参阅内容。

This behaviour can be customized by creating your own IValidatorSelector .可以通过创建您自己的IValidatorSelector定义此行为。 This was something that I was looking into doing (but haven't got round to yet) as I have a case where the child validator has default and rule set specific rules meaning I had to include the child validator twice in the parent validator.这是我正在考虑做的事情(但还没有完成),因为我有一个案例,其中子验证器具有默认值和规则集特定规则,这意味着我必须在父验证器中两次包含子验证器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM