From 6fc2fc22ac1f4aaa5a9a6d245c2a2ddb627695e1 Mon Sep 17 00:00:00 2001 From: Mikhail Kuznetsov Date: Thu, 21 May 2026 10:21:00 +1000 Subject: [PATCH] =?UTF-8?q?fix(api):=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B0=D1=81=D0=B8=D0=BD=D1=85=D1=80=D0=BE=D0=BD=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20FluentValidation=20-=20=D1=83=D0=B4=D0=B0=D0=BB=D1=91?= =?UTF-8?q?=D0=BD=20=D1=83=D1=81=D1=82=D0=B0=D1=80=D0=B5=D0=B2=D1=88=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=20FluentValidation.AspN?= =?UTF-8?q?etCore,=20=D0=B2=D1=8B=D0=B7=D1=8B=D0=B2=D0=B0=D0=B2=D1=88?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=BA=D0=BE=D0=BD=D1=84=D0=BB=D0=B8=D0=BA=D1=82?= =?UTF-8?q?=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B9=20(NU1605)=20-=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=B3?= =?UTF-8?q?=D0=BB=D0=BE=D0=B1=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D1=84?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D1=82=D1=80=20AsyncValidationFilter=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B8=20MustAsync-=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PARR.API/Infrastructure/ConfigureValidator.cs | 13 +++- .../Filters/AsyncValidationFilter.cs | 47 ++++++++++++ PARR.API/PARR.API.csproj | 74 +++++++++---------- PARR.API/Validators/JobRequestValidator.cs | 4 +- .../PARR.TemplateMatcher.csproj | 6 ++ 5 files changed, 102 insertions(+), 42 deletions(-) create mode 100644 PARR.API/Infrastructure/Filters/AsyncValidationFilter.cs diff --git a/PARR.API/Infrastructure/ConfigureValidator.cs b/PARR.API/Infrastructure/ConfigureValidator.cs index c50bab4a..bd38b386 100644 --- a/PARR.API/Infrastructure/ConfigureValidator.cs +++ b/PARR.API/Infrastructure/ConfigureValidator.cs @@ -1,7 +1,7 @@ using FluentValidation; -using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Mvc; using PARR.API.Contracts.V1.Responses.Base; +using PARR.API.Infrastructure.Filters; using PARR.API.Infrastructure.Middleware; namespace PARR.API.Infrastructure @@ -22,7 +22,11 @@ namespace PARR.API.Infrastructure services.AddProblemDetails(); // Генерирует стандартный формат ошибки RFC 7807 // Настройка контроллеров и кастомного формата ошибок валидации - services.AddControllers() + services.AddControllers(options => + { + // Автоматически применяем асинхронную валидацию ко всем контроллерам + options.Filters.Add(); + }) .ConfigureApiBehaviorOptions(options => { options.InvalidModelStateResponseFactory = context => @@ -43,7 +47,10 @@ namespace PARR.API.Infrastructure //services.AddValidatorsFromAssemblies(Assembly.GetExecutingAssembly()); // Включаем авто-валидацию FluentValidation для контроллеров - services.AddFluentValidationAutoValidation(); + //services.AddFluentValidationAutoValidation(); + + services.AddScoped(); + return services; } diff --git a/PARR.API/Infrastructure/Filters/AsyncValidationFilter.cs b/PARR.API/Infrastructure/Filters/AsyncValidationFilter.cs new file mode 100644 index 00000000..93ceccb6 --- /dev/null +++ b/PARR.API/Infrastructure/Filters/AsyncValidationFilter.cs @@ -0,0 +1,47 @@ +using FluentValidation; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using PARR.API.Contracts.V1.Responses.Base; + +namespace PARR.API.Infrastructure.Filters +{ + public class AsyncValidationFilter : IAsyncActionFilter + { + private readonly IServiceProvider _serviceProvider; + + public AsyncValidationFilter(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + foreach (var argument in context.ActionArguments.Values) + { + if (argument == null) + continue; + + var validatorType = typeof(IValidator<>).MakeGenericType(argument.GetType()); + var validator = _serviceProvider.GetService(validatorType); + + if (validator is IValidator validatorInstance) + { + var validationContext = new ValidationContext(argument); + var validationResult = await validatorInstance.ValidateAsync(validationContext, context.HttpContext.RequestAborted); + + if (!validationResult.IsValid) + { + var errors = validationResult.Errors + .Select(e => new ErrorModel { Message = e.ErrorMessage }) + .ToList(); + + context.Result = new BadRequestObjectResult(new Response(false, errors)); + return; + } + } + } + + await next(); + } + } +} \ No newline at end of file diff --git a/PARR.API/PARR.API.csproj b/PARR.API/PARR.API.csproj index 943c843d..54d050af 100644 --- a/PARR.API/PARR.API.csproj +++ b/PARR.API/PARR.API.csproj @@ -1,44 +1,44 @@ - + - - net9.0 - enable - enable - True - Linux - ..\docker-compose.dcproj - + + net9.0 + enable + enable + True + Linux + ..\docker-compose.dcproj + - - 1701;1702;1591;1587;1573;NU1803 - + + 1701;1702;1591;1587;1573;NU1803 + - - 1701;1702;1591;1587;1573;NU1803 - + + 1701;1702;1591;1587;1573;NU1803 + - - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + - - - - - + + + + + diff --git a/PARR.API/Validators/JobRequestValidator.cs b/PARR.API/Validators/JobRequestValidator.cs index bff166d3..e4ea0f82 100644 --- a/PARR.API/Validators/JobRequestValidator.cs +++ b/PARR.API/Validators/JobRequestValidator.cs @@ -11,7 +11,7 @@ namespace PARR.API.Validators { private readonly ITnkRepository tnkService; private readonly IJobGroupRepository jobGroupService; - private readonly IJobRepository jobService; + private readonly IJobRepository jobRepository; private readonly IUnitFieldRepository unitFieldService; private JobGroup? jobGroup; @@ -24,7 +24,7 @@ namespace PARR.API.Validators { this.tnkService = tnkService; this.jobGroupService = jobGroupService; - this.jobService = jobService; + this.jobRepository = jobService; this.unitFieldService = unitFieldService; RuleFor(t => t.Name) diff --git a/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj b/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj index 108704d2..a17ad3a6 100644 --- a/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj +++ b/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj @@ -13,4 +13,10 @@ + + + ..\PARR.Core\obj\Debug\net9.0\ref\PARR.Core.dll + + +