简体   繁体   English

使用FluentValidation基于布尔值的结果设置规则

[英]Set a rule based on the result of a boolean using FluentValidation

I have a class Property , which contains the following three Properties: 我有一个类Property ,它包含以下三个属性:

bool CorrespondanceAddressIsSecurityAddress
Address CorrespondanceAddress
Address SecurityAddress

The address class is just a basic address class that holds details about a user's address. 地址类只是一个基本地址类,包含有关用户地址的详细信息。

The user's correspondance address will always be filled in and therefore needs to be validated. 用户的对应地址将始终填写,因此需要进行验证。 The user has the option to select that their correspondance address is the same as their security address, when this occurs there is no need to validate the security address and it can be left as null. 用户可以选择其对应地址与其安全地址相同,当发生这种情况时,无需验证安全地址,并且可以将其保留为空。

What I would like to do is check the state of the CorrespondanceAddressIsSecurityAddress boolean and then set a validator for the security address if it is set to false, but I'm not sure of the syntax that is required to do this. 我想要做的是检查CorrespondanceAddressIsSecurityAddress布尔值的状态,然后为安全地址设置验证器,如果它设置为false,但我不确定执行此操作所需的语法。

At the moment the class that controls the validation for the property class contains the two following lines: 目前,控制属性类验证的类包含以下两行:

RuleFor(p => p.CorrespondanceAddressIsSecurityAddress)
   .NotNull()
   .WithMessage("CorrespondanceAddressIsSecurityAddress cannot be null");
RuleFor(p => P.CorrespondanceAddressIsSecurityAddress
   .Equals(false))
   .SetValidator(new FluentAddressValidator());

However the second rule, which sets the validator gives a syntax error, saying 但是,设置验证器的第二条规则会产生语法错误

Cannot convert from '...FluentAddressValidator' to 'FluentValidation.Validators.IPropertyValidator 无法从'... FluentAddressValidator'转换为'FluentValidation.Validators.IPropertyValidator

How do I go about setting a rule based on the value of a boolean? 如何根据布尔值设置规则?

The When and Unless methods can be used to specify conditions that control when the rule should execute. WhenUnless方法可用于指定控制规则何时应执行的条件。 The Unless method is simply the opposite of When Unless方法与When相反

According to question you should write your validator next way: 根据问题,你应该写下你的验证器:

public class PropertyValidator : AbstractValidator<Property>
{
    public PropertyValidator()
    {
        RuleFor(x => x.CorrespondanceAddress)
            .NotNull().WithMessage("Correspondence address cannot be null")
            .SetValidator(new AddressValidator());
        RuleFor(x => x.SecurityAddress)
            .NotNull().WithMessage("Security address cannot be null")
            .SetValidator(new AddressValidator())
            .When(x => !x.CorrespondanceAddressIsSecurityAddress); // applies to all validations chain
          //.Unless(x => x.CorrespondanceAddressIsSecurityAddress); - do the same as at previous line
    }
}

If you need to specify the same condition for multiple rules then you can call the top-level When method instead of chaining the When call at the end of the rule: 如果需要为多个规则指定相同的条件,则可以调用顶级When方法,而不是在规则末尾链接When调用:

When(x => !x.CorrespondanceAddressIsSecurityAddress, () => 
{
    RuleFor(x => x.SecurityAddress)
       .NotNull().WithMessage("Security address cannot be null")
       .SetValidator(new AddressValidator());
    // another RuleFor calls
});

Link to documentation 链接到文档

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

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