nest icon indicating copy to clipboard operation
nest copied to clipboard

Validation/Filtering layer.

Open lsilva-kununu opened this issue 9 years ago • 10 comments

How would you guys go about to validation/filtering layer?

lsilva-kununu avatar May 18 '17 12:05 lsilva-kununu

  1. Create validation middleware which is applied to a controller

or

  1. Check parameters, body content etc. in the controller itself.

Using middleware you can keep your controllers clean from validation logic.

I prefer the first method

JulianRuhrDev avatar May 18 '17 14:05 JulianRuhrDev

Would be nice to have validations out of the box :)

lsilva-kununu avatar May 18 '17 15:05 lsilva-kununu

I don't think validation should be an out-of-the-box feature, because of two reasons:

  1. It's hard to create an implementation that fits all needs.
  2. Middlewares are easy to implement and provide more flexibility.

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.

battila7 avatar May 18 '17 20:05 battila7

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

MichalLytek avatar May 21 '17 16:05 MichalLytek

@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).

battila7 avatar May 22 '17 10:05 battila7

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.

MichalLytek avatar May 22 '17 20:05 MichalLytek

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:

kamilmysliwiec avatar May 22 '17 22:05 kamilmysliwiec

Hi guys! Since 3.0.0 pipes feature is available. You can use it for validation / transformation purposes :) Read more

kamilmysliwiec avatar Jun 03 '17 23:06 kamilmysliwiec

@kamilmysliwiec <3

lucasmonstrox avatar Sep 09 '17 19:09 lucasmonstrox

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.

lock[bot] avatar Sep 25 '19 12:09 lock[bot]