SetValidator inherited rulesets behavior
See original GitHub issueDocumentation explains the following:
Validating a specific ruleset with SetValidator Previously, if you defined a child validator with
SetValidator
, then whichever ruleset you invoked on the parent validator will cascade to the child validator. Now you can explicitly define which ruleset will run on the child:
RuleFor(x => x.Address).SetValidator(new AddressValidator(), "myRuleset");
From what I’ve just experienced, if I don’t explicitly set a ruleset when calling SetValidator()
, the newly created validator (like AddressValidator
in that example), will not contemplate any ruleset at all…
This poses me a problem, because the previous behavior that “automatically inherited” the parent validator ruleset allowed me to not having to worry about passing any extra stuff if it wasn’t any different… How can I achieve the same functionality? As far as I can see, I can’t pass the current ruleset that my validator got passed from above so any new validator I create inside it, infers or inherits the same.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
This is already present inside the ValidationContext, but this is only available when validation is executed, not at the point when rules are defined, so this wouldn’t really make sense. At the point the rules are defined, you haven’t invoked validate yet so the validator has no idea what you’re planning to execute.
That’s correct, but you can just define the call to SetValidator multiple times if you need to, depending on the scenario.
However, personally I think your setup is too complicated and hard to follow using rulesets like this (as illustrated by this discussion). I’d suggest you consider splitting these into separate classes and then being very explicit about what you’re running in each scenario. Ruleset cascade is complicated and it only gets more-so when you’re trying to customise it and override the default behaviour at multiple levels deep. This feels like it’s going to cause a huge maintenance headache if you continue down this route.
Well, that’s why. The rule for
Field2
in Validator3 is explicitly wrapped in “RuleSet1”, but you’ve not included RuleSet1 for execution for validator3. The ruleset cascade only works if you’re actually triggering a ruleset. In your case you’re not running a ruleset, so the process runs like this:Because
If you want Validator3’s “RuleSet1” to execute, you need to specify it in the call to SetValidator, in the same way you did for the root validator. The cascading/automatic propogation of selected rulesets only applies if you’re actually executing a specific ruleset, which you’re not.
Essentially, because you’re not invoking a ruleset at the root level, only rules not in a ruleset will ever execute unless you override this in the SetValidator call, which you’re not doing for Validator3. I think you need to change it to this:
If you do that, RuleSet1 will always be executed inside Validator3.
If you’re still having trouble understanding, please put together some runnable unit tests, and their expected result, and I will run them and show you what you need to change