Created
June 1, 2020 18:52
-
-
Save timdows/abe49f4752fac22e9174f2bd76d315b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyCustomPipelineValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse> | |
{ | |
private readonly IEnumerable<IValidator> _validators; | |
public MyCustomPipelineValidationBehavior(IEnumerable<IValidator<TRequest>> validators) | |
{ | |
_validators = validators; | |
} | |
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | |
{ | |
var validationFailures = _validators | |
.Select(validator => validator.Validate(request)) | |
.SelectMany(validationResult => validationResult.Errors) | |
.Where(validationFailure => validationFailure != null) | |
.ToList(); | |
if (validationFailures.Any()) | |
{ | |
var error = string.Join("\r\n", validationFailures); | |
throw new MyCustomValidationException(error); | |
} | |
return next(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment