0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

lib: add validateInteger() validator

This allows validation of integers that are not int32 or uint32.

PR-URL: https://github.com/nodejs/node/pull/20851
Fixes: https://github.com/nodejs/node/issues/20844
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
cjihrig 2018-05-20 11:15:00 -04:00
parent 8fac1d910f
commit f8464c8698
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -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
};