Created
December 20, 2019 16:17
-
-
Save jbogard/aebd4521aee6843316a07a4a4a288d01 to your computer and use it in GitHub Desktop.
Revisions
-
jbogard created this gist
Dec 20, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IValidatable { private readonly IEnumerable<IValidator<T>> _validators; public ValidatorBehavior(IEnumerable<IValidator<T>> validators) => _validators = validators public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) { var results = _validators.Select(v => v.Validate(request)).ToList(); if (results.Any(result => result.Invalid)) { throw new ValidationException(result); } return next(); } }