diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index 638b43970..e7b7af06f 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -54,6 +54,9 @@ const prepareValue = function (val, seen) { return Buffer.from(val.buffer, val.byteOffset, val.byteLength) } if (isDate(val)) { + if (isNaN(val.getTime())) { + throw new Error('Invalid Date value provided to query parameter') + } 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..825b5580f 100644 --- a/packages/pg/test/unit/utils-tests.js +++ b/packages/pg/test/unit/utils-tests.js @@ -78,6 +78,18 @@ test('prepareValues: BC date prepared properly', function () { helper.resetTimezoneOffset() }) +test('prepareValues: invalid date throws error', function () { + assert.throws(function () { + utils.prepareValue(new Date(undefined)) + }, /Invalid Date/) + assert.throws(function () { + utils.prepareValue(new Date(NaN)) + }, /Invalid Date/) + assert.throws(function () { + utils.prepareValue(new Date('not a date')) + }, /Invalid Date/) +}) + test('prepareValues: 1 BC date prepared properly', function () { helper.setTimezoneOffset(-330)