Using CascadeMode.StopOnFirstFailure at the Validation Level

From the FluentValidation documentation, I found out that I can cancel the test by setting the cascade mode.

RuleFor(x => x.Surname)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotNull()
.NotEqual("foo");

Thus, if the Last Name property is null, equality checks will not be performed and null pointer exceptions will be thrown. Further in the documentation it is implied that this will also work not only within the rule, but also at the validator level.

public class PersonValidator : AbstractValidator<Person> {
  public PersonValidator() {

    // First set the cascade mode
    CascadeMode = CascadeMode.StopOnFirstFailure;

    // Rule definitions follow
    RuleFor(...) 
    RuleFor(...)
  }
}

I install CascadeMode not inside the rule definition, but for the validator instance. The expected behavior will be that if the first RuleForfails, the second RuleForwill not be evaluated, but this is not so. Regardless of previous validation errors, all rules are evaluated.

?

+4
2

JeremyS CascadeMode. , .

+8

CascadeMode ,

ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;

RuleFor(x => x.PropertyName)
    .Cascade(CascadeMode.StopOnFirstFailure)
+4

All Articles