Skip to content

Commit f8464c8

Browse files
committed
lib: add validateInteger() validator
This allows validation of integers that are not int32 or uint32. PR-URL: #20851 Fixes: #20844 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 8fac1d9 commit f8464c8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/internal/validators.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ function validateAndMaskMode(value, name, def) {
4848
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
4949
}
5050

51+
function validateInteger(value, name) {
52+
let err;
53+
54+
if (typeof value !== 'number')
55+
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
56+
else if (!Number.isSafeInteger(value))
57+
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
58+
59+
if (err) {
60+
Error.captureStackTrace(err, validateInteger);
61+
throw err;
62+
}
63+
}
64+
5165
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
5266
// The defaults for min and max correspond to the limits of 32-bit integers.
5367
if (!isInt32(value)) {
@@ -93,6 +107,7 @@ module.exports = {
93107
isInt32,
94108
isUint32,
95109
validateAndMaskMode,
110+
validateInteger,
96111
validateInt32,
97112
validateUint32
98113
};

0 commit comments

Comments
 (0)