From the FluentValidation documentation I learned that I can abort validation by setting the cascade mode.
RuleFor(x => x.Surname)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotNull()
.NotEqual("foo");
That way if the property Surname is null, the equality check won't be executed and a null pointer exception prevented. Further down in the documentation it is implied that this would also work not only within a rule but on a validator level as well.
public class PersonValidator : AbstractValidator<Person> {
public PersonValidator() {
// First set the cascade mode
CascadeMode = CascadeMode.StopOnFirstFailure;
// Rule definitions follow
RuleFor(...)
RuleFor(...)
}
}
I set the CascadeMode not inside the rule definition but for an instance of a validator. The expected behaviour would be that if the first RuleFor
fails, the second RuleFor
won't be evaluated but that's not the case. Regardless of previous validation errors, all rules are being evaluated.
Am I using it wrong or did I misinterpret the documentation?