question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CascadeMode improvements

See original GitHub issue

CascadeMode.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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:21 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
weitzhandlercommented, Jul 9, 2020

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.

Agreed 100%. The documentation should probably be elaborated, but the functionality should be left intact.

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.

That’s a great idea.

We could choose to mark StopOnFirstFailure as Obsolete 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.

1reaction
weitzhandlercommented, Jul 13, 2020

Oh I understand. I actually understood it the other day and managed to reconfuse myself!

If you want to discuss cascading between rulesets, we can do that, but that isn’t the goal here. We can open a separate issue to discuss and track that, but it’s a larger issue that needs a separate discussion.

Yes, definitely, that’s what I’m aiming for!

If that isn’t something you’re interested in working on then that’s fine, but please let me know now.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting the Cascade mode
You can set the cascade mode to customise how FluentValidation executes rules and validators when a particular rule in the validator class, or...
Read more >
11.0 Upgrade Guide — FluentValidation documentation
The CascadeMode properties on AbstractValidator and ValidatorOptions.Global have been deprecated and replaced with the properties RuleLevelCascadeMode and ...
Read more >
Using CascadeMode.StopOnFirstFailure on a validator level
From the FluentValidation documentation I learned that I can abort validation by setting the cascade mode. RuleFor(x => x.Surname) .Cascade( ...
Read more >
ASP .NET Core API – Fluent Validations – Cascade ...
Here in this article we are going to discuss about cascade modes supported by fluent validation. There are two modes – rule level...
Read more >
FluentValidation 9.1 released - Jeremy Skinner
This release contains a few new features and some bug fixes. ... FluentValidation has supported the concept of a “CascadeMode” since version ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found