ejv 2.0.4 → 2.1.0

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