GeezerVison is showing github.com — Implement RuleFor(...).ForEach(...) as an alternative to RuleForEach. · riyuexing/FluentValidation@81b23f2
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Changelog.txt +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | 8.2.0 - |
2 | 2 | Add extensions for injecting child validators using the ASP.NET Core Service Provider. |
3 | 3 | WithLocalizedMessage is deprecated. Use the overload of WithMessage that takes a Func instead. |
| 4 | +Implement RuleFor(...).ForEach(...) as an alternative to RuleForEach |
4 | 5 |
|
5 | 6 | 8.1.3 - 25 January 2019 |
6 | 7 | Resolved issue where clientside validation was incorrectly generating metadata for rules with shared conditions. |
|
Collapse file
Copy file name to clipboardExpand all lines: docs/_doc/start/collections.md +21Lines changed: 21 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -26,3 +26,24 @@ RuleForEach(x => x.Orders) |
26 | 26 | .Where(x => x.Cost != null) |
27 | 27 | .SetValidator(new OrderValidator()); |
28 | 28 | ``` |
| 29 | +As an alternative to using `RuleForEach`, you can call `ForEach` as part of a regular `RuleFor`. With this approach you can combine rules that act upon the entire collection with rules which act upon individual elements within the collection. For example: |
| 30 | + |
| 31 | +```csharp |
| 32 | +RuleFor(x => x.Orders) |
| 33 | + .Must(x => x.Orders.Count <= 10).WithMessage("No more than 10 orders are allowed") |
| 34 | + .ForEach(orderRule => { |
| 35 | + orderRule.Must(order => order.Total > 0).WithMessage("Orders must have a total of more than 0") |
| 36 | + }); |
| 37 | +``` |
| 38 | + |
| 39 | +The above example could also have been specified as 2 separate rules: |
| 40 | + |
| 41 | +```csharp |
| 42 | +RuleFor(x => x.Orders) |
| 43 | + .Must(x => x.Orders.Count <= 10).WithMessage("No more than 10 orders are allowed"); |
| 44 | + |
| 45 | +RuleForEach(x => x.Orders) |
| 46 | + .Must(order => order.Total > 0).WithMessage("Orders must have a total of more than 0") |
| 47 | +``` |
| 48 | + |
| 49 | +I personally using 2 rules is clearer and easier to read, but the option of combining them is available with the `ForEach` method. |
Collapse file
Copy file name to clipboardExpand all lines: src/FluentValidation.Tests/ForEachRuleTests.cs +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -244,6 +244,27 @@ public void Nested_conditions_Rule_For_Each() { |
244 | 244 | var result = validator.Validate(new request()); |
245 | 245 | result.Errors.Count.ShouldEqual(0); |
246 | 246 | } |
| 247 | + |
| 248 | + [Fact] |
| 249 | + public void Regular_rules_can_drop_into_RuleForEach() { |
| 250 | + var validator = new TestValidator(); |
| 251 | + validator.RuleFor(x => x.Children) |
| 252 | + .Must(x => x.Count > 2).WithMessage("Foo") |
| 253 | + .ForEach(forEachElement => { |
| 254 | + forEachElement.NotNull().WithMessage("Bar"); |
| 255 | + }); |
| 256 | + |
| 257 | + var result = validator.Validate(new Person {Children = new List<Person> {null, null}}); |
| 258 | + result.Errors.Count.ShouldEqual(3); |
| 259 | + result.Errors[0].ErrorMessage.ShouldEqual("Foo"); |
| 260 | + result.Errors[0].PropertyName.ShouldEqual("Children"); |
| 261 | + |
| 262 | + result.Errors[1].ErrorMessage.ShouldEqual("Bar"); |
| 263 | + result.Errors[1].PropertyName.ShouldEqual("Children[0]"); |
| 264 | + |
| 265 | + result.Errors[2].ErrorMessage.ShouldEqual("Bar"); |
| 266 | + result.Errors[2].PropertyName.ShouldEqual("Children[1]"); |
| 267 | + } |
247 | 268 |
|
248 | 269 | public class ApplicationViewModel { |
249 | 270 | public List<ApplicationGroup> TradingExperience { get; set; } = new List<ApplicationGroup> {new ApplicationGroup()}; |
|
Collapse file
Copy file name to clipboardExpand all lines: src/FluentValidation/DefaultValidatorExtensions.cs +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1070,5 +1070,20 @@ public static IRuleBuilderInitial<T, TProperty> Custom<T, TProperty>(this IRuleB |
1070 | 1070 | public static IRuleBuilderInitial<T, TProperty> CustomAsync<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, Func<TProperty, CustomContext, CancellationToken, Task> action) { |
1071 | 1071 | return (IRuleBuilderInitial<T, TProperty>) ruleBuilder.SetValidator(new CustomValidator<TProperty>(action)); |
1072 | 1072 | } |
| 1073 | + |
| 1074 | + /// <summary> |
| 1075 | + /// Allows rules to be built against individual elements in the collection. |
| 1076 | + /// </summary> |
| 1077 | + /// <param name="ruleBuilder"></param> |
| 1078 | + /// <param name="action"></param> |
| 1079 | + /// <typeparam name="T"></typeparam> |
| 1080 | + /// <typeparam name="TElement"></typeparam> |
| 1081 | + /// <returns></returns> |
| 1082 | + public static IRuleBuilder<T, IEnumerable<TElement>> ForEach<T, TElement>(this IRuleBuilder<T, IEnumerable<TElement>> ruleBuilder, |
| 1083 | + Action<IRuleBuilderInitialCollection<IEnumerable<TElement>, TElement>> action) { |
| 1084 | + var innerValidator = new InlineValidator<IEnumerable<TElement>>(); |
| 1085 | + action(innerValidator.RuleForEach(x => x)); |
| 1086 | + return ruleBuilder.SetValidator(innerValidator); |
| 1087 | + } |
1073 | 1088 | } |
1074 | 1089 | } |