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 + + +