Skip to content

Instantly share code, notes, and snippets.

@timdows
Created June 1, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdows/abe49f4752fac22e9174f2bd76d315b7 to your computer and use it in GitHub Desktop.
Save timdows/abe49f4752fac22e9174f2bd76d315b7 to your computer and use it in GitHub Desktop.
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