-
Notifications
You must be signed in to change notification settings - Fork 557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When is the ValidationPipelineBehavior ever used? #24
Comments
I ran into the same issue. I debugged into it and recognized that there are two separate validations being made using the same validators. The first one is done by the mvc pipeline using FluentValidation and the ValidatorActionFilter. The second one is done by the mediatR pipeline initiated by ValidationPipelineBehavior while using the same validator objects being injected into it. Taking UsersController.Create as an example: TL;DR: ValidationPipelineBehavior makes sence if you pass a different object to mediatR than that being passed to the controller. Instead of throwing ValidationExceptions, I suggest doing the same thing as in ValidatorActionFilter: respond with Statuscode 422 and an appropriate payload explaining the problem. |
I'm not sure if we should respond with a 422 in this situation. Mediatr is designed to have multiple handlers file for a single command and commands might not always be triggered by a controller. I'm still new with Mediatr but do events use the same pipeline? |
Good point, I didn't consider the cases where MediatR is used outside of controllers. So maybe the best solution is to throw in ValidationPipelineBehavior as is and explicitly handle ValidationExceptions in ErrorHandlingMiddleware or even a dedicated middleware for this concern. This provides you with the opportunity to handle the ValidationException differently in non-controller situations. However, in such situations, you might not use any validation at all and simply would not provide any validator for this kind of command type. |
I noticed that all the validators run as expected during Model Binding (probably because of
.AddFluentValidation(cfg => { cfg.RegisterValidatorsFromAssemblyContaining<Startup>(); });
) and they never even have a chance to make it to theValidationPipelineBehavior
. I assume that is why we just throw in there because something is very wrong if we got that far.Why even have this? Just as a safety net or are there situations where this comes in handy?
The text was updated successfully, but these errors were encountered: