diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 556bfb2dc08f5f..aabe71ef33979a 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -13,7 +13,21 @@ function isUint32(value) { return value === (value >>> 0); } -function validateInt32(value, name) { +function validateInteger(value, name) { + let err; + + if (typeof value !== 'number') + err = new ERR_INVALID_ARG_TYPE(name, 'number', value); + else if (!Number.isSafeInteger(value)) + err = new ERR_OUT_OF_RANGE(name, 'an integer', value); + + if (err) { + Error.captureStackTrace(err, validateInteger); + throw err; + } +} + +function validateInt32(value, name, min = -2147483648, max = 2147483647) { if (!isInt32(value)) { let err; if (typeof value !== 'number') { @@ -53,6 +67,7 @@ function validateUint32(value, name, positive) { module.exports = { isInt32, isUint32, + validateInteger, validateInt32, validateUint32 };