GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • Changelog.txt
  • docs/_doc/start
    • collections.md
  • src
    • FluentValidation.Tests
      • ForEachRuleTests.cs
    • FluentValidation
      • DefaultValidatorExtensions.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Changelog.txt‎

Copy file name to clipboardExpand all lines: Changelog.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
8.2.0 -
22
Add extensions for injecting child validators using the ASP.NET Core Service Provider.
33
WithLocalizedMessage is deprecated. Use the overload of WithMessage that takes a Func instead.
4+
Implement RuleFor(...).ForEach(...) as an alternative to RuleForEach
45

56
8.1.3 - 25 January 2019
67
Resolved issue where clientside validation was incorrectly generating metadata for rules with shared conditions.
Collapse file

‎docs/_doc/start/collections.md‎

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 numberDiff line numberDiff line change
@@ -26,3 +26,24 @@ RuleForEach(x => x.Orders)
2626
.Where(x => x.Cost != null)
2727
.SetValidator(new OrderValidator());
2828
```
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

‎src/FluentValidation.Tests/ForEachRuleTests.cs‎

Copy file name to clipboardExpand all lines: src/FluentValidation.Tests/ForEachRuleTests.cs
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,27 @@ public void Nested_conditions_Rule_For_Each() {
244244
var result = validator.Validate(new request());
245245
result.Errors.Count.ShouldEqual(0);
246246
}
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+
}
247268

248269
public class ApplicationViewModel {
249270
public List<ApplicationGroup> TradingExperience { get; set; } = new List<ApplicationGroup> {new ApplicationGroup()};
Collapse file

‎src/FluentValidation/DefaultValidatorExtensions.cs‎

Copy file name to clipboardExpand all lines: src/FluentValidation/DefaultValidatorExtensions.cs
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,5 +1070,20 @@ public static IRuleBuilderInitial<T, TProperty> Custom<T, TProperty>(this IRuleB
10701070
public static IRuleBuilderInitial<T, TProperty> CustomAsync<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, Func<TProperty, CustomContext, CancellationToken, Task> action) {
10711071
return (IRuleBuilderInitial<T, TProperty>) ruleBuilder.SetValidator(new CustomValidator<TProperty>(action));
10721072
}
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+
}
10731088
}
10741089
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.