Skip to content

Commit 3ee5434

Browse files
mawaregetsukajuanarbol
authored andcommitted
lib: improve the coverage of the validator
PR-URL: #42443 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d71df7a commit 3ee5434

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lib/internal/validators.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ const validateInteger = hideStackFrames(
8383
const validateInt32 = hideStackFrames(
8484
(value, name, min = -2147483648, max = 2147483647) => {
8585
// The defaults for min and max correspond to the limits of 32-bit integers.
86+
if (typeof value !== 'number') {
87+
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
88+
}
8689
if (!isInt32(value)) {
87-
if (typeof value !== 'number') {
88-
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
89-
}
9090
if (!NumberIsInteger(value)) {
9191
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
9292
}
@@ -99,10 +99,10 @@ const validateInt32 = hideStackFrames(
9999
);
100100

101101
const validateUint32 = hideStackFrames((value, name, positive) => {
102+
if (typeof value !== 'number') {
103+
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
104+
}
102105
if (!isUint32(value)) {
103-
if (typeof value !== 'number') {
104-
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
105-
}
106106
if (!NumberIsInteger(value)) {
107107
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
108108
}

test/parallel/test-validators.js

+30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
validateNumber,
1111
validateObject,
1212
validateString,
13+
validateInt32,
14+
validateUint32,
1315
} = require('internal/validators');
1416
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
1517
const outOfRangeError = {
@@ -41,6 +43,34 @@ const invalidArgValueError = {
4143
// validateInteger() works with unsafe integers.
4244
validateInteger(MAX_SAFE_INTEGER + 1, 'foo', 0, MAX_SAFE_INTEGER + 1);
4345
validateInteger(MIN_SAFE_INTEGER - 1, 'foo', MIN_SAFE_INTEGER - 1);
46+
47+
// validateInt32() and validateUint32()
48+
[
49+
Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1',
50+
].forEach((val) => assert.throws(() => validateInt32(val, 'name'), {
51+
code: 'ERR_INVALID_ARG_TYPE'
52+
}));
53+
[
54+
2147483647 + 1, -2147483648 - 1, NaN,
55+
].forEach((val) => assert.throws(() => validateInt32(val, 'name'), {
56+
code: 'ERR_OUT_OF_RANGE'
57+
}));
58+
[
59+
0, 1, -1,
60+
].forEach((val) => validateInt32(val, 'name'));
61+
[
62+
Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1',
63+
].forEach((val) => assert.throws(() => validateUint32(val, 'name'), {
64+
code: 'ERR_INVALID_ARG_TYPE'
65+
}));
66+
[
67+
4294967296, -1, NaN,
68+
].forEach((val) => assert.throws(() => validateUint32(val, 'name'), {
69+
code: 'ERR_OUT_OF_RANGE'
70+
}));
71+
[
72+
0, 1,
73+
].forEach((val) => validateUint32(val, 'name'));
4474
}
4575

4676
{

0 commit comments

Comments
 (0)