Skip to content

Commit c3f0e7f

Browse files
committed
lib: update according to feedback
1 parent 3b6d180 commit c3f0e7f

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

lib/internal/validators.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const {
3535
const { signals } = internalBinding('constants').os;
3636

3737
/**
38-
* @callback isInt32
3938
* @param {number} value
4039
* @returns {boolean}
4140
*/
@@ -44,7 +43,6 @@ function isInt32(value) {
4443
}
4544

4645
/**
47-
* @callback isUint32
4846
* @param {number} value
4947
* @returns {boolean}
5048
*/
@@ -179,7 +177,7 @@ function validateNumber(value, name, min = undefined, max) {
179177
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
180178

181179
if ((min != null && value < min) || (max != null && value > max) ||
182-
((min != null || max != null) && NumberIsNaN(value))) {
180+
((min != null || max != null) && NumberIsNaN(value))) {
183181
throw new ERR_OUT_OF_RANGE(
184182
name,
185183
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`,
@@ -230,7 +228,11 @@ function getOwnPropertyValueOrDefault(options, key, defaultValue) {
230228
* @callback validateObject
231229
* @param {*} value
232230
* @param {string} name
233-
* @param {{allowArray: boolean=, allowFunction: boolean=, nullable: boolean=}} [options]
231+
* @param {{
232+
* allowArray: boolean=,
233+
* allowFunction: boolean=,
234+
* nullable: boolean=
235+
* }} [options]
234236
*/
235237

236238
/** @type {validateObject} */
@@ -240,10 +242,10 @@ const validateObject = hideStackFrames(
240242
const allowFunction = getOwnPropertyValueOrDefault(options, 'allowFunction', false);
241243
const nullable = getOwnPropertyValueOrDefault(options, 'nullable', false);
242244
if ((!nullable && value === null) ||
243-
(!allowArray && ArrayIsArray(value)) ||
244-
(typeof value !== 'object' && (
245-
!allowFunction || typeof value !== 'function'
246-
))) {
245+
(!allowArray && ArrayIsArray(value)) ||
246+
(typeof value !== 'object' && (
247+
!allowFunction || typeof value !== 'function'
248+
))) {
247249
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
248250
}
249251
});
@@ -253,7 +255,7 @@ const validateObject = hideStackFrames(
253255
* @param {*} value
254256
* @param {string} name
255257
* @param {number} [minLength]
256-
* @returns {asserts value is unknown[]}
258+
* @returns {asserts value is any[]}
257259
*/
258260

259261
/** @type {validateArray} */
@@ -270,24 +272,23 @@ const validateArray = hideStackFrames((value, name, minLength = 0) => {
270272
});
271273

272274
/**
273-
* @callback validateSignalName
274275
* @param {*} signal
275276
* @param {string} name
276277
* @returns {asserts signal is keyof signals}
277278
*/
278-
279-
/** @type {validateSignalName} */
280279
function validateSignalName(signal, name = 'signal') {
281280
validateString(signal, name);
282281

283282
if (signals[signal] === undefined) {
284283
if (signals[StringPrototypeToUpperCase(signal)] !== undefined) {
285284
throw new ERR_UNKNOWN_SIGNAL(signal +
286-
' (signals must use all capital letters)');
285+
' (signals must use all capital letters)');
287286
}
288287

289288
throw new ERR_UNKNOWN_SIGNAL(signal);
290289
}
290+
291+
return true;
291292
}
292293

293294
/**
@@ -299,29 +300,28 @@ function validateSignalName(signal, name = 'signal') {
299300
/** @type {validateBuffer} */
300301
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
301302
if (!isArrayBufferView(buffer)) {
302-
throw new ERR_INVALID_ARG_TYPE(name, ['Buffer', 'TypedArray', 'DataView'], buffer);
303+
throw new ERR_INVALID_ARG_TYPE(name,
304+
['Buffer', 'TypedArray', 'DataView'],
305+
buffer);
303306
}
304307
});
305308

306309
/**
307-
* @callback validateEncoding
308310
* @param {string} data
309311
* @param {string} encoding
310312
*/
311-
312-
/** @type {validateEncoding} */
313313
function validateEncoding(data, encoding) {
314314
const normalizedEncoding = normalizeEncoding(encoding);
315315
const length = data.length;
316316

317317
if (normalizedEncoding === 'hex' && length % 2 !== 0) {
318-
throw new ERR_INVALID_ARG_VALUE('encoding', encoding, `is invalid for data of length ${length}`);
318+
throw new ERR_INVALID_ARG_VALUE('encoding', encoding,
319+
`is invalid for data of length ${length}`);
319320
}
320321
}
321322

322323
/**
323-
* @callback validatePort
324-
* @description Check that the port number is not NaN when coerced to a number,
324+
* Check that the port number is not NaN when coerced to a number,
325325
* is an integer and that it falls within the legal range of port numbers.
326326
* @param {*} port
327327
* @param {string} [name='Port']
@@ -330,25 +330,27 @@ function validateEncoding(data, encoding) {
330330
*/
331331
function validatePort(port, name = 'Port', allowZero = true) {
332332
if ((typeof port !== 'number' && typeof port !== 'string') ||
333-
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
334-
+port !== (+port >>> 0) ||
335-
port > 0xFFFF ||
336-
(port === 0 && !allowZero)) {
333+
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
334+
+port !== (+port >>> 0) ||
335+
port > 0xFFFF ||
336+
(port === 0 && !allowZero)) {
337337
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
338338
}
339339
return port | 0;
340340
}
341341

342342
/**
343343
* @callback validateAbortSignal
344-
* @param {string} signal
344+
* @param {string=} signal
345345
* @param {string} name
346346
*/
347+
348+
/** @type {validateAbortSignal} */
347349
const validateAbortSignal = hideStackFrames((signal, name) => {
348350
if (signal !== undefined &&
349-
(signal === null ||
350-
typeof signal !== 'object' ||
351-
!('aborted' in signal))) {
351+
(signal === null ||
352+
typeof signal !== 'object' ||
353+
!('aborted' in signal))) {
352354
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal);
353355
}
354356
});
@@ -393,14 +395,11 @@ const validateUndefined = hideStackFrames((value, name) => {
393395
});
394396

395397
/**
396-
* @callback validateUnion
397398
* @template T
398399
* @param {T} value
399400
* @param {string} name
400401
* @param {T[]} union
401402
*/
402-
403-
/** @type {validateUnion} */
404403
function validateUnion(value, name, union) {
405404
if (!ArrayPrototypeIncludes(union, value)) {
406405
throw new ERR_INVALID_ARG_TYPE(name, `('${ArrayPrototypeJoin(union, '|')}')`, value);

0 commit comments

Comments
 (0)