0
votes

I have two properties that must be validated, but once at a time. If one is null then validate the other, so I was trying like this :

RuleFor(a => a.CNPJ).Must(a => CNPHelper.CheckCNPJ(a)).When(a => !string.IsNullOrEmpty(a.CPF));
RuleFor(a => a.CPF).Must(a => CNPHelper.CheckCPF(a)).When(a => !string.IsNullOrEmpty(a.CNPJ));

But the must is mandatory, how to change it ?

1

1 Answers

0
votes

Can you please try DependentRules extension. As from docs (https://fluentvalidation.net/start#conditions) reference, you can achieve it as

RuleFor(x => x.Surname).NotNull().DependentRules(() => {
  RuleFor(x => x.Forename).NotNull();
});