29 lines
931 B
C#
29 lines
931 B
C#
using FluentValidation;
|
|
|
|
namespace WebApi.Filters;
|
|
|
|
public sealed class ValidationFilter : IEndpointFilter
|
|
{
|
|
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
|
{
|
|
foreach (var argument in context.Arguments)
|
|
{
|
|
if (argument is null)
|
|
continue;
|
|
|
|
var validatorType = typeof(IValidator<>).MakeGenericType(argument.GetType());
|
|
|
|
if (context.HttpContext.RequestServices.GetService(validatorType) is not IValidator validator)
|
|
continue;
|
|
|
|
var validationContext = new ValidationContext<object>(argument);
|
|
var result = await validator.ValidateAsync(validationContext, context.HttpContext.RequestAborted);
|
|
|
|
if (!result.IsValid)
|
|
return Results.ValidationProblem(result.ToDictionary());
|
|
}
|
|
|
|
return await next(context);
|
|
}
|
|
}
|