添加命令数据验证和其他杂项修改
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using FluentValidation;
|
||||
using Domain.Exceptions;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QRCodeService.Extensions;
|
||||
@@ -11,11 +12,11 @@ namespace QRCodeService.Application.Behaviors
|
||||
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
{
|
||||
private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> _logger;
|
||||
private readonly IValidator<TRequest>[] _validators;
|
||||
private readonly IValidator<TRequest> _validator;
|
||||
|
||||
public ValidatorBehavior(IValidator<TRequest>[] validators, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
|
||||
public ValidatorBehavior(IValidator<TRequest> validator, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
|
||||
{
|
||||
_validators = validators;
|
||||
_validator = validator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -25,18 +26,14 @@ namespace QRCodeService.Application.Behaviors
|
||||
|
||||
_logger.LogInformation("----- Validating command {CommandType}", typeName);
|
||||
|
||||
var failures = _validators
|
||||
.Select(v => v.Validate(request))
|
||||
.SelectMany(result => result.Errors)
|
||||
.Where(error => error != null)
|
||||
.ToList();
|
||||
var failures = _validator.Validate(request).Errors;
|
||||
|
||||
if (failures.Any())
|
||||
{
|
||||
_logger.LogWarning("Validation errors - {CommandType} - Command: {@Command} - Errors: {@ValidationErrors}", typeName, request, failures);
|
||||
|
||||
//throw new OrderingDomainException(
|
||||
// $"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures));
|
||||
throw new DomainException(
|
||||
$"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures));
|
||||
}
|
||||
|
||||
return await next();
|
||||
|
||||
Reference in New Issue
Block a user