-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: BSON
Use Case
As a BSON user
I want numeric constructors to throw on malformed input
So that the value I input is the value that is stored
Problem
The Int32, Double and Long constructors silently coerce invalid strings: new Int32("abc") gives 0 (via +value | 0), new Double("abc") gives NaN (via +value), new Long("abc") gives 0.
Long.fromString is worse, because it parses the input in 8-digit chunks, so the result depends on where the invalid characters land:
Long.fromString("1234xxx5") // 1234 Long.fromString("1234xxxx5") // 12345 Long.fromString("1234xxxxx5") // 123400
Reported externally in NODE-3660 (2021) and NODE-5638 (2023, closed as a duplicate of this ticket).
No deprecation is needed. The opt-in strict replacements already ship: Int32.fromString and Double.fromString in bson 6.6.0, Long.fromStringStrict in 6.7.0. This ticket makes them the default.
Acceptance Criteria
Implementation Requirements
- The Int32 and Double constructors delegate to their fromString helpers
- Move Long.fromStringStrict behavior into Long.fromString, which is also reached through the Long string constructor overload and Long.fromValue
- Validation must cover radix 2 through 36. Long.fromExtendedJSON has a decimal-only regex that cannot be reused
- Align the Double constructor signature, currently typed value: number but accepting strings at runtime, while Int32 is typed number | string
Testing Requirements
- Update constructor tests for the new error cases
- Benchmark Long.fromString. The 8-digit chunking exists to avoid expensive emulated multiplication, so adding validation to a hot path needs measuring
Documentation Requirements
- Update the API docs for each constructor