44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using FluentValidation;
|
|
using MediatR;
|
|
using ValidationException = AutobusApi.Application.Common.Exceptions.ValidationException;
|
|
|
|
namespace AutobusApi.Application.Common.Behaviours;
|
|
|
|
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : notnull
|
|
{
|
|
private readonly IEnumerable<IValidator<TRequest>> _validators;
|
|
|
|
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators)
|
|
{
|
|
_validators = validators;
|
|
}
|
|
|
|
public async Task<TResponse> Handle(
|
|
TRequest request,
|
|
RequestHandlerDelegate<TResponse> next,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (_validators.Any())
|
|
{
|
|
var context = new ValidationContext<TRequest>(request);
|
|
|
|
var validationResults = await Task.WhenAll(
|
|
_validators.Select(v =>
|
|
v.ValidateAsync(context, cancellationToken)));
|
|
|
|
var failures = validationResults
|
|
.Where(r => r.Errors.Any())
|
|
.SelectMany(r => r.Errors)
|
|
.ToList();
|
|
|
|
if (failures.Any())
|
|
{
|
|
throw new ValidationException(failures);
|
|
}
|
|
}
|
|
|
|
return await next();
|
|
}
|
|
}
|