ejv 2.1.0 → 2.1.1

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.
@@ -0,0 +1,1338 @@
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.DATE:
661
+ valid = (0, tester_1.dateFormatTester)(valueAsString);
662
+ break;
663
+ case constants_1.STRING_FORMAT.TIME:
664
+ valid = (0, tester_1.timeFormatTester)(valueAsString);
665
+ break;
666
+ case constants_1.STRING_FORMAT.DATE_TIME:
667
+ valid = (0, tester_1.dateTimeFormatTester)(valueAsString);
668
+ break;
669
+ }
670
+ return valid;
671
+ });
672
+ if (!foundFormatMatching) {
673
+ if (!(0, tester_1.arrayTester)(stringScheme.format)) {
674
+ result = new interfaces_1.EjvError({
675
+ type: constants_1.ERROR_TYPE.FORMAT,
676
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.FORMAT, {
677
+ placeholders: [stringScheme.format]
678
+ }),
679
+ data,
680
+ path: _options.path,
681
+ errorScheme: stringScheme,
682
+ errorData: value
683
+ });
684
+ }
685
+ else {
686
+ result = new interfaces_1.EjvError({
687
+ type: constants_1.ERROR_TYPE.FORMAT_ONE_OF,
688
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.FORMAT_ONE_OF, {
689
+ placeholders: [JSON.stringify(stringScheme.format)]
690
+ }),
691
+ data,
692
+ path: _options.path,
693
+ errorScheme: stringScheme,
694
+ errorData: value
695
+ });
696
+ }
697
+ break;
698
+ }
699
+ }
700
+ if ((0, tester_1.definedTester)(stringScheme.pattern)) {
701
+ // check parameter
702
+ if (stringScheme.pattern === null) {
703
+ return new interfaces_1.EjvError({
704
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
705
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
706
+ placeholders: ['null']
707
+ }),
708
+ data: data,
709
+ errorScheme: stringScheme,
710
+ isSchemeError: true
711
+ });
712
+ }
713
+ const isValidPattern = (pattern) => {
714
+ return ((0, tester_1.stringTester)(pattern) && (0, tester_1.minLengthTester)(pattern, 1))
715
+ || ((0, tester_1.regExpTester)(pattern) && pattern.toString() !== '/(?:)/' && pattern.toString() !== '/null/');
716
+ };
717
+ const patternToString = (pattern) => {
718
+ let regExpStr;
719
+ if (pattern === null) {
720
+ regExpStr = '/null/';
721
+ }
722
+ else if ((0, tester_1.stringTester)(pattern)) {
723
+ if ((0, tester_1.minLengthTester)(pattern, 1)) {
724
+ regExpStr = new RegExp(pattern).toString();
725
+ }
726
+ else {
727
+ regExpStr = '//';
728
+ }
729
+ }
730
+ else {
731
+ regExpStr = pattern.toString();
732
+ }
733
+ // empty regular expression
734
+ if (regExpStr === '/(?:)/') {
735
+ regExpStr = '//';
736
+ }
737
+ return regExpStr;
738
+ };
739
+ const createArrayErrorMsg = (patternsAsArray) => {
740
+ return '[' + patternsAsArray.map((onePattern) => {
741
+ return patternToString(onePattern);
742
+ }).join(', ') + ']';
743
+ };
744
+ if ((0, tester_1.arrayTester)(stringScheme.pattern)) {
745
+ const patternsAsArray = stringScheme.pattern;
746
+ if (!(0, tester_1.minLengthTester)(patternsAsArray, 1)) { // empty array
747
+ return new interfaces_1.EjvError({
748
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
749
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
750
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
751
+ }),
752
+ data: data,
753
+ errorScheme: stringScheme,
754
+ isSchemeError: true
755
+ });
756
+ }
757
+ try {
758
+ const regExpPatterns = patternsAsArray.map((pattern) => {
759
+ if (!isValidPattern(pattern)) {
760
+ throw new Error((0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
761
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
762
+ }));
763
+ }
764
+ return new RegExp(pattern);
765
+ });
766
+ // check value
767
+ const foundMatchPattern = regExpPatterns.some((regexp) => {
768
+ return (0, tester_1.stringRegExpTester)(valueAsString, regexp);
769
+ });
770
+ if (!foundMatchPattern) {
771
+ result = new interfaces_1.EjvError({
772
+ type: constants_1.ERROR_TYPE.PATTERN_ONE_OF,
773
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PATTERN_ONE_OF, {
774
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
775
+ }),
776
+ data,
777
+ path: _options.path,
778
+ errorScheme: stringScheme,
779
+ errorData: value
780
+ });
781
+ break;
782
+ }
783
+ }
784
+ catch (e) { // eslint-disable-line @typescript-eslint/no-unused-vars
785
+ return new interfaces_1.EjvError({
786
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
787
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
788
+ placeholders: [createArrayErrorMsg(patternsAsArray)]
789
+ }),
790
+ data: data,
791
+ errorScheme: stringScheme,
792
+ isSchemeError: true
793
+ });
794
+ }
795
+ }
796
+ else {
797
+ const patternAsOne = stringScheme.pattern;
798
+ if (!isValidPattern(patternAsOne)) {
799
+ return new interfaces_1.EjvError({
800
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
801
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_STRING_PATTERN, {
802
+ placeholders: [patternToString(patternAsOne)]
803
+ }),
804
+ data: data,
805
+ errorScheme: stringScheme,
806
+ isSchemeError: true
807
+ });
808
+ }
809
+ // check value
810
+ const regExp = new RegExp(patternAsOne);
811
+ if (!(0, tester_1.stringRegExpTester)(valueAsString, regExp)) {
812
+ result = new interfaces_1.EjvError({
813
+ type: constants_1.ERROR_TYPE.PATTERN,
814
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PATTERN, {
815
+ placeholders: [patternToString(patternAsOne)]
816
+ }),
817
+ data,
818
+ path: _options.path,
819
+ errorScheme: stringScheme,
820
+ errorData: value
821
+ });
822
+ break;
823
+ }
824
+ }
825
+ }
826
+ break;
827
+ }
828
+ case constants_1.DATA_TYPE.OBJECT: {
829
+ const valueAsObject = value;
830
+ const objectScheme = scheme;
831
+ if ((0, tester_1.definedTester)(objectScheme.allowNoProperty)) {
832
+ if (!(0, tester_1.booleanTester)(objectScheme.allowNoProperty)) {
833
+ return new interfaces_1.EjvError({
834
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
835
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN),
836
+ data: data,
837
+ errorScheme: objectScheme,
838
+ isSchemeError: true
839
+ });
840
+ }
841
+ if (!objectScheme.allowNoProperty && !(0, tester_1.hasPropertyTester)(valueAsObject)) {
842
+ result = new interfaces_1.EjvError({
843
+ type: constants_1.ERROR_TYPE.PROPERTY,
844
+ message: constants_1.ERROR_MESSAGE.PROPERTY,
845
+ data,
846
+ path: _options.path,
847
+ errorScheme: objectScheme,
848
+ errorData: value
849
+ });
850
+ break;
851
+ }
852
+ }
853
+ if ((0, tester_1.definedTester)(objectScheme.properties)) {
854
+ if (!(0, tester_1.arrayTester)(objectScheme.properties)) {
855
+ return new interfaces_1.EjvError({
856
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
857
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY),
858
+ data: data,
859
+ errorScheme: objectScheme,
860
+ isSchemeError: true
861
+ });
862
+ }
863
+ const properties = objectScheme.properties;
864
+ if (!(0, tester_1.minLengthTester)(properties, 1)) {
865
+ return new interfaces_1.EjvError({
866
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
867
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PROPERTIES_SHOULD_HAVE_ITEMS),
868
+ data: data,
869
+ errorScheme: objectScheme,
870
+ isSchemeError: true
871
+ });
872
+ }
873
+ if (!(0, tester_1.arrayTypeOfTester)(properties, constants_1.DATA_TYPE.OBJECT)) {
874
+ return new interfaces_1.EjvError({
875
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
876
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT),
877
+ data: data,
878
+ errorScheme: objectScheme,
879
+ isSchemeError: true
880
+ });
881
+ }
882
+ if (!(0, tester_1.objectTester)(value)) {
883
+ result = new interfaces_1.EjvError({
884
+ type: constants_1.ERROR_TYPE.TYPE_MISMATCH,
885
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH, {
886
+ placeholders: ['object']
887
+ }),
888
+ data,
889
+ path: _options.path,
890
+ errorScheme: objectScheme,
891
+ errorData: value
892
+ });
893
+ break;
894
+ }
895
+ const partialData = data[key];
896
+ const partialScheme = objectScheme.properties;
897
+ scheme.parent = objectScheme;
898
+ // call recursively
899
+ result = _ejv(partialData, partialScheme, _options);
900
+ if (result) {
901
+ // inject original data
902
+ result.data = data;
903
+ }
904
+ }
905
+ break;
906
+ }
907
+ case constants_1.DATA_TYPE.DATE: {
908
+ const valueAsDate = value;
909
+ const dateScheme = scheme;
910
+ const parentDateScheme = scheme.parent;
911
+ if ((0, tester_1.definedTester)(dateScheme.min)
912
+ || (0, tester_1.definedTester)(parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.min)) {
913
+ const minDateCandidate = dateScheme.min || (parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.min);
914
+ if (!(((0, tester_1.stringTester)(minDateCandidate)
915
+ && ((0, tester_1.dateFormatTester)(minDateCandidate)
916
+ || (0, tester_1.dateTimeFormatTester)(minDateCandidate)))
917
+ || (0, tester_1.dateTester)(minDateCandidate))) {
918
+ return new interfaces_1.EjvError({
919
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
920
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_DATE_SHOULD_BE_DATE_OR_STRING),
921
+ data: data,
922
+ errorScheme: dateScheme,
923
+ isSchemeError: true
924
+ });
925
+ }
926
+ const effectiveMin = new Date(minDateCandidate);
927
+ if ((0, tester_1.definedTester)(dateScheme.exclusiveMin)) {
928
+ if (!(0, tester_1.booleanTester)(dateScheme.exclusiveMin)) {
929
+ return new interfaces_1.EjvError({
930
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
931
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN),
932
+ data: data,
933
+ errorScheme: dateScheme,
934
+ isSchemeError: true
935
+ });
936
+ }
937
+ if (dateScheme.exclusiveMin) {
938
+ if (!(0, tester_1.exclusiveMinDateTester)(valueAsDate, effectiveMin)) {
939
+ result = new interfaces_1.EjvError({
940
+ type: constants_1.ERROR_TYPE.AFTER_DATE,
941
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.AFTER_DATE, {
942
+ placeholders: [effectiveMin.toISOString()]
943
+ }),
944
+ data,
945
+ path: _options.path,
946
+ errorScheme: dateScheme,
947
+ errorData: value
948
+ });
949
+ break;
950
+ }
951
+ }
952
+ else {
953
+ if (!(0, tester_1.minDateTester)(valueAsDate, effectiveMin)) {
954
+ result = new interfaces_1.EjvError({
955
+ type: constants_1.ERROR_TYPE.AFTER_OR_SAME_DATE,
956
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.AFTER_OR_SAME_DATE, {
957
+ placeholders: [effectiveMin.toISOString()]
958
+ }),
959
+ data,
960
+ path: _options.path,
961
+ errorScheme: dateScheme,
962
+ errorData: value
963
+ });
964
+ break;
965
+ }
966
+ }
967
+ }
968
+ else {
969
+ if (!(0, tester_1.minDateTester)(valueAsDate, effectiveMin)) {
970
+ result = new interfaces_1.EjvError({
971
+ type: constants_1.ERROR_TYPE.AFTER_OR_SAME_DATE,
972
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.AFTER_OR_SAME_DATE, {
973
+ placeholders: [effectiveMin.toISOString()]
974
+ }),
975
+ data,
976
+ path: _options.path,
977
+ errorScheme: dateScheme,
978
+ errorData: value
979
+ });
980
+ break;
981
+ }
982
+ }
983
+ }
984
+ if ((0, tester_1.definedTester)(dateScheme.max)
985
+ || (0, tester_1.definedTester)(parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.max)) {
986
+ const maxDateCandidate = dateScheme.max || (parentDateScheme === null || parentDateScheme === void 0 ? void 0 : parentDateScheme.max);
987
+ if (!(((0, tester_1.stringTester)(maxDateCandidate)
988
+ && ((0, tester_1.dateFormatTester)(maxDateCandidate)
989
+ || (0, tester_1.dateTimeFormatTester)(maxDateCandidate)))
990
+ || (0, tester_1.dateTester)(maxDateCandidate))) {
991
+ return new interfaces_1.EjvError({
992
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
993
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_DATE_SHOULD_BE_DATE_OR_STRING),
994
+ data: data,
995
+ errorScheme: dateScheme,
996
+ isSchemeError: true
997
+ });
998
+ }
999
+ const effectiveMax = new Date(maxDateCandidate);
1000
+ if ((0, tester_1.definedTester)(dateScheme.exclusiveMax)) {
1001
+ if (!(0, tester_1.booleanTester)(dateScheme.exclusiveMax)) {
1002
+ return new interfaces_1.EjvError({
1003
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1004
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN),
1005
+ data: data,
1006
+ errorScheme: dateScheme,
1007
+ isSchemeError: true
1008
+ });
1009
+ }
1010
+ }
1011
+ if (dateScheme.exclusiveMax) {
1012
+ if (!(0, tester_1.exclusiveMaxDateTester)(valueAsDate, effectiveMax)) {
1013
+ result = new interfaces_1.EjvError({
1014
+ type: constants_1.ERROR_TYPE.BEFORE_DATE,
1015
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.BEFORE_DATE, {
1016
+ placeholders: [effectiveMax.toISOString()]
1017
+ }),
1018
+ data,
1019
+ path: _options.path,
1020
+ errorScheme: dateScheme,
1021
+ errorData: value
1022
+ });
1023
+ break;
1024
+ }
1025
+ }
1026
+ else {
1027
+ if (!(0, tester_1.maxDateTester)(valueAsDate, effectiveMax)) {
1028
+ result = new interfaces_1.EjvError({
1029
+ type: constants_1.ERROR_TYPE.BEFORE_OR_SAME_DATE,
1030
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.BEFORE_OR_SAME_DATE, {
1031
+ placeholders: [effectiveMax.toISOString()]
1032
+ }),
1033
+ data,
1034
+ path: _options.path,
1035
+ errorScheme: dateScheme,
1036
+ errorData: value
1037
+ });
1038
+ break;
1039
+ }
1040
+ }
1041
+ }
1042
+ break;
1043
+ }
1044
+ case constants_1.DATA_TYPE.ARRAY: {
1045
+ const valueAsArray = value;
1046
+ const arrayScheme = scheme;
1047
+ if ((0, tester_1.definedTester)(arrayScheme.length)) {
1048
+ const length = arrayScheme.length;
1049
+ if (!((0, tester_1.numberTester)(length) && (0, tester_1.integerTester)(length))) {
1050
+ return new interfaces_1.EjvError({
1051
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1052
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER),
1053
+ data: data,
1054
+ errorScheme: arrayScheme,
1055
+ isSchemeError: true
1056
+ });
1057
+ }
1058
+ if (!(0, tester_1.lengthTester)(valueAsArray, length)) {
1059
+ result = new interfaces_1.EjvError({
1060
+ type: constants_1.ERROR_TYPE.LENGTH,
1061
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.LENGTH, {
1062
+ placeholders: ['' + length]
1063
+ }),
1064
+ data,
1065
+ path: _options.path,
1066
+ errorScheme: arrayScheme,
1067
+ errorData: value
1068
+ });
1069
+ break;
1070
+ }
1071
+ }
1072
+ if ((0, tester_1.definedTester)(arrayScheme.minLength)) {
1073
+ const minLength = arrayScheme.minLength;
1074
+ if (!((0, tester_1.numberTester)(arrayScheme.minLength) && (0, tester_1.integerTester)(minLength))) {
1075
+ return new interfaces_1.EjvError({
1076
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1077
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER),
1078
+ data: data,
1079
+ errorScheme: arrayScheme,
1080
+ isSchemeError: true
1081
+ });
1082
+ }
1083
+ if (!(0, tester_1.minLengthTester)(valueAsArray, minLength)) {
1084
+ result = new interfaces_1.EjvError({
1085
+ type: constants_1.ERROR_TYPE.MIN_LENGTH,
1086
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MIN_LENGTH, {
1087
+ placeholders: ['' + minLength]
1088
+ }),
1089
+ data,
1090
+ path: _options.path,
1091
+ errorScheme: arrayScheme,
1092
+ errorData: value
1093
+ });
1094
+ break;
1095
+ }
1096
+ }
1097
+ if ((0, tester_1.definedTester)(arrayScheme.maxLength)) {
1098
+ const maxLength = arrayScheme.maxLength;
1099
+ if (!((0, tester_1.numberTester)(arrayScheme.maxLength) && (0, tester_1.integerTester)(maxLength))) {
1100
+ return new interfaces_1.EjvError({
1101
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1102
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER),
1103
+ data: data,
1104
+ errorScheme: arrayScheme,
1105
+ isSchemeError: true
1106
+ });
1107
+ }
1108
+ if (!(0, tester_1.maxLengthTester)(valueAsArray, maxLength)) {
1109
+ result = new interfaces_1.EjvError({
1110
+ type: constants_1.ERROR_TYPE.MAX_LENGTH,
1111
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.MAX_LENGTH, {
1112
+ placeholders: ['' + maxLength]
1113
+ }),
1114
+ data,
1115
+ path: _options.path,
1116
+ errorScheme: arrayScheme,
1117
+ errorData: value
1118
+ });
1119
+ break;
1120
+ }
1121
+ }
1122
+ if ((0, tester_1.definedTester)(arrayScheme.unique)) {
1123
+ if (!(0, tester_1.booleanTester)(arrayScheme.unique)) {
1124
+ return new interfaces_1.EjvError({
1125
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1126
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.UNIQUE_SHOULD_BE_BOOLEAN),
1127
+ data: data,
1128
+ errorScheme: arrayScheme,
1129
+ isSchemeError: true
1130
+ });
1131
+ }
1132
+ if (arrayScheme.unique && !(0, tester_1.uniqueItemsTester)(valueAsArray)) {
1133
+ result = new interfaces_1.EjvError({
1134
+ type: constants_1.ERROR_TYPE.UNIQUE_ITEMS,
1135
+ message: constants_1.ERROR_MESSAGE.UNIQUE_ITEMS,
1136
+ data,
1137
+ path: _options.path,
1138
+ errorScheme: arrayScheme,
1139
+ errorData: value
1140
+ });
1141
+ break;
1142
+ }
1143
+ }
1144
+ if ((0, tester_1.definedTester)(arrayScheme.items)) {
1145
+ // convert array to object
1146
+ if (valueAsArray.length > 0) {
1147
+ const now = new Date();
1148
+ const tempKeyArr = valueAsArray.map((_value, i) => {
1149
+ return '' + (+now + i);
1150
+ });
1151
+ if ((0, tester_1.stringTester)(arrayScheme.items) // by DataType
1152
+ || ((0, tester_1.arrayTester)(arrayScheme.items) && (0, tester_1.arrayTypeOfTester)(arrayScheme.items, constants_1.DATA_TYPE.STRING)) // by DataType[]
1153
+ ) {
1154
+ const itemTypes = ((0, tester_1.arrayTester)(arrayScheme.items)
1155
+ ? arrayScheme.items
1156
+ : [arrayScheme.items]);
1157
+ const itemTypeError = itemTypes.find((type) => {
1158
+ return !(0, tester_1.definedTester)(type)
1159
+ || !(0, tester_1.stringTester)(type)
1160
+ || !(0, tester_1.enumTester)(type, allDataType);
1161
+ });
1162
+ if (itemTypeError) {
1163
+ return new interfaces_1.EjvError({
1164
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1165
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.SCHEMES_HAS_INVALID_TYPE, {
1166
+ placeholders: [itemTypeError]
1167
+ }),
1168
+ data: data,
1169
+ errorScheme: scheme,
1170
+ isSchemeError: true
1171
+ });
1172
+ }
1173
+ const partialData = {};
1174
+ const partialSchemes = [];
1175
+ tempKeyArr.forEach((tempKey, i) => {
1176
+ partialData[tempKey] = valueAsArray[i];
1177
+ partialSchemes.push({
1178
+ key: tempKey,
1179
+ type: itemTypes
1180
+ });
1181
+ });
1182
+ scheme.parent = arrayScheme;
1183
+ // call recursively
1184
+ const partialResult = _ejv(partialData, partialSchemes, _options);
1185
+ // convert new EjvError
1186
+ if (partialResult) {
1187
+ let errorMsg;
1188
+ if ((0, tester_1.arrayTester)(arrayScheme.items)) {
1189
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ITEMS_TYPE, {
1190
+ placeholders: [JSON.stringify(itemTypes)]
1191
+ });
1192
+ }
1193
+ else {
1194
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ITEMS_TYPE, {
1195
+ placeholders: [arrayScheme.items]
1196
+ });
1197
+ }
1198
+ const partialKeys = (partialResult.path || '').split('/');
1199
+ const partialKey = partialKeys[partialKeys.length - 1];
1200
+ const partialScheme = partialSchemes.find((_scheme) => {
1201
+ return _scheme.key === partialKey;
1202
+ });
1203
+ const partialKeyIndex = partialSchemes.indexOf(partialScheme);
1204
+ result = new interfaces_1.EjvError({
1205
+ type: constants_1.ERROR_TYPE.ITEMS_TYPE,
1206
+ message: errorMsg,
1207
+ data,
1208
+ path: [..._options.path, '' + partialKeyIndex],
1209
+ errorScheme: partialScheme,
1210
+ errorData: partialData[partialKey]
1211
+ });
1212
+ }
1213
+ break;
1214
+ }
1215
+ else if (((0, tester_1.objectTester)(arrayScheme.items) && arrayScheme.items !== null) // by Scheme
1216
+ || ((0, tester_1.arrayTester)(arrayScheme.items) && (0, tester_1.arrayTypeOfTester)(arrayScheme.items, constants_1.DATA_TYPE.OBJECT)) // by Scheme[]
1217
+ ) {
1218
+ const itemsAsSchemes = (0, tester_1.arrayTester)(arrayScheme.items)
1219
+ ? arrayScheme.items
1220
+ : [arrayScheme.items];
1221
+ let partialError = null;
1222
+ // use for() instead of forEach() to break
1223
+ const valueLength = valueAsArray.length;
1224
+ for (let arrIndex = 0; arrIndex < valueLength; arrIndex++) {
1225
+ const oneValue = valueAsArray[arrIndex];
1226
+ const partialData = {};
1227
+ const partialSchemes = [];
1228
+ const tempKeyForThisValue = tempKeyArr[arrIndex];
1229
+ partialData[tempKeyForThisValue] = oneValue;
1230
+ partialSchemes.push(...itemsAsSchemes.map((oneScheme) => {
1231
+ const newScheme = (0, util_1.clone)(oneScheme); // divide instance
1232
+ newScheme.key = tempKeyForThisValue;
1233
+ return newScheme;
1234
+ }));
1235
+ const partialResults = partialSchemes.map((partialScheme) => {
1236
+ // call recursively
1237
+ const partialResult = _ejv(partialData, [partialScheme], _options);
1238
+ if (partialResult) {
1239
+ partialResult.path = (partialResult.path || '').replace(tempKeyForThisValue, '' + arrIndex);
1240
+ }
1241
+ return partialResult;
1242
+ });
1243
+ if (!partialResults.some((oneResult) => oneResult === null)) {
1244
+ partialError = partialResults.find((oneResult) => {
1245
+ return !!oneResult;
1246
+ });
1247
+ break;
1248
+ }
1249
+ }
1250
+ if (partialError) {
1251
+ let errorType;
1252
+ let errorMsg;
1253
+ if (!!itemsAsSchemes && itemsAsSchemes.length > 1) {
1254
+ errorType = constants_1.ERROR_TYPE.ITEMS_SCHEMES;
1255
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.ITEMS_SCHEMES, {
1256
+ placeholders: [JSON.stringify(itemsAsSchemes)]
1257
+ });
1258
+ }
1259
+ else {
1260
+ errorType = partialError.type;
1261
+ errorMsg = partialError.message;
1262
+ if (errorType === constants_1.ERROR_TYPE.REQUIRED) {
1263
+ // REQUIRED in array is TYPE_MISMATCH except with nullable === true
1264
+ errorType = constants_1.ERROR_TYPE.TYPE_MISMATCH;
1265
+ errorMsg = (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.TYPE_MISMATCH, {
1266
+ placeholders: [JSON.stringify(arrayScheme.items)]
1267
+ });
1268
+ }
1269
+ }
1270
+ result = new interfaces_1.EjvError({
1271
+ type: errorType,
1272
+ message: errorMsg,
1273
+ data
1274
+ });
1275
+ if (errorType === constants_1.ERROR_TYPE.INVALID_SCHEMES) {
1276
+ result.errorScheme = arrayScheme;
1277
+ result.isSchemeError = true;
1278
+ result.isDataError = false;
1279
+ }
1280
+ else {
1281
+ result.path = partialError.path;
1282
+ result.errorData = partialError.errorData;
1283
+ }
1284
+ break;
1285
+ }
1286
+ }
1287
+ else {
1288
+ return new interfaces_1.EjvError({
1289
+ type: constants_1.ERROR_TYPE.INVALID_SCHEMES,
1290
+ message: (0, util_1.createErrorMsg)(constants_1.ERROR_MESSAGE.INVALID_ITEMS_SCHEME, {
1291
+ placeholders: [JSON.stringify(arrayScheme.items)]
1292
+ }),
1293
+ data: data,
1294
+ errorScheme: arrayScheme,
1295
+ isSchemeError: true
1296
+ });
1297
+ }
1298
+ }
1299
+ }
1300
+ break;
1301
+ }
1302
+ }
1303
+ if (result) {
1304
+ break;
1305
+ }
1306
+ }
1307
+ if (result !== null && (0, tester_1.definedTester)(options.customErrorMsg)) {
1308
+ const customErrorMsgObj = options.customErrorMsg;
1309
+ // override error message
1310
+ const customMsg = customErrorMsgObj[result.type];
1311
+ if ((0, tester_1.definedTester)(customMsg)) {
1312
+ result.message = customMsg;
1313
+ }
1314
+ }
1315
+ return result;
1316
+ };
1317
+ const ejv = (data, schemes, options) => {
1318
+ // check data itself
1319
+ if (!(0, tester_1.definedTester)(data) || !(0, tester_1.objectTester)(data) || data === null) {
1320
+ return new interfaces_1.EjvError({
1321
+ type: constants_1.ERROR_TYPE.NO_DATA,
1322
+ message: constants_1.ERROR_MESSAGE.NO_DATA,
1323
+ data: data,
1324
+ path: undefined,
1325
+ errorScheme: undefined,
1326
+ errorData: data,
1327
+ isSchemeError: false
1328
+ });
1329
+ }
1330
+ const internalOption = (options
1331
+ ? Object.assign({}, options) : {});
1332
+ if (!(0, tester_1.definedTester)(internalOption.path)) {
1333
+ internalOption.path = [];
1334
+ }
1335
+ return _ejv(data, schemes, internalOption);
1336
+ };
1337
+ exports.ejv = ejv;
1338
+ //# sourceMappingURL=ejv.js.map