Skip to content

fix(pg): throw on invalid Date instead of serializing NaN string#3668

Closed
terminalchai wants to merge 1 commit into
brianc:masterfrom
terminalchai:fix/invalid-date-serialization
Closed

fix(pg): throw on invalid Date instead of serializing NaN string#3668
terminalchai wants to merge 1 commit into
brianc:masterfrom
terminalchai:fix/invalid-date-serialization

Conversation

@terminalchai
Copy link
Copy Markdown

Fixes #3318

When prepareValue() receives an invalid Date (e.g.
ew Date(undefined) or
ew Date('invalid')), getTime() returns NaN. The existing code passes such a Date straight to dateToString() / dateToStringUTC(), which formats each NaN component with padStart(), producing the meaningless string �NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN that Postgres cannot parse.

Fix

Add an isNaN(val.getTime()) guard in the isDate branch of prepareValue() and throw an informative error immediately, so callers discover the bug at the JS level rather than receiving a cryptic Postgres error.

Changes

  • packages/pg/lib/utils.js: add isNaN guard and throw TypeError
  • packages/pg/test/unit/utils-tests.js: add two unit test cases for invalid Date

new Date(undefined) and new Date('invalid') produce an invalid Date
object whose getTime() returns NaN.  Previously prepareValue() would
pass such a date straight through to dateToString() / dateToStringUTC()
which format each NaN component with padStart(), producing the
meaningless string '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN' that Postgres
cannot parse.

Add an isNaN(val.getTime()) guard in the isDate branch of prepareValue()
and throw an informative error immediately, so callers discover the bug
at the JS level rather than receiving a cryptic Postgres error.

Fixes brianc#3318
@charmander charmander closed this May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not serialize new Date(undefined) as "0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN"

2 participants