I have the following code to validate an entity:
public class AffiliateValidator : AbstractValidator<Affiliate>
{
public AffiliateValidator ()
{
RuleFor(x => x.IBAN).SetValidator(new ValidIBAN()).Unless( x => String.IsNullOrWhiteSpace(x.IBAN));
}
}
And ValidIBAN() Code:
public class ValidIBAN : PropertyValidator
{
public ValidIBAN()
:base("IBAN \"{PropertyValue}\" not valid.")
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
var iban = context.PropertyValue as string;
IBAN.IBANResult result = IBAN.CheckIban(iban, false);
return result.Validation == (IBAN.ValidationResult.IsValid);
}
}
}
So,CheckIBAN method of IBAN class does the dirty job.
Now, what I need to to is apply the following rule for another property: If DirectDebit (bool) is true, then IBAN can't be empty and also it must be valid.
I can do this:
RuleFor(x => x.DirectDebit).Equal(false).When(a => string.IsNullOrEmpty(a.IBAN)).WithMessage("TheMessage.");
But how can I Invoke another rule, IBAN's rule in this case, in order to check if is or not valid?
Often the problems are simpler than they seem. This is the solution I adopt to aply the rule for DirectDebit field.
and change the rule for IBAN also:
...and then:
the trick is use instance parameter of Must() to do whetever I want.