From 310ed478591079e597905b11dc49a640212b794b Mon Sep 17 00:00:00 2001 From: Voltrex Date: Fri, 13 Aug 2021 03:11:20 +0430 Subject: [PATCH 1/3] lib: simplify validators Some of the validators can be simplified by removing unnecessary object parameters and using validators in another validators to keep consistency. --- lib/internal/validators.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 9bca0aa747725e..b3a7ee26554e1a 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -155,7 +155,7 @@ const validateObject = hideStackFrames( } }); -const validateArray = hideStackFrames((value, name, { minLength = 0 } = {}) => { +const validateArray = hideStackFrames((value, name, minLength = 0) => { if (!ArrayIsArray(value)) { throw new ERR_INVALID_ARG_TYPE(name, 'Array', value); } @@ -166,8 +166,7 @@ const validateArray = hideStackFrames((value, name, { minLength = 0 } = {}) => { }); function validateSignalName(signal, name = 'signal') { - if (typeof signal !== 'string') - throw new ERR_INVALID_ARG_TYPE(name, 'string', signal); + validateString(signal, name); if (signals[signal] === undefined) { if (signals[StringPrototypeToUpperCase(signal)] !== undefined) { @@ -199,7 +198,7 @@ function validateEncoding(data, encoding) { // Check that the port number is not NaN when coerced to a number, // is an integer and that it falls within the legal range of port numbers. -function validatePort(port, name = 'Port', { allowZero = true } = {}) { +function validatePort(port, name = 'Port', allowZero = true) { if ((typeof port !== 'number' && typeof port !== 'string') || (typeof port === 'string' && StringPrototypeTrim(port).length === 0) || +port !== (+port >>> 0) || From baa5a2f049245f6f463284ac8447587357cca8d0 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Fri, 13 Aug 2021 03:20:51 +0430 Subject: [PATCH 2/3] dgram: use simplified validator The `dgram` lib module should use the simplified `validatePort()` validator. --- lib/dgram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 4630be4dff8c5c..c515c551427d98 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -369,7 +369,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) { }; Socket.prototype.connect = function(port, address, callback) { - port = validatePort(port, 'Port', { allowZero: false }); + port = validatePort(port, 'Port', false); if (typeof address === 'function') { callback = address; address = ''; @@ -626,7 +626,7 @@ Socket.prototype.send = function(buffer, } if (!connected) - port = validatePort(port, 'Port', { allowZero: false }); + port = validatePort(port, 'Port', false); // Normalize callback so it's either a function or undefined but not anything // else. From 68e01017515236eb699c6265315e8dc71c8098c2 Mon Sep 17 00:00:00 2001 From: voltrexmaster Date: Fri, 13 Aug 2021 01:15:54 -0700 Subject: [PATCH 3/3] test: use simplfied validator The validators test should use the simplified `validateArray()` validator. --- test/parallel/test-validators.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-validators.js b/test/parallel/test-validators.js index 9dbf5f9c4599f2..6b0d49c6997a65 100644 --- a/test/parallel/test-validators.js +++ b/test/parallel/test-validators.js @@ -55,9 +55,9 @@ const invalidArgValueError = { }, invalidArgTypeError); }); - validateArray([1], 'foo', { minLength: 1 }); + validateArray([1], 'foo', 1); assert.throws(() => { - validateArray([], 'foo', { minLength: 1 }); + validateArray([], 'foo', 1); }, invalidArgValueError); }