Skip to content
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

@Param default validation #348

Closed
jsmonkey opened this issue Dec 20, 2017 · 10 comments · Fixed by #815
Closed

@Param default validation #348

jsmonkey opened this issue Dec 20, 2017 · 10 comments · Fixed by #815
Labels
type: question Questions about the usage of the library.

Comments

@jsmonkey
Copy link

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.

@timc13
Copy link

timc13 commented Dec 20, 2017

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) {
  }
}

@MichalLytek
Copy link
Contributor

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 😉

@MichalLytek MichalLytek added the type: question Questions about the usage of the library. label Dec 21, 2017
@jsmonkey
Copy link
Author

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

@MichalLytek
Copy link
Contributor

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

@timc13
Copy link

timc13 commented Dec 21, 2017

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

@MichalLytek
Copy link
Contributor

MichalLytek commented Dec 21, 2017

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

@jsmonkey
Copy link
Author

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.

@NoNameProvided
Copy link
Member

And the pro of open source is that you don’t 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’t forget to push your fixes upstream, thanks!

@clysto
Copy link

clysto commented Aug 10, 2020

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.
  }
}

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: question Questions about the usage of the library.
5 participants