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

Parse int pipe improvement #385

Merged
merged 3 commits into from
Feb 5, 2018

Conversation

oschweitzer
Copy link
Contributor

TLDR; This PR improves the ParseIntPipe validation.

Motivation

Take the following code base:

// sample.controller.ts
//...
  @Get(':id')
  async getById(
    @Param('id', new ParseIntPipe()) id: number,
  ) {
      console.log(id);
  }
//...

If a user sends the following HTTP request GET http://localhost:3000/users/1abc, the current parseIntPipe will validate the parameter. I think it's not the expected behaviour.

Proposal

This PR will improve the parseIntPipe by using the rxjs function isNumeric to be sure that the input string is only composed of numbers.
rxjs being already a dependency of nestjs, this PR doesn't add a new dependency.

@coveralls
Copy link

coveralls commented Jan 30, 2018

Coverage Status

Coverage increased (+0.006%) to 94.559% when pulling e089a47 on oschweitzer:parseInt-pipe-improvement into 1812567 on nestjs:master.

@kamilmysliwiec
Copy link
Member

Hi @oschweitzer, thanks for PR!
However, I don't think it's a good idea to reuse functions from internal API of other libraries.

@oschweitzer
Copy link
Contributor Author

Sure you're right, I propose to change the condition to:

    if ('string' === typeof value && !isNaN(parseFloat(value)) && isFinite(value as any)) {
     return parseInt(value, 10);
    }

And therefore to remove the rxjs import, is it ok for you ?

@kamilmysliwiec
Copy link
Member

@oschweitzer sounds good!

This commit also removes the unused rxjs import
@kamilmysliwiec kamilmysliwiec merged commit e089a47 into nestjs:master Feb 5, 2018
@kamilmysliwiec
Copy link
Member

Great job @oschweitzer! Thanks 🙂

@amitport
Copy link
Contributor

amitport commented Feb 5, 2018

this will map the following to the same value:
111
<spaces>111<spaces> (how do you inline space with quoted code in GFM?)
111.43
111.234
please consider the implementation in #370

@lock
Copy link

lock bot commented Sep 24, 2019

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 lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants