Nest: parse params and

Created on 16 Sep 2017  路  7Comments  路  Source: nestjs/nest

is there a way to specify that Params and Query be injected as a number instead of a string?

@Get("validate/:code?")
codes(@Param("code") code : number) {
    return ValidationService.validate(number); // error: currently gets passed as a string
}

I know I can use parseInt(code) but i am wondering if there is a more elegant way of specifying to the injector to return the param as a number.

Most helpful comment

Since the directory structure of the repository has changed, these are the new links:

_Usage example_
https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/cats/cats.controller.ts
_Pipe (which is already integrated into Nest.js)_
https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/common/pipes/parse-int.pipe.ts

All 7 comments

@jaufgang I guess the only way here is a Interceptor

It would be nice if this could be added as a feature... maybe add an optional ParamType argument to the Param decorator.

@Get("validate/:code?")
codes(@Param("code", ParamType.number) code : number) {
    return ValidationService.validate(number); 
}

This could work the same way for the Query,Body decorators

@jaufgang then check this http://docs.nestjs.com/pipes it should cover your case

Ok great. Thank you.

Since the directory structure of the repository has changed, these are the new links:

_Usage example_
https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/cats/cats.controller.ts
_Pipe (which is already integrated into Nest.js)_
https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/common/pipes/parse-int.pipe.ts

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JulianBiermann picture JulianBiermann  路  3Comments

menme95 picture menme95  路  3Comments

FranciZ picture FranciZ  路  3Comments

janckerchen picture janckerchen  路  3Comments

thohoh picture thohoh  路  3Comments