diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 7fea1bdf84c..17b10dab189 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -48,6 +48,20 @@ function validateAndMaskMode(value, name, def) { throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc); } +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) { // The defaults for min and max correspond to the limits of 32-bit integers. if (!isInt32(value)) { @@ -93,6 +107,7 @@ module.exports = { isInt32, isUint32, validateAndMaskMode, + validateInteger, validateInt32, validateUint32 };