Skip to content

fix: Check if a number is NaN in ActionParameterHandler #739

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

Closed
wants to merge 2 commits into from
Closed

fix: Check if a number is NaN in ActionParameterHandler #739

wants to merge 2 commits into from

Conversation

borjapazr
Copy link
Contributor

Description

@Param('id') id: number is not validated and accepts NaN as value in case of invalid id.

For example, if the following URL is accessed (http://example.com/users/asdf), instead of throwing an error, the id value is parsed to NaN. InvalidParamError(value, parameterName, parameterType) should be thrown instead.

The problem is due to how the checking of the result of the cast operation is done. NaN is not equal to NaN. Because of this it is necessary to use Number.isNaN().

Example

@JsonController('/users')
class UserController {

  @Get('/:id')
  get (@Param('id') id: number) {
    console.log(id);
  }
}

Result

GET http://example.com/users/1234 -> 1234
GET http://example.com/users/asdf -> NaN

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #348

Fix string to number normalization using Number.isNaN() instead of ===
@borjapazr borjapazr changed the title fix(validation): Check if a number is NaN in ActionParameterHandler fix: Check if a number is NaN in ActionParameterHandler Jul 1, 2021
@borjapazr borjapazr changed the title fix: Check if a number is NaN in ActionParameterHandler fix(validation): Check if a number is NaN in ActionParameterHandler Jul 1, 2021
@borjapazr
Copy link
Contributor Author

Hi! Could you review this PR, please? 😊

@borjapazr borjapazr changed the title fix(validation): Check if a number is NaN in ActionParameterHandler fix: Check if a number is NaN in ActionParameterHandler Dec 2, 2021
@borjapazr borjapazr closed this Dec 2, 2021
@borjapazr
Copy link
Contributor Author

Another updated PR has been created #815

@github-actions
Copy link

github-actions bot commented Jan 2, 2022

This pull request 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 Jan 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

@Param default validation
2 participants