How would you guys go about to validation/filtering layer?
or
Using middleware you can keep your controllers clean from validation logic.
I prefer the first method
Would be nice to have validations out of the box :)
I don't think validation should be an out-of-the-box feature, because of two reasons:
As @JulianBiermann said, you should create a middleware with the necessary validation logic.
For this purpose, Joi can be borrowed from Express :smile:
I think you can even develop a decorator for routes that can take a Joi object and apply it to the payload. However that might be a bit of overengineering, depending on your needs.
I don't think validation should be an out-of-the-box feature, because of two reasons:
It's hard to create an implementation that fits all needs.
Here you go 馃槈
https://github.com/pleerock/routing-controllers#auto-validating-action-params
@19majkel94
Sure, it looks great, but it's still one specific way to approach the problem. Others might prefer decoratorless implementations or whatever.
I'm not saying that there's no good solution at all. I'm trying to point out the fact that this is a higher-level problem (with great existing solutions) which should not be implemented at framework level so that clients must make use of that implementation, but a foundation should be provided for third-party solutions (just like the one you've linked).
which should not be implemented at framework level
Auto-validation action params ( like @Body
) is a greate feature, it's reducing boilerplate of calling validate
manually each time. It could even be generic - support other validating libraries with a config function which receive the param object and call validate
or other function from the lib.
Hi guys,
I agree with both @battila7 and @19majkel94. In my opinion auto-validation logic implemented at a framework level is not a good idea - this is why it isn't provided out-of-the-box. The validation logic always depends on a lot of factors and the main idea of Nest is to give as much flexibility as possible. However, in a lot of cases - auto-validation of actions params (just as @19majkel94 mentioned - @Body()
, @Param()
etc) is helpful and reduce boilerplate code in middlewares / controllers. Also - middleware does not have an access to metatypes of the properties, so it is not so easy to create a generic solution.
I have a concept, which should makes much easier validation / mapping stuff and it should be available in the nearest future. Stay tuned :cat:
Hi guys!
Since 3.0.0
pipes feature is available. You can use it for validation / transformation purposes :) Read more
@kamilmysliwiec <3
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Hi guys,
I agree with both @battila7 and @19majkel94. In my opinion auto-validation logic implemented at a framework level is not a good idea - this is why it isn't provided out-of-the-box. The validation logic always depends on a lot of factors and the main idea of Nest is to give as much flexibility as possible. However, in a lot of cases - auto-validation of actions params (just as @19majkel94 mentioned -
@Body()
,@Param()
etc) is helpful and reduce boilerplate code in middlewares / controllers. Also - middleware does not have an access to metatypes of the properties, so it is not so easy to create a generic solution.I have a concept, which should makes much easier validation / mapping stuff and it should be available in the nearest future. Stay tuned :cat: