-
Type: Sub-task
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: None
Currently, users can pass in invalid input into Int32 constructor, and the inputs will be coerced to a value regardless of if the input is valid.
This subtask will create a Int32.fromString helper that will validate input for these class and throw on inputs that are not representable as Int32.
In V7, we will add this helper to the Int32 constructor when a string input is passed in, so the numeric conversion will only happen after validation.
Note: I am using the Number() string conversion docs to guide my implementation.
Use Case
As a... BSON user
I want... correct validation for when I create a int32 from a string
So that... the value in BSON matches what is actually passed in
User Impact
- Some, but will impact more in v7 follow-up
Dependencies
- none, this is a new feature
Acceptance Criteria
Implementation Requirements
- Int32.fromString(string:value): number
- General Approach
- whitespace is NOT allowed
- leading zeros are allowed
- only decimal notation is allowed
- Error Cases
- return Number(value).toString() !== value
- returns a NaN on Number(value)
- !Number.isSafeInteger
- bounds check with int32.max and min
- checks if fractional
- -0
- empty string
- No leading or trailing whitespace allowed
- If not error: return Number(value)
- General Approach
Testing Requirements
- Int32 Cases
- Success Cases:
- Max and Min: int32.max, int32.min
- 0
- default / basic case for decimal case
- Error Cases:
- Out of bounds: int32.max+1, int32.min - 1
- '-Infinity', '+Infinity'
- 'NaN'
- fraction
- -0
- non-decimal cases
- Success Cases:
Documentation Requirements
- add API docs for Int32.fromString
Follow Up Requirements
- file v7 ticket (in subtasks)