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.
@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
Hello @jaufgang,
That's why we have param-scoped pipes.
Usage example https://github.com/nestjs/nest/blob/master/examples/01-cats-app/src/modules/cats/cats.controller.ts
Pipe https://github.com/nestjs/nest/blob/master/examples/01-cats-app/src/modules/common/pipes/parse-int.pipe.ts
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.
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