class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

Patch IsNumber decorator with maxDecimalPlaces option set for numbers in scientific annotation

Open backnight opened this issue 1 year ago • 1 comments

Description

The IsNumber() decorator doesn't work on scientifically annotated numbers when specifying maxDecimalPlaces option because value.toString() has no dot in it and crashes (see issue: https://github.com/typestack/class-validator/issues/1705).

https://github.com/typestack/class-validator/blob/4a0ec6680161fc611ba9fdea74fd34653c3997e6/src/decorator/typechecker/IsNumber.ts#L34

This patch reworks the whole handling of maxDecimalPlaces option in order to make it work with scientific numbers.

The logic behind the patch is toFixed(maxDecimalPlaces) the input so it is represented as a string with a maximum number of decimals as specified by the option maxDecimalPlaces, then parseFloat() it to be compared with the original input. If there is a difference with the original input, it means it has a number of decimals > maxDecimalPlaces because they have been truncated by toFixed(maxDecimalPlaces)

Checklist

  • [x] the pull request title describes what this PR does (not a vague title like Update index.md)
  • [x] 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
  • [x] I have run the project locally and verified that there are no errors

Fixes

fixes #1705

backnight avatar Jul 16 '23 14:07 backnight

also waiting... 😒

maierthomas avatar May 08 '24 09:05 maierthomas