Coder Social home page Coder Social logo

Comments (3)

domas-st avatar domas-st commented on July 24, 2024

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:
Before UsersController.Create is called, the mvc validation validates the Conduit.Features.Users.Create.Command object. If it fails ValidatorActionFilter will respond with Statuscode 422. However, if it does not fail, the object is being handed over to the mediatR by calling Send(). Now the second validation issued by ValidationPipelineBehavior is taking place. As this validates the same object using the same validators, the validation will never fail.
So yes in this case, ValidationPipelineBehavior is useless. However, if you pass a different object to mediatR than that being passed to the controller, the second validation makes sence again.
But here, imho, lies the actual problem: ValidationPipelineBehavior throws ValidationExceptions which are not being handled appropriately. Instead, the exception ends up being handled in the ErrorHandlingMiddleware which would respond with Statuscode 500.

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.

from aspnetcore-realworld-example-app.

VictorioBerra avatar VictorioBerra commented on July 24, 2024

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?

from aspnetcore-realworld-example-app.

domas-st avatar domas-st commented on July 24, 2024

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.
Btw, MediatR doesn't use PipelineBehaviors when you call Publish() instead of Send(). Just compare NotificationHandlerWrapper and RequestHandlerWrapper.

from aspnetcore-realworld-example-app.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.