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

Fix the issue that the ChildRules will corrupt the RuleSets of a validator passed to the SetValidator #1982

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 52 additions & 3 deletions src/FluentValidation.Tests/ChildRulesTests.cs
Expand Up @@ -33,11 +33,13 @@ public class ChildRulesTests {
order.RuleFor(x => x.Amount).GreaterThan(0);
});

var result = validator.Validate(new Person {Orders = new List<Order> {
var result = validator.Validate(new Person {
Orders = new List<Order> {
new Order { ProductName = null, Amount = 10 },
new Order { ProductName = "foo", Amount = 0},
new Order { ProductName = "foo", Amount = 10 }
}});
}
});

result.Errors.Count.ShouldEqual(2);
result.Errors[0].PropertyName.ShouldEqual("Orders[0].ProductName");
Expand Down Expand Up @@ -71,7 +73,33 @@ public class ChildRulesTests {
result.Errors.Count.ShouldEqual(0);
}

private class RulesetChildRulesValidator : AbstractValidator<Person> {
[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 RulesetChildRulesValidator : AbstractValidator<Person> {
public RulesetChildRulesValidator() {
RuleSet("testing", () => {
RuleFor(a => a.Surname).NotEmpty();
Expand All @@ -81,5 +109,26 @@ private class RulesetChildRulesValidator : AbstractValidator<Person> {
});
}
}

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();
});
}
}
}

}
}

8 changes: 7 additions & 1 deletion src/FluentValidation/DefaultValidatorExtensions.cs
Expand Up @@ -1169,8 +1169,14 @@ public static partial class DefaultValidatorExtensions {
public static IRuleBuilderOptions<T, TProperty> ChildRules<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, Action<InlineValidator<TProperty>> action) {
if (action == null) throw new ArgumentNullException(nameof(action));
var validator = new InlineValidator<TProperty>();
var ruleSets = ((IRuleBuilderInternal<T, TProperty>)ruleBuilder).Rule.RuleSets;
action(validator);
return ruleBuilder.SetValidator(validator, "*");
foreach(var rule in validator.Rules) {
if (rule.RuleSets == null) {
rule.RuleSets = ruleSets;
}
}
return ruleBuilder.SetValidator(validator, ruleSets);
}

/// <summary>
Expand Down