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:
parent
8fac1d910f
commit
f8464c8698
@ -48,6 +48,20 @@ function validateAndMaskMode(value, name, def) {
|
|||||||
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
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) {
|
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
|
||||||
// The defaults for min and max correspond to the limits of 32-bit integers.
|
// The defaults for min and max correspond to the limits of 32-bit integers.
|
||||||
if (!isInt32(value)) {
|
if (!isInt32(value)) {
|
||||||
@ -93,6 +107,7 @@ module.exports = {
|
|||||||
isInt32,
|
isInt32,
|
||||||
isUint32,
|
isUint32,
|
||||||
validateAndMaskMode,
|
validateAndMaskMode,
|
||||||
|
validateInteger,
|
||||||
validateInt32,
|
validateInt32,
|
||||||
validateUint32
|
validateUint32
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user