nest icon indicating copy to clipboard operation
nest copied to clipboard

fix(common): when transforming undefined numeric values

Open Hareloo opened this issue 1 year ago • 4 comments

Transforming numeric values in validationpipe is incorrect when value is undefined

closes #12864

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [x] Bugfix
  • [ ] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

This issue occurs when using ValidationPipe with transform: true option set. When this option is set and query/param arguments on an endpoint are used that are optional/have a default value, when no value/invalid value is passed to this query/URL param, it results in a NaN value, instead of the default value or having it be set to undefined in case it's an optional param.

Issue Number: #12864

What is the new behavior?

Values of the sort mentioned will now be parsed as undefined and converted to undefined.

Does this PR introduce a breaking change?

  • [x] Yes
  • [ ] No

Hareloo avatar Dec 06 '23 19:12 Hareloo

Pull Request Test Coverage Report for Build 4421a15f-4fad-4fed-a0b6-753367b142e5

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 92.26%

Totals Coverage Status
Change from base Build 2893054b-b3fe-4fee-a01d-9c529a7f6e46: 0.002%
Covered Lines: 6699
Relevant Lines: 7261

💛 - Coveralls

coveralls avatar Dec 06 '23 19:12 coveralls

@Hareloo the same issue happens for other types such as Enums and Strings, on both @Query and @Param decorations. I'll try to provide a subset of a project I'm working on (I'm currently migrating to swc, so that's how I caught it).

Small example:

 public async get(
    @Query('status', new ParseEnumPipe(MyStatusEnum, { optional: true })) status?: MyStatusEnum,
  ): Promise<ResponseModel> {
   ...
}

https://github.com/nestjs/nest/blob/master/packages/common/pipes/parse-enum.pipe.ts

caiorosa-seenons avatar Dec 07 '23 16:12 caiorosa-seenons

Is this change still blocked?

stuarthimmer-loop avatar Aug 02 '24 18:08 stuarthimmer-loop

@stuarthimmer-loop it's a breaking change so we'll need to wait till the next major release

kamilmysliwiec avatar Aug 05 '24 11:08 kamilmysliwiec