Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChildRules will corrupt the RuleSets of a validator passed to the SetValidator #1981

Closed
sumtec opened this issue Aug 1, 2022 · 4 comments · Fixed by #1982
Closed

ChildRules will corrupt the RuleSets of a validator passed to the SetValidator #1981

sumtec opened this issue Aug 1, 2022 · 4 comments · Fixed by #1982
Milestone

Comments

@sumtec
Copy link
Contributor

sumtec commented Aug 1, 2022

FluentValidation version

Any older than 9.4

ASP.NET version

No response

Summary

The ChildRules will use a RuleSetValidatorSelector with a rule set "*". This will overwrite rule sets to be validated specified in the validation context. This will make the child validator malfunctioned.

Steps to Reproduce

		[Fact]
		public void ChildRules_works_with_SetValidator_and_RuleSet() {
			var validator = new RulesetChildValidatorRulesValidator();

			// If the validator inside a child rule specifies a rule set "b",
			// the rules inside the rule set "b" should not be used for the validation
			// if the validation context specified the ruleset "a"
			var result = validator.Validate(new Person {
				Orders = new List<Order> {
					new Order()
				}
			}, options => options.IncludeRuleSets("a"));

			result.Errors.Count.ShouldEqual(1);
			result.Errors[0].PropertyName.ShouldEqual("Surname");			

			// They shouldn't be executed if a different ruleset is chosen.
			result = validator.Validate(new Person {
				Orders = new List<Order> {
					new Order()
				}
			}, options => options.IncludeRuleSets("other"));

			result.Errors.Count.ShouldEqual(0);
		}

		private class RulesetChildValidatorRulesValidator : AbstractValidator<Person> {
			public RulesetChildValidatorRulesValidator() {
				RuleSet("a, b", () => {
					RuleFor(x => x.Surname).NotEmpty();
					RuleFor(x => x).ChildRules(child => {
						child.RuleForEach(o => o.Orders).SetValidator(new RulesetOrderValidator());
					});
				});
			}

			private class RulesetOrderValidator : AbstractValidator<Order> {
				public RulesetOrderValidator() {
					RuleSet("b", () => {
						RuleFor(o => o.ProductName).NotEmpty();
					});
				}
			}
		}
@sumtec
Copy link
Contributor Author

sumtec commented Aug 1, 2022

Please check this one: #1982

@JeremySkinner
Copy link
Member

Thanks - good catch. I think your fix in #1982 is correct although I need to test some other scenarios. Once I've done this I'll push out a new release.

@JeremySkinner
Copy link
Member

Actually I spoke to soon, it looks like your fix is causing a test failure. Could you take a look at that please?

@JeremySkinner
Copy link
Member

Fixed in 11.1.1. Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants