diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index e23a55e9a..5f88919b0 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -64,6 +64,9 @@ const prepareValue = function (val, seen) { return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params } if (isDate(val)) { + if (isNaN(val.getTime())) { + throw new Error('date is invalid') + } if (defaults.parseInputDatesAsUTC) { return dateToStringUTC(val) } else { diff --git a/packages/pg/test/unit/utils-tests.js b/packages/pg/test/unit/utils-tests.js index 5f75f6c2d..e81ee729c 100644 --- a/packages/pg/test/unit/utils-tests.js +++ b/packages/pg/test/unit/utils-tests.js @@ -94,6 +94,22 @@ test('prepareValues: undefined prepared properly', function () { assert.strictEqual(out, null) }) +test('prepareValues: invalid date throws an error', function () { + const invalidDate = new Date(undefined) + assert.throws( + () => utils.prepareValue(invalidDate), + /date is invalid/ + ) +}) + +test('prepareValues: invalid date (NaN) does not produce NaN string', function () { + const invalidDate = new Date('not a date') + assert.throws( + () => utils.prepareValue(invalidDate), + /date is invalid/ + ) +}) + test('prepareValue: null prepared properly', function () { const out = utils.prepareValue(null) assert.strictEqual(out, null)