Skip to content

Commit a0cfb0c

Browse files
cjihrigcodebytere
authored andcommitted
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]> Backport-PR-URL: #21171 Co-authored-by: Shelley Vohr <[email protected]>
1 parent 6a098ad commit a0cfb0c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/internal/validators.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ function isUint32(value) {
1313
return value === (value >>> 0);
1414
}
1515

16-
function validateInt32(value, name) {
16+
function validateInteger(value, name) {
17+
let err;
18+
19+
if (typeof value !== 'number')
20+
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
21+
else if (!Number.isSafeInteger(value))
22+
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
23+
24+
if (err) {
25+
Error.captureStackTrace(err, validateInteger);
26+
throw err;
27+
}
28+
}
29+
30+
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
1731
if (!isInt32(value)) {
1832
let err;
1933
if (typeof value !== 'number') {
@@ -53,6 +67,7 @@ function validateUint32(value, name, positive) {
5367
module.exports = {
5468
isInt32,
5569
isUint32,
70+
validateInteger,
5671
validateInt32,
5772
validateUint32
5873
};

0 commit comments

Comments
 (0)