mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
lib: improve the coverage of the validator
PR-URL: https://github.com/nodejs/node/pull/42443 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
@@ -82,10 +82,10 @@ const validateInteger = hideStackFrames(
|
||||
const validateInt32 = hideStackFrames(
|
||||
(value, name, min = -2147483648, max = 2147483647) => {
|
||||
// The defaults for min and max correspond to the limits of 32-bit integers.
|
||||
if (typeof value !== 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||
}
|
||||
if (!isInt32(value)) {
|
||||
if (typeof value !== 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||
}
|
||||
if (!NumberIsInteger(value)) {
|
||||
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
|
||||
}
|
||||
@@ -98,10 +98,10 @@ const validateInt32 = hideStackFrames(
|
||||
);
|
||||
|
||||
const validateUint32 = hideStackFrames((value, name, positive) => {
|
||||
if (typeof value !== 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||
}
|
||||
if (!isUint32(value)) {
|
||||
if (typeof value !== 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||
}
|
||||
if (!NumberIsInteger(value)) {
|
||||
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ const {
|
||||
validateNumber,
|
||||
validateObject,
|
||||
validateString,
|
||||
validateInt32,
|
||||
validateUint32,
|
||||
} = require('internal/validators');
|
||||
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
|
||||
const outOfRangeError = {
|
||||
@@ -41,6 +43,34 @@ const invalidArgValueError = {
|
||||
// validateInteger() works with unsafe integers.
|
||||
validateInteger(MAX_SAFE_INTEGER + 1, 'foo', 0, MAX_SAFE_INTEGER + 1);
|
||||
validateInteger(MIN_SAFE_INTEGER - 1, 'foo', MIN_SAFE_INTEGER - 1);
|
||||
|
||||
// validateInt32() and validateUint32()
|
||||
[
|
||||
Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1',
|
||||
].forEach((val) => assert.throws(() => validateInt32(val, 'name'), {
|
||||
code: 'ERR_INVALID_ARG_TYPE'
|
||||
}));
|
||||
[
|
||||
2147483647 + 1, -2147483648 - 1, NaN,
|
||||
].forEach((val) => assert.throws(() => validateInt32(val, 'name'), {
|
||||
code: 'ERR_OUT_OF_RANGE'
|
||||
}));
|
||||
[
|
||||
0, 1, -1,
|
||||
].forEach((val) => validateInt32(val, 'name'));
|
||||
[
|
||||
Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1',
|
||||
].forEach((val) => assert.throws(() => validateUint32(val, 'name'), {
|
||||
code: 'ERR_INVALID_ARG_TYPE'
|
||||
}));
|
||||
[
|
||||
4294967296, -1, NaN,
|
||||
].forEach((val) => assert.throws(() => validateUint32(val, 'name'), {
|
||||
code: 'ERR_OUT_OF_RANGE'
|
||||
}));
|
||||
[
|
||||
0, 1,
|
||||
].forEach((val) => validateUint32(val, 'name'));
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user