CascadeMode improvements
See original GitHub issueCascadeMode.StopOnFirstFailure
only affects rules in the same RuleFor
chain, even when set at the validator level. This causes confusion:
CascadeMode = CascadeMode.StopOnFirstFailure;
RuleFor(x => x.Property).NotNull().NotEqual("bar");
RuleFor(x => x.AnotherProperty).NotNull().NotEqual("baz");
…so if the NotNull fails on the first rule, then the NotEqual will not run. However, the second rule will still run because these are distinct/independent rule chains. IE, this is the equivalent of:
RuleFor(x => x.Property).Cascade(CascadeMode.StopOnFirstFailure).NotNull().NotEqual("bar");
RuleFor(x => x.AnotherProperty).Cascade(CascadeMode.StopOnFirstFailure).NotNull().NotEqual("baz");
This is by design, but it isn’t intuitive and often causes confusion. I don’t think changing this behaviour is a good idea as it would be an extremely subtle breaking change that wouldn’t be caught by the compiler. Even with a major version bump this is still a very subtle change.
Investigate alternative approaches, which might involve adding new options to the CascadeMode enum to enable this behaviour.
For example, could add a new enum entry CascadeMode.FailFast
. This would do the same as StopOnFirstFailure, plus also bail out after the first failure from any rule.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:21 (20 by maintainers)
Top GitHub Comments
Agreed 100%. The documentation should probably be elaborated, but the functionality should be left intact.
That’s a great idea.
We could choose to mark
StopOnFirstFailure
asObsolete
and add another more self-explanatory enum member of an equal value, accompanying the naming style of its fail-fast/short-circuit counterpart.This sounds like a fairly easy assignment and I’d be happy to contribute.
Oh I understand. I actually understood it the other day and managed to reconfuse myself!
Yes, definitely, that’s what I’m aiming for!
The difference between chained and unchained validations are not much of a difference to me.
I might find time to work on it over the coming weeks, but it’s much lower priority than rulesets.