binhend 2.3.6 → 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.3.6",
3
+ "version": "2.3.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -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.
@@ -330,7 +341,7 @@ function Enum(input, enumValues, options = {}) {
330
341
 
331
342
  return Validator(
332
343
  input, (input) => enumValues.includes(input),
333
- { ...options, message: options.message || `Value {${input}} is not defined in enum` }
344
+ { ...options, message: options?.message || `${options?.name || 'Value'} {${input}} is not defined in ${options?.type || 'Enum'}` }
334
345
  );
335
346
  }
336
347
 
@@ -407,6 +418,8 @@ function Validator(input, validate, options = {}) {
407
418
  return input;
408
419
  }
409
420
 
421
+ options = { ...options }; // prevent options being null
422
+
410
423
  // use default value (options.default - must also be valid) if has, when input is invalid
411
424
  if (options.hasOwnProperty('default') && validate(options.default)) {
412
425
  return options.default;
@@ -449,5 +462,6 @@ module.exports = {
449
462
  NotNull,
450
463
  NotNullish,
451
464
  Validator,
452
- throwError
465
+ throwError,
466
+ Options
453
467
  };