Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: simplify validators #39753

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) ||
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down