valgen 3.2.1 → 4.0.0-alpha.2

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.
Files changed (140) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +12 -7
  3. package/cjs/core/constants.js +5 -0
  4. package/cjs/core/context.js +65 -0
  5. package/cjs/core/index.js +21 -0
  6. package/cjs/core/types.js +2 -0
  7. package/cjs/core/validation-error.js +11 -0
  8. package/cjs/core/validator.js +61 -0
  9. package/cjs/helpers/object.utils.js +19 -0
  10. package/cjs/helpers/string.utils.js +9 -0
  11. package/cjs/index.js +18 -0
  12. package/cjs/package.json +3 -0
  13. package/cjs/rules/index.js +38 -0
  14. package/cjs/rules/is-any.js +12 -0
  15. package/cjs/rules/is-array.js +38 -0
  16. package/cjs/rules/is-boolean.js +30 -0
  17. package/cjs/rules/is-date.js +63 -0
  18. package/cjs/rules/is-empty.js +68 -0
  19. package/cjs/rules/is-enum.js +30 -0
  20. package/cjs/rules/is-integer.js +26 -0
  21. package/cjs/rules/is-matches.js +24 -0
  22. package/cjs/rules/is-null.js +40 -0
  23. package/cjs/rules/is-number.js +26 -0
  24. package/cjs/rules/is-object-id.js +28 -0
  25. package/cjs/rules/is-object.js +103 -0
  26. package/cjs/rules/is-record.js +41 -0
  27. package/cjs/rules/is-string.js +79 -0
  28. package/cjs/rules/is-tuple.js +32 -0
  29. package/cjs/rules/length.js +41 -0
  30. package/cjs/rules/optional.js +44 -0
  31. package/cjs/rules/pipe.js +43 -0
  32. package/cjs/rules/range.js +89 -0
  33. package/cjs/rules/string-formats.js +296 -0
  34. package/cjs/rules/union.js +31 -0
  35. package/cjs/rules/utilities.js +31 -0
  36. package/esm/core/constants.js +2 -0
  37. package/esm/core/context.js +61 -0
  38. package/esm/core/index.js +5 -0
  39. package/esm/core/types.js +1 -0
  40. package/esm/core/validation-error.js +7 -0
  41. package/esm/core/validator.js +56 -0
  42. package/esm/helpers/object.utils.js +14 -0
  43. package/esm/helpers/string.utils.js +5 -0
  44. package/esm/index.js +2 -0
  45. package/esm/rules/index.js +22 -0
  46. package/esm/rules/is-any.js +8 -0
  47. package/esm/rules/is-array.js +34 -0
  48. package/esm/rules/is-boolean.js +26 -0
  49. package/esm/rules/is-date.js +55 -0
  50. package/esm/rules/is-empty.js +63 -0
  51. package/esm/rules/is-enum.js +26 -0
  52. package/esm/rules/is-integer.js +22 -0
  53. package/esm/rules/is-matches.js +20 -0
  54. package/esm/rules/is-null.js +34 -0
  55. package/esm/rules/is-number.js +22 -0
  56. package/esm/rules/is-object-id.js +21 -0
  57. package/esm/rules/is-object.js +99 -0
  58. package/esm/rules/is-record.js +37 -0
  59. package/esm/rules/is-string.js +70 -0
  60. package/esm/rules/is-tuple.js +28 -0
  61. package/esm/rules/length.js +35 -0
  62. package/esm/rules/optional.js +38 -0
  63. package/esm/rules/pipe.js +38 -0
  64. package/esm/rules/range.js +81 -0
  65. package/esm/rules/string-formats.js +266 -0
  66. package/esm/rules/union.js +27 -0
  67. package/esm/rules/utilities.js +26 -0
  68. package/package.json +33 -37
  69. package/typings/core/constants.d.ts +2 -0
  70. package/typings/core/context.d.ts +26 -0
  71. package/typings/core/index.d.ts +6 -0
  72. package/typings/core/types.d.ts +18 -0
  73. package/typings/core/validation-error.d.ts +5 -0
  74. package/typings/core/validator.d.ts +18 -0
  75. package/typings/helpers/object.utils.d.ts +2 -0
  76. package/typings/helpers/string.utils.d.ts +1 -0
  77. package/typings/index.d.ts +2 -0
  78. package/typings/rules/index.d.ts +22 -0
  79. package/typings/rules/is-any.d.ts +5 -0
  80. package/typings/rules/is-array.d.ts +7 -0
  81. package/typings/rules/is-boolean.d.ts +7 -0
  82. package/typings/rules/is-date.d.ts +19 -0
  83. package/typings/rules/is-empty.d.ts +13 -0
  84. package/typings/rules/is-enum.d.ts +9 -0
  85. package/typings/rules/is-integer.d.ts +7 -0
  86. package/typings/rules/is-matches.d.ts +9 -0
  87. package/typings/rules/is-null.d.ts +16 -0
  88. package/typings/rules/is-number.d.ts +7 -0
  89. package/typings/rules/is-object-id.d.ts +10 -0
  90. package/typings/rules/is-object.d.ts +22 -0
  91. package/typings/rules/is-record.d.ts +7 -0
  92. package/typings/rules/is-string.d.ts +42 -0
  93. package/typings/rules/is-tuple.d.ts +27 -0
  94. package/typings/rules/length.d.ts +21 -0
  95. package/typings/rules/optional.d.ts +17 -0
  96. package/typings/rules/pipe.d.ts +11 -0
  97. package/typings/rules/range.d.ts +48 -0
  98. package/typings/rules/string-formats.d.ts +140 -0
  99. package/typings/rules/union.d.ts +5 -0
  100. package/typings/rules/utilities.d.ts +11 -0
  101. package/lib/ArrayType.d.ts +0 -5
  102. package/lib/ArrayType.js +0 -43
  103. package/lib/DataType.d.ts +0 -25
  104. package/lib/DataType.js +0 -214
  105. package/lib/ObjectType.d.ts +0 -5
  106. package/lib/ObjectType.js +0 -78
  107. package/lib/SchemaError.d.ts +0 -3
  108. package/lib/SchemaError.js +0 -17
  109. package/lib/UnionType.d.ts +0 -5
  110. package/lib/UnionType.js +0 -54
  111. package/lib/Valgen.d.ts +0 -88
  112. package/lib/Valgen.js +0 -289
  113. package/lib/ValidationError.d.ts +0 -2
  114. package/lib/ValidationError.js +0 -4
  115. package/lib/factories/AnyFactory.d.ts +0 -28
  116. package/lib/factories/AnyFactory.js +0 -73
  117. package/lib/factories/ArrayFactory.d.ts +0 -16
  118. package/lib/factories/ArrayFactory.js +0 -148
  119. package/lib/factories/Base64Factory.d.ts +0 -12
  120. package/lib/factories/Base64Factory.js +0 -31
  121. package/lib/factories/BooleanFactory.d.ts +0 -12
  122. package/lib/factories/BooleanFactory.js +0 -49
  123. package/lib/factories/DateFactory.d.ts +0 -40
  124. package/lib/factories/DateFactory.js +0 -239
  125. package/lib/factories/FunctionFactory.d.ts +0 -12
  126. package/lib/factories/FunctionFactory.js +0 -28
  127. package/lib/factories/IntegerFactory.d.ts +0 -14
  128. package/lib/factories/IntegerFactory.js +0 -33
  129. package/lib/factories/NilFactory.d.ts +0 -4
  130. package/lib/factories/NilFactory.js +0 -20
  131. package/lib/factories/NumberFactory.d.ts +0 -36
  132. package/lib/factories/NumberFactory.js +0 -178
  133. package/lib/factories/ObjectFactory.d.ts +0 -38
  134. package/lib/factories/ObjectFactory.js +0 -326
  135. package/lib/factories/StringFactory.d.ts +0 -16
  136. package/lib/factories/StringFactory.js +0 -94
  137. package/lib/factories/UnionFactory.d.ts +0 -13
  138. package/lib/factories/UnionFactory.js +0 -62
  139. package/lib/index.d.ts +0 -19
  140. package/lib/index.js +0 -37
package/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # v4.0.0-alpha.1
2
+ [2023-06-04]
3
+
4
+ ### Changes
5
+
6
+ * Initial release of v4.0.0-alpha.1 ([`499af8e`](https://github.com/panates/valgen/commit/499af8eed81feeacb4bba5c254fbf1ede3933564))
package/README.md CHANGED
@@ -1,12 +1,11 @@
1
- # Valgen
1
+ # VALGEN
2
2
 
3
3
  [![NPM Version][npm-image]][npm-url]
4
4
  [![NPM Downloads][downloads-image]][downloads-url]
5
- [![Build Status][travis-image]][travis-url]
5
+ [![CircleCI][circleci-image]][circleci-url]
6
6
  [![Test Coverage][coveralls-image]][coveralls-url]
7
- [![DevDependencies][devdependencies-image]][devdependencies-url]
8
7
 
9
- Fast type validation generator.
8
+ Fast runtime type validator, converter and io (encoding/decoding) library.
10
9
 
11
10
  ## Installation
12
11
 
@@ -14,18 +13,24 @@ Fast type validation generator.
14
13
 
15
14
  ## Node Compatibility
16
15
 
17
- - node `>= 10.0`;
16
+ - node `>= 16.0`;
18
17
 
19
18
  ### License
20
19
  [MIT](LICENSE)
21
20
 
22
21
  [npm-image]: https://img.shields.io/npm/v/valgen.svg
23
22
  [npm-url]: https://npmjs.org/package/valgen
24
- [travis-image]: https://img.shields.io/travis/panates/valgen/master.svg
25
- [travis-url]: https://travis-ci.org/panates/valgen
23
+ [circleci-image]: https://circleci.com/gh/panates/valgen/tree/master.svg?style=shield
24
+ [circleci-url]: https://circleci.com/gh/panates/valgen/tree/master
26
25
  [coveralls-image]: https://img.shields.io/coveralls/panates/valgen/master.svg
27
26
  [coveralls-url]: https://coveralls.io/r/panates/valgen
28
27
  [downloads-image]: https://img.shields.io/npm/dm/valgen.svg
29
28
  [downloads-url]: https://npmjs.org/package/valgen
29
+ [gitter-image]: https://badges.gitter.im/panates/valgen.svg
30
+ [gitter-url]: https://gitter.im/panates/valgen?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
31
+ [dependencies-image]: https://david-dm.org/panates/valgen/status.svg
32
+ [dependencies-url]:https://david-dm.org/panates/valgen
30
33
  [devdependencies-image]: https://david-dm.org/panates/valgen/dev-status.svg
31
34
  [devdependencies-url]:https://david-dm.org/panates/valgen?type=dev
35
+ [quality-image]: http://npm.packagequality.com/shield/valgen.png
36
+ [quality-url]: http://packagequality.com/#?package=valgen
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.kOptions = exports.kValidatorFn = void 0;
4
+ exports.kValidatorFn = Symbol.for('kValidatorFn');
5
+ exports.kOptions = Symbol.for('kOptions');
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Context = void 0;
4
+ const constants_js_1 = require("./constants.js");
5
+ const validation_error_js_1 = require("./validation-error.js");
6
+ const VARIABLE_REPLACE_PATTERN = /{{([^}]*)}}/g;
7
+ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
8
+ class Context {
9
+ constructor(rule, options) {
10
+ this.errors = [];
11
+ this.input = {};
12
+ this.rule = rule;
13
+ if (options?.maxErrors != null)
14
+ this.maxErrors = options.maxErrors;
15
+ if (typeof options?.onFail === 'function')
16
+ this.onFail = options.onFail;
17
+ if (options?.coerce != null)
18
+ this.coerce = options?.coerce;
19
+ }
20
+ failure(message) {
21
+ const issue = (typeof message === 'object'
22
+ ? { ...message, message: message.message }
23
+ : { message: '' + message });
24
+ issue.rule = this.rule.id;
25
+ Object.assign(issue, this.input);
26
+ if (this.onFail) {
27
+ const proto = Object.getPrototypeOf(this);
28
+ const x = this.onFail(issue, this, proto.onFail);
29
+ if (!x)
30
+ return;
31
+ if (typeof x === 'object') {
32
+ delete x.id;
33
+ delete x.input;
34
+ Object.assign(issue, x);
35
+ }
36
+ else
37
+ issue.message = String(x);
38
+ }
39
+ issue.message = ('' + issue.message).replace(VARIABLE_REPLACE_PATTERN, (x, g) => {
40
+ const m = OPTIONAL_VAR_PATTERN.exec(g);
41
+ if (!m)
42
+ return x;
43
+ const k = m[1];
44
+ const v = this.input[k] ??
45
+ (m[1] === 'label' ? this.input.property : undefined);
46
+ if (v != null)
47
+ return '`' + v + '`';
48
+ if (m[2])
49
+ return m[2];
50
+ return m[1] === 'label' ? 'Value' : x;
51
+ });
52
+ this.errors.push(issue);
53
+ if (this.errors.length >= (this.maxErrors ?? Infinity))
54
+ throw new validation_error_js_1.ValidationError(this.errors);
55
+ }
56
+ extend(rule, options) {
57
+ const extended = new Context(rule, { onFail: undefined, ...rule[constants_js_1.kOptions], ...options });
58
+ Object.setPrototypeOf(extended, this);
59
+ delete extended.errors;
60
+ extended.parent = this;
61
+ extended.root = this.root || this;
62
+ return extended;
63
+ }
64
+ }
65
+ exports.Context = Context;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./context.js"), exports);
18
+ __exportStar(require("./constants.js"), exports);
19
+ __exportStar(require("./types.js"), exports);
20
+ __exportStar(require("./validation-error.js"), exports);
21
+ __exportStar(require("./validator.js"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidationError = void 0;
4
+ class ValidationError extends Error {
5
+ constructor(issues) {
6
+ super(issues[0].message);
7
+ this.issues = [];
8
+ this.issues = issues;
9
+ }
10
+ }
11
+ exports.ValidationError = ValidationError;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidator = exports.validator = void 0;
4
+ const string_utils_js_1 = require("../helpers/string.utils.js");
5
+ const constants_js_1 = require("./constants.js");
6
+ const context_js_1 = require("./context.js");
7
+ const validation_error_js_1 = require("./validation-error.js");
8
+ function validator(arg0, arg1, arg2) {
9
+ let id = '';
10
+ let fn;
11
+ let validatorOptions;
12
+ if (typeof arg0 === 'string') {
13
+ id = arg0;
14
+ fn = arg1;
15
+ validatorOptions = arg2;
16
+ }
17
+ else {
18
+ fn = arg0;
19
+ validatorOptions = arg1;
20
+ }
21
+ if (typeof fn !== 'function')
22
+ throw new TypeError('You must provide a rule function argument');
23
+ id = id || fn.name || 'unnamed-rule';
24
+ const name = fn.name || (0, string_utils_js_1.camelCase)(id);
25
+ const _rule = ({
26
+ [name](input, options, parent) {
27
+ const ctx = parent ? parent.extend(_rule, options) : new context_js_1.Context(_rule, options);
28
+ let value;
29
+ try {
30
+ ctx.input.value = input;
31
+ value = fn(input, ctx, _rule);
32
+ }
33
+ catch (e) {
34
+ if (!parent && e instanceof validation_error_js_1.ValidationError)
35
+ throw e;
36
+ ctx.failure(e);
37
+ }
38
+ if (!parent && ctx.errors.length)
39
+ throw new validation_error_js_1.ValidationError(ctx.errors);
40
+ return value;
41
+ }
42
+ })[name];
43
+ _rule.id = id;
44
+ _rule[constants_js_1.kValidatorFn] = fn;
45
+ _rule[constants_js_1.kOptions] = validatorOptions || {};
46
+ _rule.silent = (input, options, context) => {
47
+ try {
48
+ const value = _rule(input, options, context);
49
+ return { value };
50
+ }
51
+ catch (e) {
52
+ return { errors: e.issues };
53
+ }
54
+ };
55
+ return _rule;
56
+ }
57
+ exports.validator = validator;
58
+ function isValidator(x) {
59
+ return !!(typeof x === 'function' && x.id && x[constants_js_1.kValidatorFn]);
60
+ }
61
+ exports.isValidator = isValidator;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pickKeys = exports.omitKeys = void 0;
4
+ function omitKeys(o, keys) {
5
+ return Object.keys(o).reduce((a, k) => {
6
+ if (!keys.includes(k))
7
+ a[k] = o[k];
8
+ return a;
9
+ }, {});
10
+ }
11
+ exports.omitKeys = omitKeys;
12
+ function pickKeys(o, keys) {
13
+ return Object.keys(o).reduce((a, k) => {
14
+ if (keys.includes(k))
15
+ a[k] = o[k];
16
+ return a;
17
+ }, {});
18
+ }
19
+ exports.pickKeys = pickKeys;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.camelCase = void 0;
4
+ function camelCase(v) {
5
+ return v.replace(/[\W_\s]+([^\W_\s])/g, (arg$, c) => {
6
+ return c[0].toUpperCase();
7
+ });
8
+ }
9
+ exports.camelCase = camelCase;
package/cjs/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./core/index.js"), exports);
18
+ __exportStar(require("./rules/index.js"), exports);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./is-any.js"), exports);
18
+ __exportStar(require("./is-array.js"), exports);
19
+ __exportStar(require("./is-boolean.js"), exports);
20
+ __exportStar(require("./is-date.js"), exports);
21
+ __exportStar(require("./is-empty.js"), exports);
22
+ __exportStar(require("./is-enum.js"), exports);
23
+ __exportStar(require("./is-integer.js"), exports);
24
+ __exportStar(require("./is-null.js"), exports);
25
+ __exportStar(require("./is-matches.js"), exports);
26
+ __exportStar(require("./is-number.js"), exports);
27
+ __exportStar(require("./is-object.js"), exports);
28
+ __exportStar(require("./is-object-id.js"), exports);
29
+ __exportStar(require("./is-record.js"), exports);
30
+ __exportStar(require("./is-string.js"), exports);
31
+ __exportStar(require("./is-tuple.js"), exports);
32
+ __exportStar(require("./length.js"), exports);
33
+ __exportStar(require("./optional.js"), exports);
34
+ __exportStar(require("./pipe.js"), exports);
35
+ __exportStar(require("./range.js"), exports);
36
+ __exportStar(require("./string-formats.js"), exports);
37
+ __exportStar(require("./union.js"), exports);
38
+ __exportStar(require("./utilities.js"), exports);
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAny = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Does nothing, just returns original input value.
7
+ * @validator isAny
8
+ */
9
+ const isAny = () => (0, index_js_1.validator)('is-any', function (input) {
10
+ return input;
11
+ });
12
+ exports.isAny = isAny;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isArray = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Validates if value is "array" and applies validation for each item.
7
+ * Converts input value to array if coerce option is set to 'true'.
8
+ * @validator isArray
9
+ */
10
+ function isArray(itemValidator, options) {
11
+ return (0, index_js_1.validator)('isArray', function (input, context) {
12
+ if (input != null && context.coerce && !Array.isArray(input))
13
+ input = [input];
14
+ if (!Array.isArray(input)) {
15
+ context.failure(`{{label}} is not an array`);
16
+ return;
17
+ }
18
+ if (!itemValidator)
19
+ return input;
20
+ const location = context.input.location || '';
21
+ const itemContext = context.extend(itemValidator);
22
+ let i;
23
+ let v;
24
+ const l = input.length;
25
+ const out = [];
26
+ for (i = 0; i < l; i++) {
27
+ v = input[i];
28
+ itemContext.input.value = v;
29
+ itemContext.input.label = ((context.input.label || context.input.property) || 'Item') +
30
+ `[${i}]`;
31
+ itemContext.input.property = i;
32
+ itemContext.input.location = location + '[' + i + ']';
33
+ out.push(itemValidator[index_js_1.kValidatorFn](v, itemContext, itemValidator));
34
+ }
35
+ return out;
36
+ }, options);
37
+ }
38
+ exports.isArray = isArray;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBoolean = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ const TRUE_PATTERN = /^true|t|1|yes|y$/i;
6
+ const FALSE_PATTERN = /^false|f|0|no|n$/i;
7
+ /**
8
+ * Validates if value is "boolean".
9
+ * Converts input value to boolean if coerce option is set to 'true'.
10
+ * @validator isBoolean
11
+ */
12
+ function isBoolean(options) {
13
+ return (0, index_js_1.validator)('isBoolean', function (input, context) {
14
+ if (input != null && typeof input !== 'boolean' && context.coerce) {
15
+ if (typeof input === 'string') {
16
+ if (TRUE_PATTERN.test(input))
17
+ return true;
18
+ if (FALSE_PATTERN.test(input))
19
+ return false;
20
+ throw new TypeError(`Invalid boolean string`);
21
+ }
22
+ if (input === 1 || input === 0)
23
+ input = !!input;
24
+ }
25
+ if (typeof input === 'boolean')
26
+ return input;
27
+ context.failure(`{{label}} is not a valid boolean`);
28
+ }, options);
29
+ }
30
+ exports.isBoolean = isBoolean;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isDateString = exports.isDate = void 0;
7
+ const dayjs_1 = __importDefault(require("dayjs"));
8
+ const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
9
+ const index_js_1 = require("../core/index.js");
10
+ const object_utils_js_1 = require("../helpers/object.utils.js");
11
+ dayjs_1.default.extend(customParseFormat_1.default);
12
+ /**
13
+ * Validates if value is instance of "Date".
14
+ * Converts input value to Date if coerce option is set to 'true'.
15
+ * @validator isDate
16
+ */
17
+ function isDate(options) {
18
+ return (0, index_js_1.validator)('isDate', function (input, context) {
19
+ if (input != null && !(input instanceof Date) && context.coerce) {
20
+ if (typeof input === 'string') {
21
+ const d = (0, dayjs_1.default)(input, options?.format);
22
+ if (d.isValid())
23
+ input = d.toDate();
24
+ }
25
+ else if (typeof input === 'number')
26
+ input = new Date(input);
27
+ }
28
+ if (input && input instanceof Date && !isNaN(input.getTime()))
29
+ return input;
30
+ context.failure({
31
+ message: `{{label}} is not a valid Date instance` +
32
+ (context.coerce
33
+ ? ` or a date formatted${options?.format ? " (" + options.format + ")" : ''} string`
34
+ : ''),
35
+ format: options?.format
36
+ });
37
+ }, (0, object_utils_js_1.omitKeys)(options || {}, ['format']));
38
+ }
39
+ exports.isDate = isDate;
40
+ /**
41
+ * Validates if value is DFS (date formatted string).
42
+ * Converts input value to DFS if coerce option is set to 'true'.
43
+ * @validator isDateString
44
+ */
45
+ function isDateString(options) {
46
+ return (0, index_js_1.validator)('isDateString', function (input, context) {
47
+ if (input && typeof input !== 'string' && context.coerce) {
48
+ const d = (0, dayjs_1.default)(input);
49
+ if (d.isValid())
50
+ return options?.format ? d.format(options?.format) : d.toISOString();
51
+ }
52
+ if (typeof input === 'string') {
53
+ const d = (0, dayjs_1.default)(input, options?.format);
54
+ if (d.isValid())
55
+ return input;
56
+ }
57
+ context.failure({
58
+ message: `{{label}} is not a valid date formatted${options?.format ? " (" + options.format + ")" : ''} string`,
59
+ format: options?.format
60
+ });
61
+ }, (0, object_utils_js_1.omitKeys)(options || {}, ['format']));
62
+ }
63
+ exports.isDateString = isDateString;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNotEmpty = exports.isEmpty = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Checks if value is an empty. Value should be string, array, set, map or object
7
+ * @validator isEmpty
8
+ */
9
+ function isEmpty(options) {
10
+ return (0, index_js_1.validator)('isEmpty', function (input, context) {
11
+ if (input != null) {
12
+ if (typeof input === 'string') {
13
+ if (!input)
14
+ return input;
15
+ }
16
+ else if (Array.isArray(input)) {
17
+ if (!input.length)
18
+ return input;
19
+ }
20
+ else if (input instanceof Set) {
21
+ if (!input.size)
22
+ return input;
23
+ }
24
+ else if (input instanceof Map) {
25
+ if (!input.size)
26
+ return input;
27
+ }
28
+ else if (typeof input === 'object') {
29
+ if (!Object.keys(input).length)
30
+ return input;
31
+ }
32
+ }
33
+ context.failure(`{{label}} is not empty`);
34
+ }, options);
35
+ }
36
+ exports.isEmpty = isEmpty;
37
+ /**
38
+ * Checks if value is a not empty. Value should be string, array, set, map or object
39
+ * @validator isNotEmpty
40
+ */
41
+ function isNotEmpty(options) {
42
+ return (0, index_js_1.validator)('isNotEmpty', function (input, context) {
43
+ if (input != null) {
44
+ if (typeof input === 'string') {
45
+ if (input)
46
+ return input;
47
+ }
48
+ else if (Array.isArray(input)) {
49
+ if (input.length)
50
+ return input;
51
+ }
52
+ else if (input instanceof Set) {
53
+ if (input.size)
54
+ return input;
55
+ }
56
+ else if (input instanceof Map) {
57
+ if (input.size)
58
+ return input;
59
+ }
60
+ else if (typeof input === 'object') {
61
+ if (Object.keys(input).length)
62
+ return input;
63
+ }
64
+ }
65
+ context.failure(`{{label}} is empty`);
66
+ }, options);
67
+ }
68
+ exports.isNotEmpty = isNotEmpty;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEnum = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Validates if given value is one of enum values.
7
+ * @validator isEnum
8
+ */
9
+ function isEnum(values, options) {
10
+ const caseInSensitive = !!(options?.caseInSensitive);
11
+ const enumName = options?.enumName;
12
+ // Prepare an object for fast lookup
13
+ const valObj = (Array.isArray(values) ? values : [values])
14
+ .reduce((a, v) => {
15
+ if (typeof v === 'string' && caseInSensitive)
16
+ a[v.toUpperCase()] = true;
17
+ else
18
+ a[v] = true;
19
+ return a;
20
+ }, {});
21
+ return (0, index_js_1.validator)('isEnum', function (input, context) {
22
+ if (input != null && valObj[typeof input === 'string' && caseInSensitive ? input.toUpperCase() : input])
23
+ return input;
24
+ context.failure({
25
+ message: `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`,
26
+ enum: enumName
27
+ });
28
+ }, options);
29
+ }
30
+ exports.isEnum = isEnum;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isInteger = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Validates if value is "integer".
7
+ * Converts input value to integer number if coerce option is set to 'true'.
8
+ * @validator isInteger
9
+ */
10
+ function isInteger(options) {
11
+ return (0, index_js_1.validator)('isInteger', function (input, context) {
12
+ if (input != null && typeof input !== 'number' && context.coerce) {
13
+ if (typeof input === 'string')
14
+ input = parseFloat(input);
15
+ if (typeof input === 'bigint') {
16
+ const v = Number(input);
17
+ if (input === BigInt(v))
18
+ input = v;
19
+ }
20
+ }
21
+ if (typeof input === 'number' && !isNaN(input) && Number.isInteger(input))
22
+ return input;
23
+ context.failure(`{{label}} is not a valid integer number`);
24
+ }, options);
25
+ }
26
+ exports.isInteger = isInteger;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMatches = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Coerces given value to "UUID" format or returns undefined if nullish
7
+ * @validator uuid
8
+ */
9
+ function isMatches(format, options) {
10
+ const regExp = format instanceof RegExp ? format : new RegExp(format);
11
+ const formatName = options?.formatName;
12
+ return (0, index_js_1.validator)('matches', function (input, context) {
13
+ if (input == null)
14
+ return;
15
+ if (typeof input === 'string' && regExp.test(input))
16
+ return input;
17
+ context.failure({
18
+ message: `{{label}} does not match ${formatName || 'requested'} format`,
19
+ format,
20
+ formatName
21
+ });
22
+ }, options);
23
+ }
24
+ exports.isMatches = isMatches;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isUndefined = exports.isDefined = exports.isNull = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Validates if value is "null".
7
+ * @validator isNull
8
+ */
9
+ function isNull(options) {
10
+ return (0, index_js_1.validator)('isNull', function (input, context) {
11
+ if (input === null)
12
+ return input;
13
+ context.failure(`{{label}} is not null`);
14
+ }, options);
15
+ }
16
+ exports.isNull = isNull;
17
+ /**
18
+ * Validates if value is not "undefined" nor "null"
19
+ * @validator isDefined
20
+ */
21
+ function isDefined(options) {
22
+ return (0, index_js_1.validator)('is-defined', function (input, context) {
23
+ if (input !== undefined)
24
+ return input;
25
+ context.failure(`{{label}} is not defined`);
26
+ }, options);
27
+ }
28
+ exports.isDefined = isDefined;
29
+ /**
30
+ * Validates if value is "undefined"
31
+ * @validator isNotDefined
32
+ */
33
+ function isUndefined(options) {
34
+ return (0, index_js_1.validator)('isUndefined', function (input, context) {
35
+ if (input === undefined)
36
+ return;
37
+ context.failure(`{{label}} defined`);
38
+ }, options);
39
+ }
40
+ exports.isUndefined = isUndefined;