ejv 2.1.0 → 2.1.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 (62) hide show
  1. package/.mocharc.json +8 -8
  2. package/CHANGELOG.md +134 -127
  3. package/README-KR.md +597 -597
  4. package/README.md +603 -603
  5. package/build/cjs/constants.js +124 -0
  6. package/build/cjs/constants.js.map +1 -0
  7. package/build/cjs/ejv.js +1341 -0
  8. package/build/cjs/ejv.js.map +1 -0
  9. package/build/cjs/index.js +14 -0
  10. package/build/cjs/index.js.map +1 -0
  11. package/build/cjs/interfaces.js +29 -0
  12. package/build/cjs/interfaces.js.map +1 -0
  13. package/build/cjs/package.json +1 -0
  14. package/build/cjs/tester.js +288 -0
  15. package/build/cjs/tester.js.map +1 -0
  16. package/build/cjs/util.js +103 -0
  17. package/build/cjs/util.js.map +1 -0
  18. package/build/constants.d.ts +107 -0
  19. package/build/ejv.d.ts +2 -0
  20. package/build/esm/constants.js +121 -0
  21. package/build/esm/constants.js.map +1 -0
  22. package/build/esm/ejv.js +1339 -0
  23. package/build/esm/ejv.js.map +1 -0
  24. package/build/esm/index.js +4 -0
  25. package/build/esm/index.js.map +1 -0
  26. package/build/esm/interfaces.js +33 -0
  27. package/build/esm/interfaces.js.map +1 -0
  28. package/build/esm/package.json +1 -0
  29. package/build/esm/tester.js +252 -0
  30. package/build/esm/tester.js.map +1 -0
  31. package/build/esm/util.js +96 -0
  32. package/build/esm/util.js.map +1 -0
  33. package/build/index.d.ts +3 -0
  34. package/build/interfaces.d.ts +77 -0
  35. package/build/scripts/add-js-extensions.js +46 -0
  36. package/build/scripts/add-js-extensions.js.map +1 -0
  37. package/build/tester.d.ts +35 -0
  38. package/build/util.d.ts +7 -0
  39. package/eslint.config.mjs +66 -59
  40. package/package.json +54 -55
  41. package/scripts/add-js-extensions.ts +59 -59
  42. package/spec/ArrayScheme.ts +1021 -1021
  43. package/spec/CommonScheme.ts +251 -251
  44. package/spec/DateScheme.ts +472 -472
  45. package/spec/NumberScheme.ts +1160 -1160
  46. package/spec/ObjectScheme.ts +499 -499
  47. package/spec/RegExpScheme.ts +112 -112
  48. package/spec/StringScheme.ts +1407 -1336
  49. package/spec/common-test-util.ts +63 -63
  50. package/spec/ejv.spec.ts +235 -235
  51. package/spec/testers.spec.ts +833 -833
  52. package/src/constants.ts +164 -162
  53. package/src/ejv.ts +1751 -1746
  54. package/src/index.ts +14 -14
  55. package/src/interfaces.ts +144 -144
  56. package/src/tester.ts +323 -312
  57. package/src/util.ts +124 -124
  58. package/tsconfig.cjs.json +8 -8
  59. package/tsconfig.esm.json +7 -7
  60. package/tsconfig.json +19 -18
  61. package/tsconfig.scripts.json +14 -14
  62. package/tsconfig.types.json +9 -9
@@ -0,0 +1,1341 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ejv = void 0;
4
+ const interfaces_1 = require("./interfaces.js");
5
+ const constants_1 = require("./constants.js");
6
+ const tester_1 = require("./tester.js");
7
+ const util_1 = require("./util.js");
8
+ function _getEffectiveTypes(scheme) {
9
+ let result;
10
+ if ((0, tester_1.definedTester)(scheme.type)) {
11
+ result = ((0, tester_1.arrayTester)(scheme.type)
12
+ ? scheme.type
13
+ : [scheme.type]);
14
+ }
15
+ else if ((0, tester_1.definedTester)(scheme.parent)) {
16
+ result = _getEffectiveTypes(scheme.parent);
17
+ }
18
+ return result;
19
+ }
20
+ const _ejv = (data, schemes, options) => {
21
+ var _a, _b, _c, _d, _e;
22
+ // check schemes
23
+ if (!(0, tester_1.definedTester)(schemes) || schemes === null) {
24
+ return new interfaces_1.EjvError({
25
+ type: constants_1.ERROR_TYPE.NO_SCHEME,
26
+ message: constants_1.ERROR_MESSAGE.NO_SCHEME,
27
+ data: data,
28
+ errorScheme: schemes,
29
+ isSchemeError: true
30
+ });
31
+ }
32
+ if (!(0, tester_1.arrayTester)(schemes)) {
33
+ return new interfaces_1.EjvError({
34
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
35
+ message: constants_1.ERROR_MESSAGE.NO_ARRAY_SCHEME,
36
+ data: data,
37
+ errorScheme: schemes,
38
+ isSchemeError: true
39
+ });
40
+ }
41
+ if (!(0, tester_1.arrayTypeOfTester)(schemes, constants_1.DATA_TYPE.OBJECT)) {
42
+ return new interfaces_1.EjvError({
43
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
44
+ message: constants_1.ERROR_MESSAGE.NO_OBJECT_ARRAY_SCHEME,
45
+ data: data,
46
+ errorScheme: schemes,
47
+ isSchemeError: true
48
+ });
49
+ }
50
+ if (!(0, tester_1.minLengthTester)(schemes, 1)) {
51
+ return new interfaces_1.EjvError({
52
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
53
+ message: constants_1.ERROR_MESSAGE.EMPTY_SCHEME,
54
+ data: data,
55
+ errorScheme: schemes,
56
+ isSchemeError: true
57
+ });
58
+ }
59
+ // check data by schemes
60
+ let result = null;
61
+ // use for() instead of forEach() to stop
62
+ for (const scheme of schemes) {
63
+ const key = scheme.key;
64
+ const _options = (0, util_1.clone)(options); // divide instance
65
+ let value;
66
+ if (key) {
67
+ value = data[key];
68
+ _options.path.push(key);
69
+ }
70
+ const types = _getEffectiveTypes(scheme);
71
+ if (!(0, tester_1.definedTester)(types)) {
72
+ return new interfaces_1.EjvError({
73
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
74
+ message: constants_1.ERROR_MESSAGE.SCHEMES_SHOULD_HAVE_TYPE,
75
+ data: data,
76
+ errorScheme: scheme,
77
+ isSchemeError: true
78
+ });
79
+ }
80
+ const allDataType = Object.values(constants_1.DATA_TYPE);
81
+ const typeError = types.find((type) => {
82
+ return !(0, tester_1.definedTester)(type)
83
+ || !(0, tester_1.stringTester)(type)
84
+ || !(0, tester_1.enumTester)(type, allDataType);
85
+ });
86
+ if (typeError) {
87
+ return new interfaces_1.EjvError({
88
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
89
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.SCHEMES_HAS_INVALID_TYPE, {
90
+ placeholders: [typeError]
91
+ }),
92
+ data: data,
93
+ errorScheme: scheme,
94
+ isSchemeError: true
95
+ });
96
+ }
97
+ if (!(0, tester_1.uniqueItemsTester)(types)) {
98
+ const notUniqueItems = types.filter((type) => {
99
+ return types.filter((_type) => _type === type).length > 1;
100
+ });
101
+ const notUniqueItemsSifted = (0, util_1.sift)(notUniqueItems);
102
+ return new interfaces_1.EjvError({
103
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
104
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.SCHEMES_HAS_DUPLICATED_TYPE, {
105
+ placeholders: [notUniqueItemsSifted.join(', ')]
106
+ }),
107
+ data: data,
108
+ errorScheme: scheme,
109
+ isSchemeError: true
110
+ });
111
+ }
112
+ if (!(0, tester_1.definedTester)(value)) {
113
+ if (scheme.optional !== true) {
114
+ result = new interfaces_1.EjvError({
115
+ type: constants_1.ERROR_TYPE.REQUIRED,
116
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.REQUIRED),
117
+ data,
118
+ path: _options.path,
119
+ errorScheme: scheme,
120
+ errorData: value
121
+ });
122
+ break;
123
+ }
124
+ else {
125
+ continue;
126
+ }
127
+ }
128
+ if (value === null) {
129
+ if (scheme.nullable !== true) {
130
+ result = new interfaces_1.EjvError({
131
+ type: constants_1.ERROR_TYPE.REQUIRED,
132
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.REQUIRED),
133
+ data,
134
+ path: _options.path,
135
+ errorScheme: scheme,
136
+ errorData: value
137
+ });
138
+ break;
139
+ }
140
+ else {
141
+ continue;
142
+ }
143
+ }
144
+ const typeResolved = types.find((type) => {
145
+ return (0, tester_1.typeTester)(value, type);
146
+ });
147
+ if (typeResolved) {
148
+ if ((0, tester_1.definedTester)(scheme.type)) {
149
+ if (!(0, tester_1.arrayTester)(scheme.type)) {
150
+ if (scheme.type !== typeResolved) {
151
+ result = new interfaces_1.EjvError({
152
+ type: constants_1.ERROR_TYPE.TYPE_MISMATCH,
153
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH, {
154
+ placeholders: [scheme.type]
155
+ }),
156
+ data,
157
+ path: _options.path,
158
+ errorScheme: scheme,
159
+ errorData: value
160
+ });
161
+ }
162
+ }
163
+ else {
164
+ if (!scheme.type.includes(typeResolved)) {
165
+ result = new interfaces_1.EjvError({
166
+ type: constants_1.ERROR_TYPE.TYPE_MISMATCH_ONE_OF,
167
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
168
+ placeholders: [JSON.stringify(scheme.type)]
169
+ }),
170
+ data,
171
+ path: _options.path,
172
+ errorScheme: scheme,
173
+ errorData: value
174
+ });
175
+ }
176
+ }
177
+ }
178
+ // else do additional validation
179
+ }
180
+ else {
181
+ // type is not resolved
182
+ const typesForMsg = scheme.type || ((_a = scheme.parent) === null || _a === void 0 ? void 0 : _a.type);
183
+ if (!(0, tester_1.arrayTester)(typesForMsg)) {
184
+ result = new interfaces_1.EjvError({
185
+ type: constants_1.ERROR_TYPE.TYPE_MISMATCH,
186
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH, {
187
+ placeholders: [typesForMsg]
188
+ }),
189
+ data,
190
+ path: _options.path,
191
+ errorScheme: scheme,
192
+ errorData: value
193
+ });
194
+ }
195
+ else {
196
+ result = new interfaces_1.EjvError({
197
+ type: constants_1.ERROR_TYPE.TYPE_MISMATCH_ONE_OF,
198
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
199
+ placeholders: [JSON.stringify(typesForMsg)]
200
+ }),
201
+ data,
202
+ path: _options.path,
203
+ errorScheme: scheme,
204
+ errorData: value
205
+ });
206
+ }
207
+ break;
208
+ }
209
+ // additional check for type resolved
210
+ switch (typeResolved) {
211
+ case constants_1.DATA_TYPE.NUMBER: {
212
+ const valueAsNumber = value;
213
+ const numberScheme = scheme;
214
+ if ((0, tester_1.definedTester)(numberScheme.enum)) {
215
+ if (!(0, tester_1.arrayTester)(numberScheme.enum)) {
216
+ return new interfaces_1.EjvError({
217
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
218
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY),
219
+ data: data,
220
+ errorScheme: numberScheme,
221
+ isSchemeError: true
222
+ });
223
+ }
224
+ const enumArr = numberScheme.enum;
225
+ if (!(0, tester_1.arrayTypeOfTester)(enumArr, constants_1.DATA_TYPE.NUMBER)) {
226
+ return new interfaces_1.EjvError({
227
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
228
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ENUM_SHOULD_BE_NUMBERS),
229
+ data: data,
230
+ errorScheme: numberScheme,
231
+ isSchemeError: true
232
+ });
233
+ }
234
+ if (!(0, tester_1.enumTester)(valueAsNumber, enumArr)) {
235
+ result = new interfaces_1.EjvError({
236
+ type: constants_1.ERROR_TYPE.ONE_VALUE_OF,
237
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ONE_VALUE_OF, {
238
+ placeholders: [JSON.stringify(enumArr)]
239
+ }),
240
+ data,
241
+ path: _options.path,
242
+ errorScheme: numberScheme,
243
+ errorData: value
244
+ });
245
+ break;
246
+ }
247
+ }
248
+ if ((0, tester_1.definedTester)(numberScheme.notEnum)) {
249
+ if (!(0, tester_1.arrayTester)(numberScheme.notEnum)) {
250
+ return new interfaces_1.EjvError({
251
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
252
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY),
253
+ data: data,
254
+ errorScheme: numberScheme,
255
+ isSchemeError: true
256
+ });
257
+ }
258
+ const enumArr = numberScheme.notEnum;
259
+ if (!(0, tester_1.arrayTypeOfTester)(enumArr, constants_1.DATA_TYPE.NUMBER)) {
260
+ return new interfaces_1.EjvError({
261
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
262
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_NUMBERS),
263
+ data: data,
264
+ errorScheme: numberScheme,
265
+ isSchemeError: true
266
+ });
267
+ }
268
+ if (!(0, tester_1.notEnumTester)(valueAsNumber, enumArr)) {
269
+ result = new interfaces_1.EjvError({
270
+ type: constants_1.ERROR_TYPE.NOT_ONE_VALUE_OF,
271
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
272
+ placeholders: [JSON.stringify(enumArr)]
273
+ }),
274
+ data,
275
+ path: _options.path,
276
+ errorScheme: numberScheme,
277
+ errorData: value
278
+ });
279
+ break;
280
+ }
281
+ }
282
+ if ((0, tester_1.definedTester)(numberScheme.min)
283
+ || (0, tester_1.definedTester)((_b = scheme.parent) === null || _b === void 0 ? void 0 : _b.min)) {
284
+ const effectiveMin = (0, tester_1.definedTester)(numberScheme.min)
285
+ ? numberScheme.min
286
+ : (_c = scheme.parent) === null || _c === void 0 ? void 0 : _c.min;
287
+ if (!(0, tester_1.numberTester)(effectiveMin)) {
288
+ return new interfaces_1.EjvError({
289
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
290
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_SHOULD_BE_NUMBER),
291
+ data: data,
292
+ errorScheme: numberScheme,
293
+ isSchemeError: true
294
+ });
295
+ }
296
+ if ((0, tester_1.definedTester)(numberScheme.exclusiveMin)) {
297
+ if (!(0, tester_1.booleanTester)(numberScheme.exclusiveMin)) {
298
+ return new interfaces_1.EjvError({
299
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
300
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN),
301
+ data: data,
302
+ errorScheme: numberScheme,
303
+ isSchemeError: true
304
+ });
305
+ }
306
+ }
307
+ if (numberScheme.exclusiveMin) {
308
+ if (!(0, tester_1.exclusiveMinNumberTester)(valueAsNumber, effectiveMin)) {
309
+ result = new interfaces_1.EjvError({
310
+ type: constants_1.ERROR_TYPE.BIGGER_THAN,
311
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.BIGGER_THAN, {
312
+ placeholders: [effectiveMin]
313
+ }),
314
+ data,
315
+ path: _options.path,
316
+ errorScheme: numberScheme,
317
+ errorData: value
318
+ });
319
+ break;
320
+ }
321
+ }
322
+ else {
323
+ if (!(0, tester_1.minNumberTester)(valueAsNumber, effectiveMin)) {
324
+ result = new interfaces_1.EjvError({
325
+ type: constants_1.ERROR_TYPE.BIGGER_THAN_OR_EQUAL,
326
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.BIGGER_THAN_OR_EQUAL, {
327
+ placeholders: [effectiveMin]
328
+ }),
329
+ data,
330
+ path: _options.path,
331
+ errorScheme: numberScheme,
332
+ errorData: value
333
+ });
334
+ break;
335
+ }
336
+ }
337
+ }
338
+ if ((0, tester_1.definedTester)(numberScheme.max)
339
+ || (0, tester_1.definedTester)((_d = scheme.parent) === null || _d === void 0 ? void 0 : _d.max)) {
340
+ const effectiveMax = (0, tester_1.definedTester)(numberScheme.max)
341
+ ? numberScheme.max
342
+ : (_e = scheme.parent) === null || _e === void 0 ? void 0 : _e.max;
343
+ if (!(0, tester_1.numberTester)(effectiveMax)) {
344
+ return new interfaces_1.EjvError({
345
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
346
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_SHOULD_BE_NUMBER),
347
+ data: data,
348
+ errorScheme: numberScheme,
349
+ isSchemeError: true
350
+ });
351
+ }
352
+ if ((0, tester_1.definedTester)(numberScheme.exclusiveMax)) {
353
+ if (!(0, tester_1.booleanTester)(numberScheme.exclusiveMax)) {
354
+ return new interfaces_1.EjvError({
355
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
356
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN),
357
+ data: data,
358
+ errorScheme: numberScheme,
359
+ isSchemeError: true
360
+ });
361
+ }
362
+ }
363
+ if (numberScheme.exclusiveMax) {
364
+ if (!(0, tester_1.exclusiveMaxNumberTester)(valueAsNumber, effectiveMax)) {
365
+ result = new interfaces_1.EjvError({
366
+ type: constants_1.ERROR_TYPE.SMALLER_THAN,
367
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.SMALLER_THAN, {
368
+ placeholders: [effectiveMax]
369
+ }),
370
+ data,
371
+ path: _options.path,
372
+ errorScheme: numberScheme,
373
+ errorData: value
374
+ });
375
+ break;
376
+ }
377
+ }
378
+ else {
379
+ if (!(0, tester_1.maxNumberTester)(valueAsNumber, effectiveMax)) {
380
+ result = new interfaces_1.EjvError({
381
+ type: constants_1.ERROR_TYPE.SMALLER_THAN_OR_EQUAL,
382
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.SMALLER_THAN_OR_EQUAL, {
383
+ placeholders: [effectiveMax]
384
+ }),
385
+ data,
386
+ path: _options.path,
387
+ errorScheme: numberScheme,
388
+ errorData: value
389
+ });
390
+ break;
391
+ }
392
+ }
393
+ }
394
+ if ((0, tester_1.definedTester)(numberScheme.format)) {
395
+ let formats;
396
+ const allNumberFormat = Object.values(constants_1.NUMBER_FORMAT);
397
+ if (!(0, tester_1.arrayTester)(numberScheme.format)) {
398
+ const formatAsString = numberScheme.format;
399
+ if (!(0, tester_1.enumTester)(formatAsString, allNumberFormat)) {
400
+ return new interfaces_1.EjvError({
401
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
402
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
403
+ placeholders: [formatAsString]
404
+ }),
405
+ data: data,
406
+ errorScheme: numberScheme,
407
+ isSchemeError: true
408
+ });
409
+ }
410
+ formats = [numberScheme.format];
411
+ }
412
+ else {
413
+ const formatAsArray = numberScheme.format;
414
+ const errorFormat = formatAsArray.find((format) => {
415
+ return !(0, tester_1.enumTester)(format, allNumberFormat);
416
+ });
417
+ if (errorFormat) {
418
+ return new interfaces_1.EjvError({
419
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
420
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
421
+ placeholders: [errorFormat]
422
+ }),
423
+ data: data,
424
+ errorScheme: numberScheme,
425
+ isSchemeError: true
426
+ });
427
+ }
428
+ formats = numberScheme.format;
429
+ }
430
+ const someFormatIsWrong = formats.some((format) => {
431
+ let valid = false;
432
+ switch (format) {
433
+ case constants_1.NUMBER_FORMAT.INTEGER:
434
+ valid = (0, tester_1.integerTester)(valueAsNumber);
435
+ break;
436
+ case constants_1.NUMBER_FORMAT.INDEX:
437
+ valid = (0, tester_1.indexTester)(valueAsNumber);
438
+ break;
439
+ }
440
+ return valid;
441
+ });
442
+ if (!someFormatIsWrong) {
443
+ if (!(0, tester_1.arrayTester)(numberScheme.format)) {
444
+ result = new interfaces_1.EjvError({
445
+ type: constants_1.ERROR_TYPE.FORMAT,
446
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.FORMAT, {
447
+ placeholders: [numberScheme.format]
448
+ }),
449
+ data,
450
+ path: _options.path,
451
+ errorScheme: numberScheme,
452
+ errorData: value
453
+ });
454
+ }
455
+ else {
456
+ result = new interfaces_1.EjvError({
457
+ type: constants_1.ERROR_TYPE.FORMAT_ONE_OF,
458
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.FORMAT_ONE_OF, {
459
+ placeholders: [JSON.stringify(numberScheme.format)]
460
+ }),
461
+ data,
462
+ path: _options.path,
463
+ errorScheme: numberScheme,
464
+ errorData: value
465
+ });
466
+ }
467
+ break;
468
+ }
469
+ }
470
+ break;
471
+ }
472
+ case constants_1.DATA_TYPE.STRING: {
473
+ const valueAsString = value;
474
+ const stringScheme = scheme;
475
+ if ((0, tester_1.definedTester)(stringScheme.enum)) {
476
+ if (!(0, tester_1.arrayTester)(stringScheme.enum)) {
477
+ return new interfaces_1.EjvError({
478
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
479
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY),
480
+ data: data,
481
+ errorScheme: stringScheme,
482
+ isSchemeError: true
483
+ });
484
+ }
485
+ const enumArr = stringScheme.enum;
486
+ if (!(0, tester_1.arrayTypeOfTester)(enumArr, constants_1.DATA_TYPE.STRING)) {
487
+ return new interfaces_1.EjvError({
488
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
489
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ENUM_SHOULD_BE_STRINGS),
490
+ data: data,
491
+ errorScheme: stringScheme,
492
+ isSchemeError: true
493
+ });
494
+ }
495
+ if (!(0, tester_1.enumTester)(valueAsString, enumArr)) {
496
+ result = new interfaces_1.EjvError({
497
+ type: constants_1.ERROR_TYPE.ONE_VALUE_OF,
498
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ONE_VALUE_OF, {
499
+ placeholders: [JSON.stringify(stringScheme.enum)]
500
+ }),
501
+ data,
502
+ path: _options.path,
503
+ errorScheme: stringScheme,
504
+ errorData: value
505
+ });
506
+ break;
507
+ }
508
+ }
509
+ if ((0, tester_1.definedTester)(stringScheme.notEnum)) {
510
+ if (!(0, tester_1.arrayTester)(stringScheme.notEnum)) {
511
+ return new interfaces_1.EjvError({
512
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
513
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY),
514
+ data: data,
515
+ errorScheme: stringScheme,
516
+ isSchemeError: true
517
+ });
518
+ }
519
+ const enumArr = stringScheme.notEnum;
520
+ if (!(0, tester_1.arrayTypeOfTester)(enumArr, constants_1.DATA_TYPE.STRING)) {
521
+ return new interfaces_1.EjvError({
522
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
523
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_STRINGS),
524
+ data: data,
525
+ errorScheme: stringScheme,
526
+ isSchemeError: true
527
+ });
528
+ }
529
+ if (!(0, tester_1.notEnumTester)(valueAsString, enumArr)) {
530
+ result = new interfaces_1.EjvError({
531
+ type: constants_1.ERROR_TYPE.NOT_ONE_VALUE_OF,
532
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
533
+ placeholders: [JSON.stringify(stringScheme.notEnum)]
534
+ }),
535
+ data,
536
+ path: _options.path,
537
+ errorScheme: stringScheme,
538
+ errorData: value
539
+ });
540
+ break;
541
+ }
542
+ }
543
+ if ((0, tester_1.definedTester)(stringScheme.length)) {
544
+ const length = stringScheme.length;
545
+ if (!((0, tester_1.numberTester)(length) && (0, tester_1.integerTester)(length))) {
546
+ return new interfaces_1.EjvError({
547
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
548
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER),
549
+ data: data,
550
+ errorScheme: stringScheme,
551
+ isSchemeError: true
552
+ });
553
+ }
554
+ if (!(0, tester_1.lengthTester)(valueAsString, length)) {
555
+ result = new interfaces_1.EjvError({
556
+ type: constants_1.ERROR_TYPE.LENGTH,
557
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.LENGTH, {
558
+ placeholders: [length]
559
+ }),
560
+ data,
561
+ path: _options.path,
562
+ errorScheme: stringScheme,
563
+ errorData: value
564
+ });
565
+ break;
566
+ }
567
+ }
568
+ if ((0, tester_1.definedTester)(stringScheme.minLength)) {
569
+ const minLength = stringScheme.minLength;
570
+ if (!((0, tester_1.numberTester)(minLength) && (0, tester_1.integerTester)(minLength))) {
571
+ return new interfaces_1.EjvError({
572
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
573
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER),
574
+ data: data,
575
+ errorScheme: stringScheme,
576
+ isSchemeError: true
577
+ });
578
+ }
579
+ if (!(0, tester_1.minLengthTester)(valueAsString, minLength)) {
580
+ result = new interfaces_1.EjvError({
581
+ type: constants_1.ERROR_TYPE.MIN_LENGTH,
582
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_LENGTH, {
583
+ placeholders: ['' + minLength]
584
+ }),
585
+ data,
586
+ path: _options.path,
587
+ errorScheme: stringScheme,
588
+ errorData: value
589
+ });
590
+ break;
591
+ }
592
+ }
593
+ if ((0, tester_1.definedTester)(stringScheme.maxLength)) {
594
+ const maxLength = stringScheme.maxLength;
595
+ if (!((0, tester_1.numberTester)(maxLength) && (0, tester_1.integerTester)(maxLength))) {
596
+ return new interfaces_1.EjvError({
597
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
598
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER),
599
+ data: data,
600
+ errorScheme: stringScheme,
601
+ isSchemeError: true
602
+ });
603
+ }
604
+ if (!(0, tester_1.maxLengthTester)(valueAsString, maxLength)) {
605
+ result = new interfaces_1.EjvError({
606
+ type: constants_1.ERROR_TYPE.MAX_LENGTH,
607
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_LENGTH, {
608
+ placeholders: ['' + maxLength]
609
+ }),
610
+ data,
611
+ path: _options.path,
612
+ errorScheme: stringScheme,
613
+ errorData: value
614
+ });
615
+ break;
616
+ }
617
+ }
618
+ if ((0, tester_1.definedTester)(stringScheme.format)) {
619
+ let formats;
620
+ const allStringFormat = Object.values(constants_1.STRING_FORMAT);
621
+ if (!(0, tester_1.arrayTester)(stringScheme.format)) {
622
+ const formatAsString = stringScheme.format;
623
+ if (!(0, tester_1.enumTester)(formatAsString, allStringFormat)) {
624
+ return new interfaces_1.EjvError({
625
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
626
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_FORMAT, {
627
+ placeholders: [formatAsString]
628
+ }),
629
+ data: data,
630
+ errorScheme: stringScheme,
631
+ isSchemeError: true
632
+ });
633
+ }
634
+ formats = [stringScheme.format];
635
+ }
636
+ else {
637
+ const formatAsArray = stringScheme.format;
638
+ const errorFormat = formatAsArray.find((format) => {
639
+ return !(0, tester_1.enumTester)(format, allStringFormat);
640
+ });
641
+ if (errorFormat) {
642
+ return new interfaces_1.EjvError({
643
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
644
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_FORMAT, {
645
+ placeholders: [errorFormat]
646
+ }),
647
+ data: data,
648
+ errorScheme: stringScheme,
649
+ isSchemeError: true
650
+ });
651
+ }
652
+ formats = stringScheme.format;
653
+ }
654
+ const foundFormatMatching = formats.some((format) => {
655
+ let valid = false;
656
+ switch (format) {
657
+ case constants_1.STRING_FORMAT.EMAIL:
658
+ valid = (0, tester_1.emailTester)(valueAsString);
659
+ break;
660
+ case constants_1.STRING_FORMAT.JSON:
661
+ valid = (0, tester_1.jsonStrTester)(valueAsString);
662
+ break;
663
+ case constants_1.STRING_FORMAT.DATE:
664
+ valid = (0, tester_1.dateFormatTester)(valueAsString);
665
+ break;
666
+ case constants_1.STRING_FORMAT.TIME:
667
+ valid = (0, tester_1.timeFormatTester)(valueAsString);
668
+ break;
669
+ case constants_1.STRING_FORMAT.DATE_TIME:
670
+ valid = (0, tester_1.dateTimeFormatTester)(valueAsString);
671
+ break;
672
+ }
673
+ return valid;
674
+ });
675
+ if (!foundFormatMatching) {
676
+ if (!(0, tester_1.arrayTester)(stringScheme.format)) {
677
+ result = new interfaces_1.EjvError({
678
+ type: constants_1.ERROR_TYPE.FORMAT,
679
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.FORMAT, {
680
+ placeholders: [stringScheme.format]
681
+ }),
682
+ data,
683
+ path: _options.path,
684
+ errorScheme: stringScheme,
685
+ errorData: value
686
+ });
687
+ }
688
+ else {
689
+ result = new interfaces_1.EjvError({
690
+ type: constants_1.ERROR_TYPE.FORMAT_ONE_OF,
691
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.FORMAT_ONE_OF, {
692
+ placeholders: [JSON.stringify(stringScheme.format)]
693
+ }),
694
+ data,
695
+ path: _options.path,
696
+ errorScheme: stringScheme,
697
+ errorData: value
698
+ });
699
+ }
700
+ break;
701
+ }
702
+ }
703
+ if ((0, tester_1.definedTester)(stringScheme.pattern)) {
704
+ // check parameter
705
+ if (stringScheme.pattern === null) {
706
+ return new interfaces_1.EjvError({
707
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
708
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
709
+ placeholders: ['null']
710
+ }),
711
+ data: data,
712
+ errorScheme: stringScheme,
713
+ isSchemeError: true
714
+ });
715
+ }
716
+ const isValidPattern = (pattern) => {
717
+ return ((0, tester_1.stringTester)(pattern) && (0, tester_1.minLengthTester)(pattern, 1))
718
+ || ((0, tester_1.regExpTester)(pattern) && pattern.toString() !== '/(?:)/' && pattern.toString() !== '/null/');
719
+ };
720
+ const patternToString = (pattern) => {
721
+ let regExpStr;
722
+ if (pattern === null) {
723
+ regExpStr = '/null/';
724
+ }
725
+ else if ((0, tester_1.stringTester)(pattern)) {
726
+ if ((0, tester_1.minLengthTester)(pattern, 1)) {
727
+ regExpStr = new RegExp(pattern).toString();
728
+ }
729
+ else {
730
+ regExpStr = '//';
731
+ }
732
+ }
733
+ else {
734
+ regExpStr = pattern.toString();
735
+ }
736
+ // empty regular expression
737
+ if (regExpStr === '/(?:)/') {
738
+ regExpStr = '//';
739
+ }
740
+ return regExpStr;
741
+ };
742
+ const createArrayErrorMsg = (patternsAsArray) => {
743
+ return '[' + patternsAsArray.map((onePattern) => {
744
+ return patternToString(onePattern);
745
+ }).join(', ') + ']';
746
+ };
747
+ if ((0, tester_1.arrayTester)(stringScheme.pattern)) {
748
+ const patternsAsArray = stringScheme.pattern;
749
+ if (!(0, tester_1.minLengthTester)(patternsAsArray, 1)) { // empty array
750
+ return new interfaces_1.EjvError({
751
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
752
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
753
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
754
+ }),
755
+ data: data,
756
+ errorScheme: stringScheme,
757
+ isSchemeError: true
758
+ });
759
+ }
760
+ try {
761
+ const regExpPatterns = patternsAsArray.map((pattern) => {
762
+ if (!isValidPattern(pattern)) {
763
+ throw new Error((0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
764
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
765
+ }));
766
+ }
767
+ return new RegExp(pattern);
768
+ });
769
+ // check value
770
+ const foundMatchPattern = regExpPatterns.some((regexp) => {
771
+ return (0, tester_1.stringRegExpTester)(valueAsString, regexp);
772
+ });
773
+ if (!foundMatchPattern) {
774
+ result = new interfaces_1.EjvError({
775
+ type: constants_1.ERROR_TYPE.PATTERN_ONE_OF,
776
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PATTERN_ONE_OF, {
777
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
778
+ }),
779
+ data,
780
+ path: _options.path,
781
+ errorScheme: stringScheme,
782
+ errorData: value
783
+ });
784
+ break;
785
+ }
786
+ }
787
+ catch (e) { // eslint-disable-line @typescript-eslint/no-unused-vars
788
+ return new interfaces_1.EjvError({
789
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
790
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
791
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
792
+ }),
793
+ data: data,
794
+ errorScheme: stringScheme,
795
+ isSchemeError: true
796
+ });
797
+ }
798
+ }
799
+ else {
800
+ const patternAsOne = stringScheme.pattern;
801
+ if (!isValidPattern(patternAsOne)) {
802
+ return new interfaces_1.EjvError({
803
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
804
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
805
+ placeholders: [patternToString(patternAsOne)]
806
+ }),
807
+ data: data,
808
+ errorScheme: stringScheme,
809
+ isSchemeError: true
810
+ });
811
+ }
812
+ // check value
813
+ const regExp = new RegExp(patternAsOne);
814
+ if (!(0, tester_1.stringRegExpTester)(valueAsString, regExp)) {
815
+ result = new interfaces_1.EjvError({
816
+ type: constants_1.ERROR_TYPE.PATTERN,
817
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PATTERN, {
818
+ placeholders: [patternToString(patternAsOne)]
819
+ }),
820
+ data,
821
+ path: _options.path,
822
+ errorScheme: stringScheme,
823
+ errorData: value
824
+ });
825
+ break;
826
+ }
827
+ }
828
+ }
829
+ break;
830
+ }
831
+ case constants_1.DATA_TYPE.OBJECT: {
832
+ const valueAsObject = value;
833
+ const objectScheme = scheme;
834
+ if ((0, tester_1.definedTester)(objectScheme.allowNoProperty)) {
835
+ if (!(0, tester_1.booleanTester)(objectScheme.allowNoProperty)) {
836
+ return new interfaces_1.EjvError({
837
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
838
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN),
839
+ data: data,
840
+ errorScheme: objectScheme,
841
+ isSchemeError: true
842
+ });
843
+ }
844
+ if (!objectScheme.allowNoProperty && !(0, tester_1.hasPropertyTester)(valueAsObject)) {
845
+ result = new interfaces_1.EjvError({
846
+ type: constants_1.ERROR_TYPE.PROPERTY,
847
+ message: constants_1.ERROR_MESSAGE.PROPERTY,
848
+ data,
849
+ path: _options.path,
850
+ errorScheme: objectScheme,
851
+ errorData: value
852
+ });
853
+ break;
854
+ }
855
+ }
856
+ if ((0, tester_1.definedTester)(objectScheme.properties)) {
857
+ if (!(0, tester_1.arrayTester)(objectScheme.properties)) {
858
+ return new interfaces_1.EjvError({
859
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
860
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY),
861
+ data: data,
862
+ errorScheme: objectScheme,
863
+ isSchemeError: true
864
+ });
865
+ }
866
+ const properties = objectScheme.properties;
867
+ if (!(0, tester_1.minLengthTester)(properties, 1)) {
868
+ return new interfaces_1.EjvError({
869
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
870
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PROPERTIES_SHOULD_HAVE_ITEMS),
871
+ data: data,
872
+ errorScheme: objectScheme,
873
+ isSchemeError: true
874
+ });
875
+ }
876
+ if (!(0, tester_1.arrayTypeOfTester)(properties, constants_1.DATA_TYPE.OBJECT)) {
877
+ return new interfaces_1.EjvError({
878
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
879
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT),
880
+ data: data,
881
+ errorScheme: objectScheme,
882
+ isSchemeError: true
883
+ });
884
+ }
885
+ if (!(0, tester_1.objectTester)(value)) {
886
+ result = new interfaces_1.EjvError({
887
+ type: constants_1.ERROR_TYPE.TYPE_MISMATCH,
888
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH, {
889
+ placeholders: ['object']
890
+ }),
891
+ data,
892
+ path: _options.path,
893
+ errorScheme: objectScheme,
894
+ errorData: value
895
+ });
896
+ break;
897
+ }
898
+ const partialData = data[key];
899
+ const partialScheme = objectScheme.properties;
900
+ scheme.parent = objectScheme;
901
+ // call recursively
902
+ result = _ejv(partialData, partialScheme, _options);
903
+ if (result) {
904
+ // inject original data
905
+ result.data = data;
906
+ }
907
+ }
908
+ break;
909
+ }
910
+ case constants_1.DATA_TYPE.DATE: {
911
+ const valueAsDate = value;
912
+ const dateScheme = scheme;
913
+ const parentDateScheme = scheme.parent;
914
+ if ((0, tester_1.definedTester)(dateScheme.min)
915
+ || (0, tester_1.definedTester)(parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.min)) {
916
+ const minDateCandidate = dateScheme.min || (parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.min);
917
+ if (!(((0, tester_1.stringTester)(minDateCandidate)
918
+ && ((0, tester_1.dateFormatTester)(minDateCandidate)
919
+ || (0, tester_1.dateTimeFormatTester)(minDateCandidate)))
920
+ || (0, tester_1.dateTester)(minDateCandidate))) {
921
+ return new interfaces_1.EjvError({
922
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
923
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_DATE_SHOULD_BE_DATE_OR_STRING),
924
+ data: data,
925
+ errorScheme: dateScheme,
926
+ isSchemeError: true
927
+ });
928
+ }
929
+ const effectiveMin = new Date(minDateCandidate);
930
+ if ((0, tester_1.definedTester)(dateScheme.exclusiveMin)) {
931
+ if (!(0, tester_1.booleanTester)(dateScheme.exclusiveMin)) {
932
+ return new interfaces_1.EjvError({
933
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
934
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN),
935
+ data: data,
936
+ errorScheme: dateScheme,
937
+ isSchemeError: true
938
+ });
939
+ }
940
+ if (dateScheme.exclusiveMin) {
941
+ if (!(0, tester_1.exclusiveMinDateTester)(valueAsDate, effectiveMin)) {
942
+ result = new interfaces_1.EjvError({
943
+ type: constants_1.ERROR_TYPE.AFTER_DATE,
944
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.AFTER_DATE, {
945
+ placeholders: [effectiveMin.toISOString()]
946
+ }),
947
+ data,
948
+ path: _options.path,
949
+ errorScheme: dateScheme,
950
+ errorData: value
951
+ });
952
+ break;
953
+ }
954
+ }
955
+ else {
956
+ if (!(0, tester_1.minDateTester)(valueAsDate, effectiveMin)) {
957
+ result = new interfaces_1.EjvError({
958
+ type: constants_1.ERROR_TYPE.AFTER_OR_SAME_DATE,
959
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.AFTER_OR_SAME_DATE, {
960
+ placeholders: [effectiveMin.toISOString()]
961
+ }),
962
+ data,
963
+ path: _options.path,
964
+ errorScheme: dateScheme,
965
+ errorData: value
966
+ });
967
+ break;
968
+ }
969
+ }
970
+ }
971
+ else {
972
+ if (!(0, tester_1.minDateTester)(valueAsDate, effectiveMin)) {
973
+ result = new interfaces_1.EjvError({
974
+ type: constants_1.ERROR_TYPE.AFTER_OR_SAME_DATE,
975
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.AFTER_OR_SAME_DATE, {
976
+ placeholders: [effectiveMin.toISOString()]
977
+ }),
978
+ data,
979
+ path: _options.path,
980
+ errorScheme: dateScheme,
981
+ errorData: value
982
+ });
983
+ break;
984
+ }
985
+ }
986
+ }
987
+ if ((0, tester_1.definedTester)(dateScheme.max)
988
+ || (0, tester_1.definedTester)(parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.max)) {
989
+ const maxDateCandidate = dateScheme.max || (parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.max);
990
+ if (!(((0, tester_1.stringTester)(maxDateCandidate)
991
+ && ((0, tester_1.dateFormatTester)(maxDateCandidate)
992
+ || (0, tester_1.dateTimeFormatTester)(maxDateCandidate)))
993
+ || (0, tester_1.dateTester)(maxDateCandidate))) {
994
+ return new interfaces_1.EjvError({
995
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
996
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_DATE_SHOULD_BE_DATE_OR_STRING),
997
+ data: data,
998
+ errorScheme: dateScheme,
999
+ isSchemeError: true
1000
+ });
1001
+ }
1002
+ const effectiveMax = new Date(maxDateCandidate);
1003
+ if ((0, tester_1.definedTester)(dateScheme.exclusiveMax)) {
1004
+ if (!(0, tester_1.booleanTester)(dateScheme.exclusiveMax)) {
1005
+ return new interfaces_1.EjvError({
1006
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1007
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN),
1008
+ data: data,
1009
+ errorScheme: dateScheme,
1010
+ isSchemeError: true
1011
+ });
1012
+ }
1013
+ }
1014
+ if (dateScheme.exclusiveMax) {
1015
+ if (!(0, tester_1.exclusiveMaxDateTester)(valueAsDate, effectiveMax)) {
1016
+ result = new interfaces_1.EjvError({
1017
+ type: constants_1.ERROR_TYPE.BEFORE_DATE,
1018
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.BEFORE_DATE, {
1019
+ placeholders: [effectiveMax.toISOString()]
1020
+ }),
1021
+ data,
1022
+ path: _options.path,
1023
+ errorScheme: dateScheme,
1024
+ errorData: value
1025
+ });
1026
+ break;
1027
+ }
1028
+ }
1029
+ else {
1030
+ if (!(0, tester_1.maxDateTester)(valueAsDate, effectiveMax)) {
1031
+ result = new interfaces_1.EjvError({
1032
+ type: constants_1.ERROR_TYPE.BEFORE_OR_SAME_DATE,
1033
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.BEFORE_OR_SAME_DATE, {
1034
+ placeholders: [effectiveMax.toISOString()]
1035
+ }),
1036
+ data,
1037
+ path: _options.path,
1038
+ errorScheme: dateScheme,
1039
+ errorData: value
1040
+ });
1041
+ break;
1042
+ }
1043
+ }
1044
+ }
1045
+ break;
1046
+ }
1047
+ case constants_1.DATA_TYPE.ARRAY: {
1048
+ const valueAsArray = value;
1049
+ const arrayScheme = scheme;
1050
+ if ((0, tester_1.definedTester)(arrayScheme.length)) {
1051
+ const length = arrayScheme.length;
1052
+ if (!((0, tester_1.numberTester)(length) && (0, tester_1.integerTester)(length))) {
1053
+ return new interfaces_1.EjvError({
1054
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1055
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER),
1056
+ data: data,
1057
+ errorScheme: arrayScheme,
1058
+ isSchemeError: true
1059
+ });
1060
+ }
1061
+ if (!(0, tester_1.lengthTester)(valueAsArray, length)) {
1062
+ result = new interfaces_1.EjvError({
1063
+ type: constants_1.ERROR_TYPE.LENGTH,
1064
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.LENGTH, {
1065
+ placeholders: ['' + length]
1066
+ }),
1067
+ data,
1068
+ path: _options.path,
1069
+ errorScheme: arrayScheme,
1070
+ errorData: value
1071
+ });
1072
+ break;
1073
+ }
1074
+ }
1075
+ if ((0, tester_1.definedTester)(arrayScheme.minLength)) {
1076
+ const minLength = arrayScheme.minLength;
1077
+ if (!((0, tester_1.numberTester)(arrayScheme.minLength) && (0, tester_1.integerTester)(minLength))) {
1078
+ return new interfaces_1.EjvError({
1079
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1080
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER),
1081
+ data: data,
1082
+ errorScheme: arrayScheme,
1083
+ isSchemeError: true
1084
+ });
1085
+ }
1086
+ if (!(0, tester_1.minLengthTester)(valueAsArray, minLength)) {
1087
+ result = new interfaces_1.EjvError({
1088
+ type: constants_1.ERROR_TYPE.MIN_LENGTH,
1089
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_LENGTH, {
1090
+ placeholders: ['' + minLength]
1091
+ }),
1092
+ data,
1093
+ path: _options.path,
1094
+ errorScheme: arrayScheme,
1095
+ errorData: value
1096
+ });
1097
+ break;
1098
+ }
1099
+ }
1100
+ if ((0, tester_1.definedTester)(arrayScheme.maxLength)) {
1101
+ const maxLength = arrayScheme.maxLength;
1102
+ if (!((0, tester_1.numberTester)(arrayScheme.maxLength) && (0, tester_1.integerTester)(maxLength))) {
1103
+ return new interfaces_1.EjvError({
1104
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1105
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER),
1106
+ data: data,
1107
+ errorScheme: arrayScheme,
1108
+ isSchemeError: true
1109
+ });
1110
+ }
1111
+ if (!(0, tester_1.maxLengthTester)(valueAsArray, maxLength)) {
1112
+ result = new interfaces_1.EjvError({
1113
+ type: constants_1.ERROR_TYPE.MAX_LENGTH,
1114
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_LENGTH, {
1115
+ placeholders: ['' + maxLength]
1116
+ }),
1117
+ data,
1118
+ path: _options.path,
1119
+ errorScheme: arrayScheme,
1120
+ errorData: value
1121
+ });
1122
+ break;
1123
+ }
1124
+ }
1125
+ if ((0, tester_1.definedTester)(arrayScheme.unique)) {
1126
+ if (!(0, tester_1.booleanTester)(arrayScheme.unique)) {
1127
+ return new interfaces_1.EjvError({
1128
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1129
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.UNIQUE_SHOULD_BE_BOOLEAN),
1130
+ data: data,
1131
+ errorScheme: arrayScheme,
1132
+ isSchemeError: true
1133
+ });
1134
+ }
1135
+ if (arrayScheme.unique && !(0, tester_1.uniqueItemsTester)(valueAsArray)) {
1136
+ result = new interfaces_1.EjvError({
1137
+ type: constants_1.ERROR_TYPE.UNIQUE_ITEMS,
1138
+ message: constants_1.ERROR_MESSAGE.UNIQUE_ITEMS,
1139
+ data,
1140
+ path: _options.path,
1141
+ errorScheme: arrayScheme,
1142
+ errorData: value
1143
+ });
1144
+ break;
1145
+ }
1146
+ }
1147
+ if ((0, tester_1.definedTester)(arrayScheme.items)) {
1148
+ // convert array to object
1149
+ if (valueAsArray.length > 0) {
1150
+ const now = new Date();
1151
+ const tempKeyArr = valueAsArray.map((_value, i) => {
1152
+ return '' + (+now + i);
1153
+ });
1154
+ if ((0, tester_1.stringTester)(arrayScheme.items) // by DataType
1155
+ || ((0, tester_1.arrayTester)(arrayScheme.items) && (0, tester_1.arrayTypeOfTester)(arrayScheme.items, constants_1.DATA_TYPE.STRING)) // by DataType[]
1156
+ ) {
1157
+ const itemTypes = ((0, tester_1.arrayTester)(arrayScheme.items)
1158
+ ? arrayScheme.items
1159
+ : [arrayScheme.items]);
1160
+ const itemTypeError = itemTypes.find((type) => {
1161
+ return !(0, tester_1.definedTester)(type)
1162
+ || !(0, tester_1.stringTester)(type)
1163
+ || !(0, tester_1.enumTester)(type, allDataType);
1164
+ });
1165
+ if (itemTypeError) {
1166
+ return new interfaces_1.EjvError({
1167
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1168
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.SCHEMES_HAS_INVALID_TYPE, {
1169
+ placeholders: [itemTypeError]
1170
+ }),
1171
+ data: data,
1172
+ errorScheme: scheme,
1173
+ isSchemeError: true
1174
+ });
1175
+ }
1176
+ const partialData = {};
1177
+ const partialSchemes = [];
1178
+ tempKeyArr.forEach((tempKey, i) => {
1179
+ partialData[tempKey] = valueAsArray[i];
1180
+ partialSchemes.push({
1181
+ key: tempKey,
1182
+ type: itemTypes
1183
+ });
1184
+ });
1185
+ scheme.parent = arrayScheme;
1186
+ // call recursively
1187
+ const partialResult = _ejv(partialData, partialSchemes, _options);
1188
+ // convert new EjvError
1189
+ if (partialResult) {
1190
+ let errorMsg;
1191
+ if ((0, tester_1.arrayTester)(arrayScheme.items)) {
1192
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ITEMS_TYPE, {
1193
+ placeholders: [JSON.stringify(itemTypes)]
1194
+ });
1195
+ }
1196
+ else {
1197
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ITEMS_TYPE, {
1198
+ placeholders: [arrayScheme.items]
1199
+ });
1200
+ }
1201
+ const partialKeys = (partialResult.path || '').split('/');
1202
+ const partialKey = partialKeys[partialKeys.length - 1];
1203
+ const partialScheme = partialSchemes.find((_scheme) => {
1204
+ return _scheme.key === partialKey;
1205
+ });
1206
+ const partialKeyIndex = partialSchemes.indexOf(partialScheme);
1207
+ result = new interfaces_1.EjvError({
1208
+ type: constants_1.ERROR_TYPE.ITEMS_TYPE,
1209
+ message: errorMsg,
1210
+ data,
1211
+ path: [..._options.path, '' + partialKeyIndex],
1212
+ errorScheme: partialScheme,
1213
+ errorData: partialData[partialKey]
1214
+ });
1215
+ }
1216
+ break;
1217
+ }
1218
+ else if (((0, tester_1.objectTester)(arrayScheme.items) && arrayScheme.items !== null) // by Scheme
1219
+ || ((0, tester_1.arrayTester)(arrayScheme.items) && (0, tester_1.arrayTypeOfTester)(arrayScheme.items, constants_1.DATA_TYPE.OBJECT)) // by Scheme[]
1220
+ ) {
1221
+ const itemsAsSchemes = (0, tester_1.arrayTester)(arrayScheme.items)
1222
+ ? arrayScheme.items
1223
+ : [arrayScheme.items];
1224
+ let partialError = null;
1225
+ // use for() instead of forEach() to break
1226
+ const valueLength = valueAsArray.length;
1227
+ for (let arrIndex = 0; arrIndex < valueLength; arrIndex++) {
1228
+ const oneValue = valueAsArray[arrIndex];
1229
+ const partialData = {};
1230
+ const partialSchemes = [];
1231
+ const tempKeyForThisValue = tempKeyArr[arrIndex];
1232
+ partialData[tempKeyForThisValue] = oneValue;
1233
+ partialSchemes.push(...itemsAsSchemes.map((oneScheme) => {
1234
+ const newScheme = (0, util_1.clone)(oneScheme); // divide instance
1235
+ newScheme.key = tempKeyForThisValue;
1236
+ return newScheme;
1237
+ }));
1238
+ const partialResults = partialSchemes.map((partialScheme) => {
1239
+ // call recursively
1240
+ const partialResult = _ejv(partialData, [partialScheme], _options);
1241
+ if (partialResult) {
1242
+ partialResult.path = (partialResult.path || '').replace(tempKeyForThisValue, '' + arrIndex);
1243
+ }
1244
+ return partialResult;
1245
+ });
1246
+ if (!partialResults.some((oneResult) => oneResult === null)) {
1247
+ partialError = partialResults.find((oneResult) => {
1248
+ return !!oneResult;
1249
+ });
1250
+ break;
1251
+ }
1252
+ }
1253
+ if (partialError) {
1254
+ let errorType;
1255
+ let errorMsg;
1256
+ if (!!itemsAsSchemes && itemsAsSchemes.length > 1) {
1257
+ errorType = constants_1.ERROR_TYPE.ITEMS_SCHEMES;
1258
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ITEMS_SCHEMES, {
1259
+ placeholders: [JSON.stringify(itemsAsSchemes)]
1260
+ });
1261
+ }
1262
+ else {
1263
+ errorType = partialError.type;
1264
+ errorMsg = partialError.message;
1265
+ if (errorType === constants_1.ERROR_TYPE.REQUIRED) {
1266
+ // REQUIRED in array is TYPE_MISMATCH except with nullable === true
1267
+ errorType = constants_1.ERROR_TYPE.TYPE_MISMATCH;
1268
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH, {
1269
+ placeholders: [JSON.stringify(arrayScheme.items)]
1270
+ });
1271
+ }
1272
+ }
1273
+ result = new interfaces_1.EjvError({
1274
+ type: errorType,
1275
+ message: errorMsg,
1276
+ data
1277
+ });
1278
+ if (errorType === constants_1.ERROR_TYPE.INVALID_SCHEMES) {
1279
+ result.errorScheme = arrayScheme;
1280
+ result.isSchemeError = true;
1281
+ result.isDataError = false;
1282
+ }
1283
+ else {
1284
+ result.path = partialError.path;
1285
+ result.errorData = partialError.errorData;
1286
+ }
1287
+ break;
1288
+ }
1289
+ }
1290
+ else {
1291
+ return new interfaces_1.EjvError({
1292
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1293
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_ITEMS_SCHEME, {
1294
+ placeholders: [JSON.stringify(arrayScheme.items)]
1295
+ }),
1296
+ data: data,
1297
+ errorScheme: arrayScheme,
1298
+ isSchemeError: true
1299
+ });
1300
+ }
1301
+ }
1302
+ }
1303
+ break;
1304
+ }
1305
+ }
1306
+ if (result) {
1307
+ break;
1308
+ }
1309
+ }
1310
+ if (result !== null && (0, tester_1.definedTester)(options.customErrorMsg)) {
1311
+ const customErrorMsgObj = options.customErrorMsg;
1312
+ // override error message
1313
+ const customMsg = customErrorMsgObj[result.type];
1314
+ if ((0, tester_1.definedTester)(customMsg)) {
1315
+ result.message = customMsg;
1316
+ }
1317
+ }
1318
+ return result;
1319
+ };
1320
+ const ejv = (data, schemes, options) => {
1321
+ // check data itself
1322
+ if (!(0, tester_1.definedTester)(data) || !(0, tester_1.objectTester)(data) || data === null) {
1323
+ return new interfaces_1.EjvError({
1324
+ type: constants_1.ERROR_TYPE.NO_DATA,
1325
+ message: constants_1.ERROR_MESSAGE.NO_DATA,
1326
+ data: data,
1327
+ path: undefined,
1328
+ errorScheme: undefined,
1329
+ errorData: data,
1330
+ isSchemeError: false
1331
+ });
1332
+ }
1333
+ const internalOption = (options
1334
+ ? Object.assign({}, options) : {});
1335
+ if (!(0, tester_1.definedTester)(internalOption.path)) {
1336
+ internalOption.path = [];
1337
+ }
1338
+ return _ejv(data, schemes, internalOption);
1339
+ };
1340
+ exports.ejv = ejv;
1341
+ //# sourceMappingURL=ejv.js.map