1

I have a class Property, which contains the following three Properties:

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.

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

How do I go about setting a rule based on the value of a boolean?

1 Answer 1

3

The When and Unless methods can be used to specify conditions that control when the rule should execute. The Unless method is simply the opposite of 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(x => !x.CorrespondanceAddressIsSecurityAddress, () => 
{
    RuleFor(x => x.SecurityAddress)
       .NotNull().WithMessage("Security address cannot be null")
       .SetValidator(new AddressValidator());
    // another RuleFor calls
});

Link to documentation

Sign up to request clarification or add additional context in comments.

1 Comment

You are a fantastic person. I didn't realise the When() method existed. I couldn't see it anywhere on the documentation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.