Skip to content

Commit

Permalink
Fix #1597 child rules not working as expeted with rulesets.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Jan 14, 2021
1 parent a147ef4 commit be10c61
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
9.4.0 - 14 January 2021
ChildRules now work as expected when inside a ruleset (#1597)

9.3.0 - 10 November 2020
Support for .NET 5
Improvements to LanguageManager's lazy loading of resources.
Expand Down
38 changes: 38 additions & 0 deletions src/FluentValidation.Tests/ChildRulesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,43 @@ public class ChildRulesTests {
result.Errors[0].PropertyName.ShouldEqual("Orders[0].ProductName");
result.Errors[1].PropertyName.ShouldEqual("Orders[1].Amount");
}

[Fact]
public void ChildRules_works_with_RuleSet() {
var validator = new RulesetChildRulesValidator();

// As Child Rules are implemented as a child validator, the child rules are technically
// not inside the "testing" ruleset (going by the usual way rulesets cascade).
// However, child rules should still be executed.
var result = validator.Validate(new Person {
Orders = new List<Order> {
new Order()
}
}, options => options.IncludeRuleSets("testing"));

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

// 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();
RuleForEach(a => a.Orders).ChildRules(child => {
child.RuleFor(o => o.ProductName).NotEmpty();
});
});
}
}
}
}
2 changes: 1 addition & 1 deletion src/FluentValidation/DefaultValidatorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ public static partial class DefaultValidatorExtensions {
if (action == null) throw new ArgumentNullException(nameof(action));
var validator = new InlineValidator<TProperty>();
action(validator);
return ruleBuilder.SetValidator(validator);
return ruleBuilder.SetValidator(validator, "*");
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/FluentValidation/FluentValidation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<PackageReleaseNotes>
FluentValidation 9 is a major release. Please read the upgrade notes at https://docs.fluentvalidation.net/en/latest/upgrading-to-9.html

Changes in 9.4.0:
* ChildRules now work as expected when inside a ruleset

Changes in 9.3.0:
* Improvements to LanguageManager's lazy loading of resources.
* Deprecate IStringSource and its implementors. Use delegates instead.
Expand Down

0 comments on commit be10c61

Please sign in to comment.