From 35814f437c6b59d4a4c9d760b9db142f7b87fd0c Mon Sep 17 00:00:00 2001 From: yaya Date: Wed, 23 Mar 2022 14:58:23 +0800 Subject: [PATCH 1/3] lib: improve the coverage of the validator --- lib/internal/validators.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index b07eebfbd8b1a0..3422b4efb1dc89 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -82,6 +82,9 @@ 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 == 'symbol' || typeof value == 'bigint') { + 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); @@ -98,6 +101,9 @@ const validateInt32 = hideStackFrames( ); const validateUint32 = hideStackFrames((value, name, positive) => { + if (typeof value == 'symbol' || typeof value == 'bigint') { + 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); From 82888c0bedf054efda4d94ed9dac6e2a5da18a40 Mon Sep 17 00:00:00 2001 From: yaya Date: Wed, 23 Mar 2022 19:50:28 +0800 Subject: [PATCH 2/3] add some tests --- lib/internal/validators.js | 10 ++-------- test/parallel/test-validators.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 3422b4efb1dc89..e3584aaf2e13f7 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -82,13 +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 == 'symbol' || typeof value == 'bigint') { + 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); } @@ -101,13 +98,10 @@ const validateInt32 = hideStackFrames( ); const validateUint32 = hideStackFrames((value, name, positive) => { - if (typeof value == 'symbol' || typeof value == 'bigint') { + 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); } diff --git a/test/parallel/test-validators.js b/test/parallel/test-validators.js index 6b0d49c6997a65..9ce5826939147f 100644 --- a/test/parallel/test-validators.js +++ b/test/parallel/test-validators.js @@ -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,28 @@ 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, + ].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' + })); + [ + Symbol(), 1n, {}, [], false, true, undefined, null, + ].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' + })); } { From 796a7487d87d21dad6906e20394008ae3139a797 Mon Sep 17 00:00:00 2001 From: yaya Date: Fri, 25 Mar 2022 15:46:43 +0800 Subject: [PATCH 3/3] add test case --- test/parallel/test-validators.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-validators.js b/test/parallel/test-validators.js index 9ce5826939147f..0bba9d13b20bf0 100644 --- a/test/parallel/test-validators.js +++ b/test/parallel/test-validators.js @@ -46,7 +46,7 @@ const invalidArgValueError = { // validateInt32() and validateUint32() [ - Symbol(), 1n, {}, [], false, true, undefined, null, + Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1', ].forEach((val) => assert.throws(() => validateInt32(val, 'name'), { code: 'ERR_INVALID_ARG_TYPE' })); @@ -56,7 +56,10 @@ const invalidArgValueError = { code: 'ERR_OUT_OF_RANGE' })); [ - Symbol(), 1n, {}, [], false, true, undefined, null, + 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' })); @@ -65,6 +68,9 @@ const invalidArgValueError = { ].forEach((val) => assert.throws(() => validateUint32(val, 'name'), { code: 'ERR_OUT_OF_RANGE' })); + [ + 0, 1, + ].forEach((val) => validateUint32(val, 'name')); } {