binhend 2.3.5 → 2.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -10,6 +10,17 @@ const types = require('@binhend/types');
|
|
|
10
10
|
* @property {*=} default - The default value being used if null or undefined
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Type options used in validation functions.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} input - Options for validation.
|
|
17
|
+
* @returns {Options}
|
|
18
|
+
* @throws {HttpError}
|
|
19
|
+
*/
|
|
20
|
+
function Options(input = {}) {
|
|
21
|
+
return input;
|
|
22
|
+
}
|
|
23
|
+
|
|
13
24
|
/**
|
|
14
25
|
* Throw error by default for invalid input.
|
|
15
26
|
* A `HttpError` is thrown for `router` sending response to client.
|
|
@@ -287,7 +298,7 @@ function AsyncFunction(input, options = {}) {
|
|
|
287
298
|
return Validator(input, (input) => input instanceof AsyncFunctionConstructor, { ...options, type: 'async function' });
|
|
288
299
|
}
|
|
289
300
|
|
|
290
|
-
const AsyncFunctionConstructor = global.Object.getPrototypeOf(async function() {}).constructor;
|
|
301
|
+
const AsyncFunctionConstructor = global.Object.getPrototypeOf(async function () { }).constructor;
|
|
291
302
|
|
|
292
303
|
/**
|
|
293
304
|
* Validate an input being a date
|
|
@@ -314,30 +325,49 @@ function DateLike(input, options = {}) {
|
|
|
314
325
|
}
|
|
315
326
|
|
|
316
327
|
/**
|
|
317
|
-
* Validate an input being one of
|
|
328
|
+
* Validate an input being one of allowed values
|
|
318
329
|
*
|
|
330
|
+
* @template {string | number | boolean | symbol | bigint} T
|
|
319
331
|
* @param {*} input - Input value needs to be validated.
|
|
320
|
-
* @param {
|
|
321
|
-
* @param {Options} options -
|
|
322
|
-
* @returns {
|
|
323
|
-
* @throws {HttpError} - If validation fails
|
|
332
|
+
* @param {readonly T[]} enumValues - Array of allowed values.
|
|
333
|
+
* @param {Options} options - Options for validation.
|
|
334
|
+
* @returns {T} - The validated input.
|
|
335
|
+
* @throws {HttpError} - If validation fails.
|
|
324
336
|
*/
|
|
325
337
|
function Enum(input, enumValues, options = {}) {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (!(validArray || validObject)) {
|
|
330
|
-
throwError('Enum validator requires an array or object of allowed values');
|
|
338
|
+
if (!types.isArray(enumValues)) {
|
|
339
|
+
throwError('Enum validator requires an array of allowed values');
|
|
331
340
|
}
|
|
332
341
|
|
|
333
|
-
var method = validArray ? 'includes' : 'hasOwnProperty';
|
|
334
|
-
|
|
335
342
|
return Validator(
|
|
336
|
-
input, (input) => enumValues
|
|
337
|
-
{ ...options, message: options
|
|
343
|
+
input, (input) => enumValues.includes(input),
|
|
344
|
+
{ ...options, message: options?.message || `${options?.name || 'Value'} {${input}} is not defined in ${options?.type || 'Enum'}` }
|
|
338
345
|
);
|
|
339
346
|
}
|
|
340
347
|
|
|
348
|
+
/**
|
|
349
|
+
* Return all keys of an object.
|
|
350
|
+
* Returns an array of names of the enumerable properties and methods of an object.
|
|
351
|
+
*
|
|
352
|
+
* @template {{ [key: PropertyKey]: any }} T
|
|
353
|
+
* @param {T} object - An object with keys and values
|
|
354
|
+
* @returns {(keyof T)[]} - List of all keys
|
|
355
|
+
*/
|
|
356
|
+
function Keys(object) {
|
|
357
|
+
return global.Object.keys(object);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Returns an array of values owned by the enumerable properties of an object.
|
|
362
|
+
*
|
|
363
|
+
* @template {{ [key: PropertyKey]: any }} T
|
|
364
|
+
* @param {T} object
|
|
365
|
+
* @returns {T[keyof T][]}
|
|
366
|
+
*/
|
|
367
|
+
function Values(object) {
|
|
368
|
+
return global.Object.values(object);
|
|
369
|
+
}
|
|
370
|
+
|
|
341
371
|
/**
|
|
342
372
|
* Validate an input being defined (not undefined)
|
|
343
373
|
*
|
|
@@ -388,6 +418,8 @@ function Validator(input, validate, options = {}) {
|
|
|
388
418
|
return input;
|
|
389
419
|
}
|
|
390
420
|
|
|
421
|
+
options = { ...options }; // prevent options being null
|
|
422
|
+
|
|
391
423
|
// use default value (options.default - must also be valid) if has, when input is invalid
|
|
392
424
|
if (options.hasOwnProperty('default') && validate(options.default)) {
|
|
393
425
|
return options.default;
|
|
@@ -425,10 +457,11 @@ module.exports = {
|
|
|
425
457
|
AsyncFunction,
|
|
426
458
|
Date,
|
|
427
459
|
DateLike,
|
|
428
|
-
Enum,
|
|
460
|
+
Enum, Keys, Values,
|
|
429
461
|
Defined,
|
|
430
462
|
NotNull,
|
|
431
463
|
NotNullish,
|
|
432
464
|
Validator,
|
|
433
|
-
throwError
|
|
465
|
+
throwError,
|
|
466
|
+
Options
|
|
434
467
|
};
|