Routing-controllers: @Param default validation

Created on 20 Dec 2017  路  10Comments  路  Source: typestack/routing-controllers

Hi,

I found that This technique works not only with @Body but also with @Param, @QueryParam, @BodyParam and other decorators. in this validation docs article.

But by default @Param('id') id: number is not validated and accepts NaN as value in case of invalid id.
There is no validate option for @Param decorator, which mentioned in the docs.

So mb you need a fix to be with your library docs.

Thanks.

question

Most helpful comment

And the pro of open source is that you don鈥檛 have to spend weeks and months writing software that others already has written. Feel free to fork and use you own version anytime, just don鈥檛 forget to push your fixes upstream, thanks!

All 10 comments

I was confused by this also and also a follow up question: Is there anyway to validate a route param at all? Assuming route params are primitives (string/number) - is there a way to specify a class property to validate against?

Let's say I have a model:

class User {

  @IsUUID('4')
  uuid: string
}

Would it be possible to have a property selector or something like

@JsonController('/users')
class UserController {

  @Get('/:uuid')
  get (@Param('uuid', user => user.uuid) uuid: string) {
  }
}

Validation works only with classes annotated with class-validator decorators.
Extented normalization of params from path/query + validation for @Params and @QueryParams will be in 0.8.x - #315 馃槈

Merge request is in opened state since 31 Aug. When will it be released?

I have no idea, pleerock is a busy guy. You can use my fork - @19majkel94/routing-controllers :wink:

@19majkel94 Can you provide an example of how that would work in the new release with the model & controller that I posted above? I am using @IsUUID decorate from class-validator...

@JsonController('/users')
class UserController {

  @Get('/:uuid')
  get (@Params() { uuid }: User) {
    // here uuid will be valid, if not it won't enter this method and throw 400 Bad Request
  }
}

But you should create a separate class, eg.:

class GetParams{
  @IsUUID('4')
  uuid: string
}

This is the living documentation of the API - you can change the internal representation of User model and just update the get method of UserController without changing how the API looks from outside.

That's the cons of opensource. It's normal to release the product which is not working according to the docs with bugs and no one knows when the fixes will arrive. Anyways thanks for the suggestions.

And the pro of open source is that you don鈥檛 have to spend weeks and months writing software that others already has written. Feel free to fork and use you own version anytime, just don鈥檛 forget to push your fixes upstream, thanks!

just use:

@JsonController('/users')
class UserController {

  @Get('/:uuid(\\d+)')
  get (@Param('uuid') uuid: number) {
    // if the uuid does't match `\d+` the request will not go into this handler, and express will throw a 404 page.
  }
}

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hellraisercenobit picture hellraisercenobit  路  4Comments

AnthoniG picture AnthoniG  路  4Comments

szabobar picture szabobar  路  4Comments

d-bechtel picture d-bechtel  路  6Comments

circy picture circy  路  3Comments