You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var context = new ValidationContext<TRequest>(request);
var validationResults = await Task.WhenAll(
_validators.Select(v =>v.ValidateAsync(context, cancellationToken))
);
Unfortunately context is not thread safe and adds validation errors to collection (somewhere inside).
Steps to reproduce, have 2 validators for the same Request object, you will see that messages will duplicate. And occasionally (if you use real DB validation like BeUniqueTitle), you'll get error 'collection was modified..., when 2 threads will try to add error message simultaneously.
I rewrote it using simple foreach
List<FluentValidation.Results.ValidationFailure> failures = [];
foreach (var v in _validators)
{
var context = new ValidationContext<TRequest>(request);
var r = await v.ValidateAsync(context, cancellationToken);
if (!r.IsValid)
failures.AddRange(r.Errors);
}
File src/Application/Common/Behaviours/ValidationBehaviour.cs
Lines # 19-23
Unfortunately context is not thread safe and adds validation errors to collection (somewhere inside).
Steps to reproduce, have 2 validators for the same Request object, you will see that messages will duplicate. And occasionally (if you use real DB validation like BeUniqueTitle), you'll get error 'collection was modified..., when 2 threads will try to add error message simultaneously.
I rewrote it using simple foreach
Source for ValidationContext, Line 86 has "internal List Failures { get; }" which makes it not thread safe.
https://github.com/FluentValidation/FluentValidation/blob/main/src/FluentValidation/IValidationContext.cs
The text was updated successfully, but these errors were encountered: