diff --git a/src/N8T.Infrastructure/Validator/RequestValidationBehavior.cs b/src/N8T.Infrastructure/Validator/RequestValidationBehavior.cs index 3c7001d..3aa43f1 100644 --- a/src/N8T.Infrastructure/Validator/RequestValidationBehavior.cs +++ b/src/N8T.Infrastructure/Validator/RequestValidationBehavior.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -14,13 +16,13 @@ public class RequestValidationBehavior : IPipelineBehavior< where TRequest : notnull, IRequest where TResponse : notnull { - private readonly IValidator _validator; + private readonly IEnumerable> _validators; private readonly ILogger> _logger; - public RequestValidationBehavior(IValidator validator, + public RequestValidationBehavior(IEnumerable>? validators, ILogger> logger) { - _validator = validator ?? throw new ArgumentNullException(nameof(validator)); + _validators = validators ?? throw new ArgumentNullException(nameof(validators)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } @@ -34,12 +36,16 @@ public async Task Handle(TRequest request, _logger.LogDebug($"Handling {typeof(TRequest).FullName} with content {JsonSerializer.Serialize(request)}"); - await _validator.HandleValidation(request); - - var response = await next(); + if (_validators.Any()) + { + var context = new ValidationContext(request); + await Task.WhenAll(_validators.Select(v => v.HandleValidation(request))); + } + TResponse? response = await next(); _logger.LogInformation($"Handled {typeof(TRequest).FullName}"); return response; + } } }