valgen 3.2.0 → 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
@@ -0,0 +1,296 @@
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.isUppercase = exports.isLowercase = exports.isJWT = exports.isHexColor = exports.isHash = exports.isETHAddress = exports.isBtcAddress = exports.isVAT = exports.isISSN = exports.isFQDN = exports.isEAN = exports.isPassportNumber = exports.isIBAN = exports.isCreditCard = exports.isBIC = exports.isBase64 = exports.isURL = exports.isPort = exports.isMACAddress = exports.isIPRange = exports.isIP = exports.isMobilePhone = exports.isEmail = exports.isUUID = void 0;
7
+ const validator_1 = __importDefault(require("validator"));
8
+ const index_js_1 = require("../core/index.js");
9
+ /**
10
+ * Validates if value is an "UUID".
11
+ * @validator isUUID
12
+ */
13
+ function isUUID(version, options) {
14
+ return (0, index_js_1.validator)('isUUID', function (input, context) {
15
+ if (input != null && typeof input === 'string' && validator_1.default.isUUID(input, version))
16
+ return input;
17
+ context.failure(`{{label}} is not a valid UUID${version ? ' v' + version : ''}`);
18
+ }, options);
19
+ }
20
+ exports.isUUID = isUUID;
21
+ /**
22
+ * Validates if value is a valid Email
23
+ * @validator isEmail
24
+ */
25
+ function isEmail(options) {
26
+ return (0, index_js_1.validator)('isEmail', function (input, context) {
27
+ if (typeof input === 'string' && validator_1.default.isEmail(input, options))
28
+ return input;
29
+ context.failure(`{{label}} is not a valid a EMail`);
30
+ }, options);
31
+ }
32
+ exports.isEmail = isEmail;
33
+ /**
34
+ * Validates if value is a valid Email
35
+ * @validator isMobilePhone
36
+ */
37
+ function isMobilePhone(options) {
38
+ return (0, index_js_1.validator)('isMobilePhone', function (input, context) {
39
+ if (typeof input === 'string' && validator_1.default.isMobilePhone(input, options?.locale, options))
40
+ return input;
41
+ context.failure(`{{label}} is not a valid a Mobile Phone Number`);
42
+ }, options);
43
+ }
44
+ exports.isMobilePhone = isMobilePhone;
45
+ /**
46
+ * Validates if value is an IP
47
+ * @validator isIP
48
+ */
49
+ function isIP(version, options) {
50
+ return (0, index_js_1.validator)('isIP', function (input, context) {
51
+ if (input != null && typeof input === 'string' && validator_1.default.isIP(input, version))
52
+ return input;
53
+ context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''}`);
54
+ }, options);
55
+ }
56
+ exports.isIP = isIP;
57
+ /**
58
+ * Validates if value is an IP
59
+ * @validator isUUID
60
+ */
61
+ function isIPRange(version, options) {
62
+ return (0, index_js_1.validator)('isIPRange', function (input, context) {
63
+ if (input != null && typeof input === 'string' && validator_1.default.isIPRange(input, version))
64
+ return input;
65
+ context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''} range`);
66
+ }, options);
67
+ }
68
+ exports.isIPRange = isIPRange;
69
+ /**
70
+ * Validates if value is an MACAddress
71
+ * @validator isMACAddress
72
+ */
73
+ function isMACAddress(options) {
74
+ return (0, index_js_1.validator)('isMACAddress', function (input, context) {
75
+ if (input != null && typeof input === 'string' && validator_1.default.isMACAddress(input, options))
76
+ return input;
77
+ context.failure(`{{label}} is not a valid MAC address`);
78
+ }, options);
79
+ }
80
+ exports.isMACAddress = isMACAddress;
81
+ /**
82
+ * Validates if value is a port number
83
+ * @validator isPort
84
+ */
85
+ function isPort(options) {
86
+ return (0, index_js_1.validator)('isPort', function (input, context) {
87
+ if (input != null && typeof input === 'string' && validator_1.default.isPort(input))
88
+ return input;
89
+ context.failure(`{{label}} is not a valid port number`);
90
+ }, options);
91
+ }
92
+ exports.isPort = isPort;
93
+ /**
94
+ * Validates if value is an MACAddress
95
+ * @validator isURL
96
+ */
97
+ function isURL(options) {
98
+ return (0, index_js_1.validator)('isURL', function (input, context) {
99
+ if (input != null && typeof input === 'string' && validator_1.default.isURL(input, options))
100
+ return input;
101
+ context.failure(`{{label}} is not a valid URL`);
102
+ }, options);
103
+ }
104
+ exports.isURL = isURL;
105
+ /**
106
+ * Validates if value is a "Base64" string.
107
+ * @validator isBase64
108
+ */
109
+ function isBase64(options) {
110
+ return (0, index_js_1.validator)('isBase64', function (input, context) {
111
+ if (typeof input === 'string' && validator_1.default.isBase64(input, options))
112
+ return input;
113
+ context.failure(`{{label}} is not a valid a Base64 string`);
114
+ }, options);
115
+ }
116
+ exports.isBase64 = isBase64;
117
+ /**
118
+ * Validates if value is a BIC (Bank Identification Code) or SWIFT code
119
+ * @validator isBIC
120
+ */
121
+ function isBIC(options) {
122
+ return (0, index_js_1.validator)('isBIC', function (input, context) {
123
+ if (typeof input === 'string' && validator_1.default.isBIC(input))
124
+ return input;
125
+ context.failure(`{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`);
126
+ }, options);
127
+ }
128
+ exports.isBIC = isBIC;
129
+ /**
130
+ * Validates if value is a "Base64" formatted string.
131
+ * @validator isCreditCard
132
+ */
133
+ function isCreditCard(options) {
134
+ return (0, index_js_1.validator)('isCreditCard', function (input, context) {
135
+ if (typeof input === 'string' && validator_1.default.isCreditCard(input, options))
136
+ return input;
137
+ context.failure(`{{label}} is not a valid Credit Card number`);
138
+ }, options);
139
+ }
140
+ exports.isCreditCard = isCreditCard;
141
+ /**
142
+ * Validates if value is an IBAN (International Bank Account Number)
143
+ * @validator isEAN
144
+ */
145
+ function isIBAN(options) {
146
+ return (0, index_js_1.validator)('isIBAN', function (input, context) {
147
+ if (typeof input === 'string' && validator_1.default.isIBAN(input))
148
+ return input;
149
+ context.failure(`{{label}} is not a valid IBAN (International Bank Account Number)`);
150
+ }, options);
151
+ }
152
+ exports.isIBAN = isIBAN;
153
+ /**
154
+ * Validates if value is an passport number
155
+ * @validator isPassportNumber
156
+ */
157
+ function isPassportNumber(countryCode, options) {
158
+ return (0, index_js_1.validator)('isPassportNumber', function (input, context) {
159
+ if (typeof input === 'string' && validator_1.default.isPassportNumber(input, countryCode))
160
+ return input;
161
+ context.failure(`{{label}} is not a valid ${countryCode} PassportNumber)`);
162
+ }, options);
163
+ }
164
+ exports.isPassportNumber = isPassportNumber;
165
+ /**
166
+ * Validates if value is an EAN (European Article Number)
167
+ * @validator isEAN
168
+ */
169
+ function isEAN(options) {
170
+ return (0, index_js_1.validator)('isEAN', function (input, context) {
171
+ if (typeof input === 'string' && validator_1.default.isEAN(input))
172
+ return input;
173
+ context.failure(`{{label}} is not a valid EAN (European Article Number)`);
174
+ }, options);
175
+ }
176
+ exports.isEAN = isEAN;
177
+ /**
178
+ * Validates if value is an FQDN
179
+ * @validator isFQDN
180
+ */
181
+ function isFQDN(options) {
182
+ return (0, index_js_1.validator)('isFQDN', function (input, context) {
183
+ if (typeof input === 'string' && validator_1.default.isFQDN(input, options))
184
+ return input;
185
+ context.failure(`{{label}} is not valid FQDN`);
186
+ }, options);
187
+ }
188
+ exports.isFQDN = isFQDN;
189
+ /**
190
+ * Validates if value is an ISSN
191
+ * @validator isISSN
192
+ */
193
+ function isISSN(options) {
194
+ return (0, index_js_1.validator)('isISSN', function (input, context) {
195
+ if (typeof input === 'string' && validator_1.default.isISSN(input, options))
196
+ return input;
197
+ context.failure(`{{label}} is not a valid ISSN`);
198
+ }, options);
199
+ }
200
+ exports.isISSN = isISSN;
201
+ /**
202
+ * Validates if value is an VAT number
203
+ * @validator isVAT
204
+ */
205
+ function isVAT(countryCode, options) {
206
+ return (0, index_js_1.validator)('isVAT', function (input, context) {
207
+ if (typeof input === 'string' && validator_1.default.isVAT(input, countryCode))
208
+ return input;
209
+ context.failure(`{{label}} is not a valid VAT number`);
210
+ }, options);
211
+ }
212
+ exports.isVAT = isVAT;
213
+ /**
214
+ * Validates if value is a BTC address.
215
+ * @validator isBtcAddress
216
+ */
217
+ function isBtcAddress(options) {
218
+ return (0, index_js_1.validator)('isBtcAddress', function (input, context) {
219
+ if (typeof input === 'string' && validator_1.default.isBtcAddress(input))
220
+ return input;
221
+ context.failure(`{{label}} is not a valid a BTC address`);
222
+ }, options);
223
+ }
224
+ exports.isBtcAddress = isBtcAddress;
225
+ /**
226
+ * Validates if value is a ETH (Ethereum) address.
227
+ * @validator isBtcAddress
228
+ */
229
+ function isETHAddress(options) {
230
+ return (0, index_js_1.validator)('isETHAddress', function (input, context) {
231
+ if (typeof input === 'string' && validator_1.default.isEthereumAddress(input))
232
+ return input;
233
+ context.failure(`{{label}} is not a valid a ETH (Ethereum) address`);
234
+ }, options);
235
+ }
236
+ exports.isETHAddress = isETHAddress;
237
+ /**
238
+ * Validates if value a hash of type algorithm
239
+ * @validator isHash
240
+ */
241
+ function isHash(algorithm, options) {
242
+ return (0, index_js_1.validator)('isHash', function (input, context) {
243
+ if (typeof input === 'string' && validator_1.default.isHash(input, algorithm))
244
+ return input;
245
+ context.failure(`{{label}} is not a valid ${algorithm} hash`);
246
+ }, options);
247
+ }
248
+ exports.isHash = isHash;
249
+ /**
250
+ * Validates if value is a Hex Color
251
+ * @validator isHexColor
252
+ */
253
+ function isHexColor(options) {
254
+ return (0, index_js_1.validator)('isHexColor', function (input, context) {
255
+ if (typeof input === 'string' && validator_1.default.isHexColor(input))
256
+ return input;
257
+ context.failure(`{{label}} is not a valid Hex Color`);
258
+ }, options);
259
+ }
260
+ exports.isHexColor = isHexColor;
261
+ /**
262
+ * Validates if value a valid JWT token
263
+ * @validator isHash
264
+ */
265
+ function isJWT(options) {
266
+ return (0, index_js_1.validator)('isJWT', function (input, context) {
267
+ if (typeof input === 'string' && validator_1.default.isJWT(input))
268
+ return input;
269
+ context.failure(`{{label}} is not a valid JWT token`);
270
+ }, options);
271
+ }
272
+ exports.isJWT = isJWT;
273
+ /**
274
+ * Validates if value a Lowercase string
275
+ * @validator isLowercase
276
+ */
277
+ function isLowercase(options) {
278
+ return (0, index_js_1.validator)('isLowercase', function (input, context) {
279
+ if (typeof input === 'string' && validator_1.default.isLowercase(input))
280
+ return input;
281
+ context.failure(`{{label}} is not a lowercase string`);
282
+ }, options);
283
+ }
284
+ exports.isLowercase = isLowercase;
285
+ /**
286
+ * Validates if value a Uppercase string
287
+ * @validator isUppercase
288
+ */
289
+ function isUppercase(options) {
290
+ return (0, index_js_1.validator)('isLowercase', function (input, context) {
291
+ if (typeof input === 'string' && validator_1.default.isUppercase(input))
292
+ return input;
293
+ context.failure(`{{label}} is not an uppercase string`);
294
+ }, options);
295
+ }
296
+ exports.isUppercase = isUppercase;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.union = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ *
7
+ */
8
+ function union(codecs) {
9
+ const l = codecs.length;
10
+ return (0, index_js_1.validator)('union', function (input, context) {
11
+ let i;
12
+ let c;
13
+ let v;
14
+ let passed = false;
15
+ for (i = 0; i < l; i++) {
16
+ c = codecs[i];
17
+ try {
18
+ v = c(input, context);
19
+ passed = true;
20
+ break;
21
+ }
22
+ catch (e) {
23
+ //
24
+ }
25
+ }
26
+ if (passed)
27
+ return v;
28
+ context.failure(`{{location}} didn't match any of required format`);
29
+ });
30
+ }
31
+ exports.union = union;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.iif = exports.forwardRef = void 0;
4
+ const index_js_1 = require("../core/index.js");
5
+ /**
6
+ * Forwards codec process to a sub codec. Useful for circular checks
7
+ * @validator forwardRef
8
+ */
9
+ function forwardRef(fn) {
10
+ return (0, index_js_1.validator)('forwardRef', function (input, context) {
11
+ const nested = fn(context);
12
+ return nested[index_js_1.kValidatorFn](input, context, nested);
13
+ });
14
+ }
15
+ exports.forwardRef = forwardRef;
16
+ function iif(check, _then, _else) {
17
+ return (0, index_js_1.validator)('iif', function (input, context) {
18
+ let c = _else;
19
+ try {
20
+ if (check(input) !== undefined)
21
+ c = _then;
22
+ }
23
+ catch {
24
+ // ignored
25
+ }
26
+ if ((0, index_js_1.isValidator)(c))
27
+ return c[index_js_1.kValidatorFn](input, context, c);
28
+ return c;
29
+ });
30
+ }
31
+ exports.iif = iif;
@@ -0,0 +1,2 @@
1
+ export const kValidatorFn = Symbol.for('kValidatorFn');
2
+ export const kOptions = Symbol.for('kOptions');
@@ -0,0 +1,61 @@
1
+ import { kOptions } from './constants.js';
2
+ import { ValidationError } from './validation-error.js';
3
+ const VARIABLE_REPLACE_PATTERN = /{{([^}]*)}}/g;
4
+ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
5
+ export class Context {
6
+ constructor(rule, options) {
7
+ this.errors = [];
8
+ this.input = {};
9
+ this.rule = rule;
10
+ if (options?.maxErrors != null)
11
+ this.maxErrors = options.maxErrors;
12
+ if (typeof options?.onFail === 'function')
13
+ this.onFail = options.onFail;
14
+ if (options?.coerce != null)
15
+ this.coerce = options?.coerce;
16
+ }
17
+ failure(message) {
18
+ const issue = (typeof message === 'object'
19
+ ? { ...message, message: message.message }
20
+ : { message: '' + message });
21
+ issue.rule = this.rule.id;
22
+ Object.assign(issue, this.input);
23
+ if (this.onFail) {
24
+ const proto = Object.getPrototypeOf(this);
25
+ const x = this.onFail(issue, this, proto.onFail);
26
+ if (!x)
27
+ return;
28
+ if (typeof x === 'object') {
29
+ delete x.id;
30
+ delete x.input;
31
+ Object.assign(issue, x);
32
+ }
33
+ else
34
+ issue.message = String(x);
35
+ }
36
+ issue.message = ('' + issue.message).replace(VARIABLE_REPLACE_PATTERN, (x, g) => {
37
+ const m = OPTIONAL_VAR_PATTERN.exec(g);
38
+ if (!m)
39
+ return x;
40
+ const k = m[1];
41
+ const v = this.input[k] ??
42
+ (m[1] === 'label' ? this.input.property : undefined);
43
+ if (v != null)
44
+ return '`' + v + '`';
45
+ if (m[2])
46
+ return m[2];
47
+ return m[1] === 'label' ? 'Value' : x;
48
+ });
49
+ this.errors.push(issue);
50
+ if (this.errors.length >= (this.maxErrors ?? Infinity))
51
+ throw new ValidationError(this.errors);
52
+ }
53
+ extend(rule, options) {
54
+ const extended = new Context(rule, { onFail: undefined, ...rule[kOptions], ...options });
55
+ Object.setPrototypeOf(extended, this);
56
+ delete extended.errors;
57
+ extended.parent = this;
58
+ extended.root = this.root || this;
59
+ return extended;
60
+ }
61
+ }
@@ -0,0 +1,5 @@
1
+ export * from './context.js';
2
+ export * from './constants.js';
3
+ export * from './types.js';
4
+ export * from './validation-error.js';
5
+ export * from './validator.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export class ValidationError extends Error {
2
+ constructor(issues) {
3
+ super(issues[0].message);
4
+ this.issues = [];
5
+ this.issues = issues;
6
+ }
7
+ }
@@ -0,0 +1,56 @@
1
+ import { camelCase } from '../helpers/string.utils.js';
2
+ import { kOptions, kValidatorFn } from './constants.js';
3
+ import { Context } from './context.js';
4
+ import { ValidationError } from './validation-error.js';
5
+ export function validator(arg0, arg1, arg2) {
6
+ let id = '';
7
+ let fn;
8
+ let validatorOptions;
9
+ if (typeof arg0 === 'string') {
10
+ id = arg0;
11
+ fn = arg1;
12
+ validatorOptions = arg2;
13
+ }
14
+ else {
15
+ fn = arg0;
16
+ validatorOptions = arg1;
17
+ }
18
+ if (typeof fn !== 'function')
19
+ throw new TypeError('You must provide a rule function argument');
20
+ id = id || fn.name || 'unnamed-rule';
21
+ const name = fn.name || camelCase(id);
22
+ const _rule = ({
23
+ [name](input, options, parent) {
24
+ const ctx = parent ? parent.extend(_rule, options) : new Context(_rule, options);
25
+ let value;
26
+ try {
27
+ ctx.input.value = input;
28
+ value = fn(input, ctx, _rule);
29
+ }
30
+ catch (e) {
31
+ if (!parent && e instanceof ValidationError)
32
+ throw e;
33
+ ctx.failure(e);
34
+ }
35
+ if (!parent && ctx.errors.length)
36
+ throw new ValidationError(ctx.errors);
37
+ return value;
38
+ }
39
+ })[name];
40
+ _rule.id = id;
41
+ _rule[kValidatorFn] = fn;
42
+ _rule[kOptions] = validatorOptions || {};
43
+ _rule.silent = (input, options, context) => {
44
+ try {
45
+ const value = _rule(input, options, context);
46
+ return { value };
47
+ }
48
+ catch (e) {
49
+ return { errors: e.issues };
50
+ }
51
+ };
52
+ return _rule;
53
+ }
54
+ export function isValidator(x) {
55
+ return !!(typeof x === 'function' && x.id && x[kValidatorFn]);
56
+ }
@@ -0,0 +1,14 @@
1
+ export function omitKeys(o, keys) {
2
+ return Object.keys(o).reduce((a, k) => {
3
+ if (!keys.includes(k))
4
+ a[k] = o[k];
5
+ return a;
6
+ }, {});
7
+ }
8
+ export function pickKeys(o, keys) {
9
+ return Object.keys(o).reduce((a, k) => {
10
+ if (keys.includes(k))
11
+ a[k] = o[k];
12
+ return a;
13
+ }, {});
14
+ }
@@ -0,0 +1,5 @@
1
+ export function camelCase(v) {
2
+ return v.replace(/[\W_\s]+([^\W_\s])/g, (arg$, c) => {
3
+ return c[0].toUpperCase();
4
+ });
5
+ }
package/esm/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './core/index.js';
2
+ export * from './rules/index.js';
@@ -0,0 +1,22 @@
1
+ export * from './is-any.js';
2
+ export * from './is-array.js';
3
+ export * from './is-boolean.js';
4
+ export * from './is-date.js';
5
+ export * from './is-empty.js';
6
+ export * from './is-enum.js';
7
+ export * from './is-integer.js';
8
+ export * from './is-null.js';
9
+ export * from './is-matches.js';
10
+ export * from './is-number.js';
11
+ export * from './is-object.js';
12
+ export * from './is-object-id.js';
13
+ export * from './is-record.js';
14
+ export * from './is-string.js';
15
+ export * from './is-tuple.js';
16
+ export * from './length.js';
17
+ export * from './optional.js';
18
+ export * from './pipe.js';
19
+ export * from './range.js';
20
+ export * from './string-formats.js';
21
+ export * from './union.js';
22
+ export * from './utilities.js';
@@ -0,0 +1,8 @@
1
+ import { validator } from '../core/index.js';
2
+ /**
3
+ * Does nothing, just returns original input value.
4
+ * @validator isAny
5
+ */
6
+ export const isAny = () => validator('is-any', function (input) {
7
+ return input;
8
+ });
@@ -0,0 +1,34 @@
1
+ import { kValidatorFn, validator, } from '../core/index.js';
2
+ /**
3
+ * Validates if value is "array" and applies validation for each item.
4
+ * Converts input value to array if coerce option is set to 'true'.
5
+ * @validator isArray
6
+ */
7
+ export function isArray(itemValidator, options) {
8
+ return validator('isArray', function (input, context) {
9
+ if (input != null && context.coerce && !Array.isArray(input))
10
+ input = [input];
11
+ if (!Array.isArray(input)) {
12
+ context.failure(`{{label}} is not an array`);
13
+ return;
14
+ }
15
+ if (!itemValidator)
16
+ return input;
17
+ const location = context.input.location || '';
18
+ const itemContext = context.extend(itemValidator);
19
+ let i;
20
+ let v;
21
+ const l = input.length;
22
+ const out = [];
23
+ for (i = 0; i < l; i++) {
24
+ v = input[i];
25
+ itemContext.input.value = v;
26
+ itemContext.input.label = ((context.input.label || context.input.property) || 'Item') +
27
+ `[${i}]`;
28
+ itemContext.input.property = i;
29
+ itemContext.input.location = location + '[' + i + ']';
30
+ out.push(itemValidator[kValidatorFn](v, itemContext, itemValidator));
31
+ }
32
+ return out;
33
+ }, options);
34
+ }
@@ -0,0 +1,26 @@
1
+ import { validator } from '../core/index.js';
2
+ const TRUE_PATTERN = /^true|t|1|yes|y$/i;
3
+ const FALSE_PATTERN = /^false|f|0|no|n$/i;
4
+ /**
5
+ * Validates if value is "boolean".
6
+ * Converts input value to boolean if coerce option is set to 'true'.
7
+ * @validator isBoolean
8
+ */
9
+ export function isBoolean(options) {
10
+ return validator('isBoolean', function (input, context) {
11
+ if (input != null && typeof input !== 'boolean' && context.coerce) {
12
+ if (typeof input === 'string') {
13
+ if (TRUE_PATTERN.test(input))
14
+ return true;
15
+ if (FALSE_PATTERN.test(input))
16
+ return false;
17
+ throw new TypeError(`Invalid boolean string`);
18
+ }
19
+ if (input === 1 || input === 0)
20
+ input = !!input;
21
+ }
22
+ if (typeof input === 'boolean')
23
+ return input;
24
+ context.failure(`{{label}} is not a valid boolean`);
25
+ }, options);
26
+ }
@@ -0,0 +1,55 @@
1
+ import dayjs from 'dayjs';
2
+ import customParseFormat from 'dayjs/plugin/customParseFormat';
3
+ import { validator } from '../core/index.js';
4
+ import { omitKeys } from '../helpers/object.utils.js';
5
+ dayjs.extend(customParseFormat);
6
+ /**
7
+ * Validates if value is instance of "Date".
8
+ * Converts input value to Date if coerce option is set to 'true'.
9
+ * @validator isDate
10
+ */
11
+ export function isDate(options) {
12
+ return validator('isDate', function (input, context) {
13
+ if (input != null && !(input instanceof Date) && context.coerce) {
14
+ if (typeof input === 'string') {
15
+ const d = dayjs(input, options?.format);
16
+ if (d.isValid())
17
+ input = d.toDate();
18
+ }
19
+ else if (typeof input === 'number')
20
+ input = new Date(input);
21
+ }
22
+ if (input && input instanceof Date && !isNaN(input.getTime()))
23
+ return input;
24
+ context.failure({
25
+ message: `{{label}} is not a valid Date instance` +
26
+ (context.coerce
27
+ ? ` or a date formatted${options?.format ? " (" + options.format + ")" : ''} string`
28
+ : ''),
29
+ format: options?.format
30
+ });
31
+ }, omitKeys(options || {}, ['format']));
32
+ }
33
+ /**
34
+ * Validates if value is DFS (date formatted string).
35
+ * Converts input value to DFS if coerce option is set to 'true'.
36
+ * @validator isDateString
37
+ */
38
+ export function isDateString(options) {
39
+ return validator('isDateString', function (input, context) {
40
+ if (input && typeof input !== 'string' && context.coerce) {
41
+ const d = dayjs(input);
42
+ if (d.isValid())
43
+ return options?.format ? d.format(options?.format) : d.toISOString();
44
+ }
45
+ if (typeof input === 'string') {
46
+ const d = dayjs(input, options?.format);
47
+ if (d.isValid())
48
+ return input;
49
+ }
50
+ context.failure({
51
+ message: `{{label}} is not a valid date formatted${options?.format ? " (" + options.format + ")" : ''} string`,
52
+ format: options?.format
53
+ });
54
+ }, omitKeys(options || {}, ['format']));
55
+ }