Skip to content

Commit 386d5d7

Browse files
cjihrigtargos
authored andcommitted
lib: support min/max values in validateInteger()
This commit updates validateInteger() in two ways: - Number.isInteger() is used instead of Number.isSafeInteger(). This ensures that all integer values are supported. - Minimum and maximum values are supported. They default to the min and max safe integer values, but can be customized. PR-URL: #28810 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent e334c1f commit 386d5d7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/internal/validators.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
isArrayBufferView
1414
} = require('internal/util/types');
1515
const { signals } = internalBinding('constants').os;
16+
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
1617

1718
function isInt32(value) {
1819
return value === (value | 0);
@@ -60,12 +61,16 @@ function parseMode(value, name, def) {
6061
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
6162
}
6263

63-
const validateInteger = hideStackFrames((value, name) => {
64-
if (typeof value !== 'number')
65-
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
66-
if (!Number.isSafeInteger(value))
67-
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
68-
});
64+
const validateInteger = hideStackFrames(
65+
(value, name, min = MIN_SAFE_INTEGER, max = MAX_SAFE_INTEGER) => {
66+
if (typeof value !== 'number')
67+
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
68+
if (!Number.isInteger(value))
69+
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
70+
if (value < min || value > max)
71+
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
72+
}
73+
);
6974

7075
const validateInt32 = hideStackFrames(
7176
(value, name, min = -2147483648, max = 2147483647) => {

0 commit comments

Comments
 (0)