Skip to content

Commit

Permalink
Running FluentValidation validators single file to dodge concurrency …
Browse files Browse the repository at this point in the history
…problems. Closes GH-537
  • Loading branch information
jeremydmiller committed Sep 15, 2023
1 parent 6751874 commit 2bfa2ac
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using FluentValidation;
using FluentValidation.Results;

namespace Wolverine.FluentValidation.Internals;

Expand All @@ -19,16 +20,21 @@ public static async Task ExecuteOne<T>(IValidator<T> validator, IFailureAction<T
public static async Task ExecuteMany<T>(IReadOnlyList<IValidator<T>> validators, IFailureAction<T> failureAction,
T message)
{
var validationFailureTasks = validators
.Select(validator => validator.ValidateAsync(message));
var validationFailures = await Task.WhenAll(validationFailureTasks);
var failures = validationFailures.SelectMany(validationResult => validationResult.Errors)
.Where(validationFailure => validationFailure != null)
.ToList();
var failures = new List<ValidationFailure>();

foreach (var validator in validators)
{
var result = await validator.ValidateAsync(message);
if (result is not null && result.Errors.Any())
{
failures.AddRange(result.Errors);
}
}

if (failures.Any())
{
failureAction.Throw(message, failures);
}
}

}

0 comments on commit 2bfa2ac

Please sign in to comment.