ejv 1.1.9 → 1.1.11

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.
package/build/ejv.js CHANGED
@@ -1,684 +1,687 @@
1
- "use strict";
2
- var __spreadArrays = (this && this.__spreadArrays) || function () {
3
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
5
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6
- r[k] = a[j];
7
- return r;
8
- };
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.ejv = void 0;
11
- var interfaces_1 = require("./interfaces");
12
- var constants_1 = require("./constants");
13
- var tester_1 = require("./tester");
14
- var util_1 = require("./util");
15
- var _ejv = function (data, schemes, options) {
16
- if (options === void 0) { options = {
17
- path: []
18
- }; }
19
- // check schemes
20
- if (!tester_1.arrayTester(schemes)) {
21
- throw new Error(constants_1.ErrorMsg.NO_ARRAY_SCHEME);
22
- }
23
- if (!tester_1.arrayTypeOfTester(schemes, constants_1.DataType.OBJECT)) {
24
- throw new Error(constants_1.ErrorMsg.NO_OBJECT_ARRAY_SCHEME);
25
- }
26
- if (!tester_1.minLengthTester(schemes, 1)) {
27
- throw new Error(constants_1.ErrorMsg.EMPTY_SCHEME);
28
- }
29
- // check data by schemes
30
- var result = null;
31
- // use for() instead of forEach() to stop
32
- var schemeLength = schemes.length;
33
- var _loop_1 = function (i) {
34
- var _options = util_1.clone(options); // divide instance
35
- if (!tester_1.definedTester(_options.path)) {
36
- _options.path = [];
37
- }
38
- var scheme = schemes[i];
39
- var key = scheme.key;
40
- var value;
41
- if (!!key) {
42
- value = data[key];
43
- _options.path.push(key);
44
- }
45
- var types = void 0;
46
- if (!tester_1.definedTester(scheme.type)) {
47
- throw new Error(constants_1.ErrorMsg.SCHEMES_SHOULD_HAVE_TYPE);
48
- }
49
- if (!tester_1.arrayTester(scheme.type)) {
50
- types = [scheme.type];
51
- }
52
- else {
53
- types = scheme.type;
54
- }
55
- var allDataType = Object.values(constants_1.DataType);
56
- var errorType = types.find(function (type) {
57
- return !(tester_1.stringTester(type) && tester_1.enumTester(type, allDataType));
58
- });
59
- if (!!errorType) {
60
- throw new Error(constants_1.ErrorMsg.SCHEMES_HAS_INVALID_TYPE.replace(constants_1.ErrorMsgCursorA, errorType));
61
- }
62
- if (!tester_1.uniqueItemsTester(types)) {
63
- throw new Error(constants_1.ErrorMsg.SCHEMES_HAS_DUPLICATED_TYPE);
64
- }
65
- if (!tester_1.definedTester(value)) {
66
- if (scheme.optional !== true) {
67
- result = new interfaces_1.EjvError(constants_1.ErrorType.REQUIRED, constants_1.ErrorMsg.REQUIRED, _options.path, data, value);
68
- return "break";
69
- }
70
- else {
71
- return "continue";
72
- }
73
- }
74
- if (value === null) {
75
- if (scheme.nullable !== true) {
76
- result = new interfaces_1.EjvError(constants_1.ErrorType.REQUIRED, constants_1.ErrorMsg.REQUIRED, _options.path, data, value);
77
- return "break";
78
- }
79
- else {
80
- return "continue";
81
- }
82
- }
83
- var typeResolved = types.find(function (type) {
84
- return tester_1.typeTester(value, type);
85
- });
86
- if (!typeResolved) {
87
- if (!tester_1.arrayTester(scheme.type)) {
88
- result = new interfaces_1.EjvError(constants_1.ErrorType.TYPE_MISMATCH, constants_1.ErrorMsg.TYPE_MISMATCH.replace(constants_1.ErrorMsgCursorA, scheme.type), _options.path, data, value);
89
- }
90
- else {
91
- result = new interfaces_1.EjvError(constants_1.ErrorType.TYPE_MISMATCH_ONE_OF, constants_1.ErrorMsg.TYPE_MISMATCH_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.type)), _options.path, data, value);
92
- }
93
- return "break";
94
- }
95
- // additional check for type resolved
96
- switch (typeResolved) {
97
- case constants_1.DataType.NUMBER:
98
- var valueAsNumber = value;
99
- if (tester_1.definedTester(scheme.enum)) {
100
- if (!tester_1.arrayTester(scheme.enum)) {
101
- throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_ARRAY);
102
- }
103
- var enumArr = scheme.enum;
104
- if (!tester_1.arrayTypeOfTester(enumArr, constants_1.DataType.NUMBER)) {
105
- throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_NUMBERS);
106
- }
107
- if (!tester_1.enumTester(valueAsNumber, enumArr)) {
108
- result = new interfaces_1.EjvError(constants_1.ErrorType.ONE_OF, constants_1.ErrorMsg.ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(enumArr)), _options.path, data, value);
109
- break;
110
- }
111
- }
112
- if (tester_1.definedTester(scheme.enumReverse)) {
113
- var enumReverseArr = scheme.enumReverse;
114
- if (!tester_1.arrayTester(enumReverseArr)) {
115
- throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
116
- }
117
- if (!tester_1.arrayTypeOfTester(enumReverseArr, constants_1.DataType.NUMBER)) {
118
- throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_NUMBERS);
119
- }
120
- if (tester_1.enumTester(valueAsNumber, enumReverseArr)) {
121
- result = new interfaces_1.EjvError(constants_1.ErrorType.NOT_ONE_OF, constants_1.ErrorMsg.NOT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(enumReverseArr)), _options.path, data, value);
122
- break;
123
- }
124
- }
125
- if (tester_1.definedTester(scheme.min)) {
126
- if (!tester_1.numberTester(scheme.min)) {
127
- throw new Error(constants_1.ErrorMsg.MIN_SHOULD_BE_NUMBER);
128
- }
129
- if (tester_1.definedTester(scheme.exclusiveMin)) {
130
- if (!tester_1.booleanTester(scheme.exclusiveMin)) {
131
- throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN);
132
- }
133
- if (scheme.exclusiveMin === true) {
134
- if (!tester_1.exclusiveMinNumberTester(valueAsNumber, scheme.min)) {
135
- result = new interfaces_1.EjvError(constants_1.ErrorType.GREATER_THAN, constants_1.ErrorMsg.GREATER_THAN.replace(constants_1.ErrorMsgCursorA, '' + scheme.min), _options.path, data, value);
136
- break;
137
- }
138
- }
139
- else {
140
- if (!tester_1.minNumberTester(valueAsNumber, scheme.min)) {
141
- result = new interfaces_1.EjvError(constants_1.ErrorType.GREATER_THAN_OR_EQUAL, constants_1.ErrorMsg.GREATER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.min), _options.path, data, value);
142
- break;
143
- }
144
- }
145
- }
146
- else {
147
- if (!tester_1.minNumberTester(valueAsNumber, scheme.min)) {
148
- result = new interfaces_1.EjvError(constants_1.ErrorType.GREATER_THAN_OR_EQUAL, constants_1.ErrorMsg.GREATER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.min), _options.path, data, value);
149
- break;
150
- }
151
- }
152
- }
153
- if (tester_1.definedTester(scheme.max)) {
154
- if (!tester_1.numberTester(scheme.max)) {
155
- throw new Error(constants_1.ErrorMsg.MAX_SHOULD_BE_NUMBER);
156
- }
157
- if (tester_1.definedTester(scheme.exclusiveMax)) {
158
- if (!tester_1.booleanTester(scheme.exclusiveMax)) {
159
- throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN);
160
- }
161
- if (scheme.exclusiveMax === true) {
162
- if (!tester_1.exclusiveMaxNumberTester(valueAsNumber, scheme.max)) {
163
- result = new interfaces_1.EjvError(constants_1.ErrorType.SMALLER_THAN, constants_1.ErrorMsg.SMALLER_THAN.replace(constants_1.ErrorMsgCursorA, '' + scheme.max), _options.path, data, value);
164
- break;
165
- }
166
- }
167
- else {
168
- if (!tester_1.maxNumberTester(valueAsNumber, scheme.max)) {
169
- result = new interfaces_1.EjvError(constants_1.ErrorType.SMALLER_THAN_OR_EQUAL, constants_1.ErrorMsg.SMALLER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.max), _options.path, data, value);
170
- break;
171
- }
172
- }
173
- }
174
- else {
175
- if (!tester_1.maxNumberTester(valueAsNumber, scheme.max)) {
176
- result = new interfaces_1.EjvError(constants_1.ErrorType.SMALLER_THAN_OR_EQUAL, constants_1.ErrorMsg.SMALLER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.max), _options.path, data, value);
177
- break;
178
- }
179
- }
180
- }
181
- if (tester_1.definedTester(scheme.format)) {
182
- var formats = void 0;
183
- var allNumberFormat_1 = Object.values(constants_1.NumberFormat);
184
- if (!tester_1.arrayTester(scheme.format)) {
185
- var formatAsString = scheme.format;
186
- if (!tester_1.enumTester(formatAsString, allNumberFormat_1)) {
187
- throw new Error(constants_1.ErrorMsg.INVALID_NUMBER_FORMAT.replace(constants_1.ErrorMsgCursorA, formatAsString));
188
- }
189
- formats = [scheme.format];
190
- }
191
- else {
192
- var formatAsArray = scheme.format;
193
- var errorFormat = formatAsArray.find(function (format) {
194
- return !tester_1.enumTester(format, allNumberFormat_1);
195
- });
196
- if (!!errorFormat) {
197
- throw new Error(constants_1.ErrorMsg.INVALID_NUMBER_FORMAT.replace(constants_1.ErrorMsgCursorA, errorFormat));
198
- }
199
- formats = scheme.format;
200
- }
201
- if (!formats.some(function (format) {
202
- var valid = false;
203
- switch (format) {
204
- case constants_1.NumberFormat.INTEGER:
205
- valid = tester_1.integerTester(value);
206
- break;
207
- case constants_1.NumberFormat.INDEX:
208
- valid = tester_1.indexTester(value);
209
- break;
210
- }
211
- return valid;
212
- })) {
213
- if (!tester_1.arrayTester(scheme.format)) {
214
- result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT, constants_1.ErrorMsg.FORMAT.replace(constants_1.ErrorMsgCursorA, scheme.format), _options.path, data, value);
215
- }
216
- else {
217
- result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT_ONE_OF, constants_1.ErrorMsg.FORMAT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.format)), _options.path, data, value);
218
- }
219
- break;
220
- }
221
- }
222
- break;
223
- case constants_1.DataType.STRING:
224
- var valueAsString = value;
225
- if (tester_1.definedTester(scheme.enum)) {
226
- if (!tester_1.arrayTester(scheme.enum)) {
227
- throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_ARRAY);
228
- }
229
- var enumArr = scheme.enum;
230
- if (!tester_1.arrayTypeOfTester(enumArr, constants_1.DataType.STRING)) {
231
- throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_STRINGS);
232
- }
233
- if (!tester_1.enumTester(valueAsString, enumArr)) {
234
- result = new interfaces_1.EjvError(constants_1.ErrorType.ONE_OF, constants_1.ErrorMsg.ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.enum)), _options.path, data, value);
235
- break;
236
- }
237
- }
238
- if (tester_1.definedTester(scheme.enumReverse)) {
239
- if (!tester_1.arrayTester(scheme.enumReverse)) {
240
- throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
241
- }
242
- var enumReverseArr = scheme.enumReverse;
243
- if (!tester_1.arrayTypeOfTester(enumReverseArr, constants_1.DataType.STRING)) {
244
- throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_STRINGS);
245
- }
246
- if (tester_1.enumTester(valueAsString, enumReverseArr)) {
247
- result = new interfaces_1.EjvError(constants_1.ErrorType.NOT_ONE_OF, constants_1.ErrorMsg.NOT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(enumReverseArr)), _options.path, data, value);
248
- break;
249
- }
250
- }
251
- if (tester_1.definedTester(scheme.length)) {
252
- var length_1 = scheme.length;
253
- if (!(tester_1.numberTester(length_1) && tester_1.integerTester(length_1))) {
254
- throw new Error(constants_1.ErrorMsg.LENGTH_SHOULD_BE_INTEGER);
255
- }
256
- if (!tester_1.lengthTester(valueAsString, length_1)) {
257
- result = new interfaces_1.EjvError(constants_1.ErrorType.LENGTH, constants_1.ErrorMsg.LENGTH.replace(constants_1.ErrorMsgCursorA, '' + length_1), _options.path, data, value);
258
- break;
259
- }
260
- }
261
- if (tester_1.definedTester(scheme.minLength)) {
262
- var minLength = scheme.minLength;
263
- if (!(tester_1.numberTester(minLength) && tester_1.integerTester(minLength))) {
264
- throw new Error(constants_1.ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
265
- }
266
- if (!tester_1.minLengthTester(valueAsString, minLength)) {
267
- result = new interfaces_1.EjvError(constants_1.ErrorType.MIN_LENGTH, constants_1.ErrorMsg.MIN_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + minLength), _options.path, data, value);
268
- break;
269
- }
270
- }
271
- if (tester_1.definedTester(scheme.maxLength)) {
272
- var maxLength = scheme.maxLength;
273
- if (!(tester_1.numberTester(maxLength) && tester_1.integerTester(maxLength))) {
274
- throw new Error(constants_1.ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
275
- }
276
- if (!tester_1.maxLengthTester(valueAsString, maxLength)) {
277
- result = new interfaces_1.EjvError(constants_1.ErrorType.MAX_LENGTH, constants_1.ErrorMsg.MAX_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + maxLength), _options.path, data, value);
278
- break;
279
- }
280
- }
281
- if (tester_1.definedTester(scheme.format)) {
282
- var formats = void 0;
283
- var allStringFormat_1 = Object.values(constants_1.StringFormat);
284
- if (!tester_1.arrayTester(scheme.format)) {
285
- var formatAsString = scheme.format;
286
- if (!tester_1.enumTester(formatAsString, allStringFormat_1)) {
287
- throw new Error(constants_1.ErrorMsg.INVALID_STRING_FORMAT.replace(constants_1.ErrorMsgCursorA, formatAsString));
288
- }
289
- formats = [scheme.format];
290
- }
291
- else {
292
- var formatAsArray = scheme.format;
293
- var errorFormat = formatAsArray.find(function (format) {
294
- return !tester_1.enumTester(format, allStringFormat_1);
295
- });
296
- if (!!errorFormat) {
297
- throw new Error(constants_1.ErrorMsg.INVALID_STRING_FORMAT.replace(constants_1.ErrorMsgCursorA, errorFormat));
298
- }
299
- formats = scheme.format;
300
- }
301
- if (!formats.some(function (format) {
302
- var valid = false;
303
- switch (format) {
304
- case constants_1.StringFormat.EMAIL:
305
- valid = tester_1.emailTester(value);
306
- break;
307
- case constants_1.StringFormat.DATE:
308
- valid = tester_1.dateFormatTester(value);
309
- break;
310
- case constants_1.StringFormat.TIME:
311
- valid = tester_1.timeFormatTester(value);
312
- break;
313
- case constants_1.StringFormat.DATE_TIME:
314
- valid = tester_1.dateTimeFormatTester(value);
315
- break;
316
- }
317
- return valid;
318
- })) {
319
- if (!tester_1.arrayTester(scheme.format)) {
320
- result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT, constants_1.ErrorMsg.FORMAT.replace(constants_1.ErrorMsgCursorA, scheme.format), _options.path, data, value);
321
- }
322
- else {
323
- result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT_ONE_OF, constants_1.ErrorMsg.FORMAT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.format)), _options.path, data, value);
324
- }
325
- break;
326
- }
327
- }
328
- if (tester_1.definedTester(scheme.pattern)) {
329
- // check parameter
330
- if (scheme.pattern === null) {
331
- throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
332
- .replace(constants_1.ErrorMsgCursorA, 'null'));
333
- }
334
- var isValidPattern_1 = function (pattern) {
335
- return (tester_1.stringTester(pattern) && tester_1.minLengthTester(pattern, 1))
336
- || (tester_1.regExpTester(pattern) && pattern.toString() !== '/(?:)/' && pattern.toString() !== '/null/');
337
- };
338
- var patternToString_1 = function (pattern) {
339
- var regExpStr;
340
- if (pattern === null) {
341
- regExpStr = '/null/';
342
- }
343
- else if (tester_1.stringTester(pattern)) {
344
- if (tester_1.minLengthTester(pattern, 1)) {
345
- regExpStr = new RegExp(pattern).toString();
346
- }
347
- else {
348
- regExpStr = '//';
349
- }
350
- }
351
- else {
352
- regExpStr = pattern.toString();
353
- }
354
- // empty regular expression
355
- if (regExpStr === '/(?:)/') {
356
- regExpStr = '//';
357
- }
358
- return regExpStr;
359
- };
360
- var createArrayErrorMsg_1 = function (patternsAsArray) {
361
- return '[' + patternsAsArray.map(function (onePattern) {
362
- return patternToString_1(onePattern);
363
- }).join(', ') + ']';
364
- };
365
- if (tester_1.arrayTester(scheme.pattern)) {
366
- var patternsAsArray_1 = scheme.pattern;
367
- if (!tester_1.minLengthTester(patternsAsArray_1, 1)) { // empty array
368
- throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
369
- .replace(constants_1.ErrorMsgCursorA, createArrayErrorMsg_1(patternsAsArray_1)));
370
- }
371
- var regExpPatterns = patternsAsArray_1.map(function (pattern) {
372
- if (!isValidPattern_1(pattern)) {
373
- throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
374
- .replace(constants_1.ErrorMsgCursorA, createArrayErrorMsg_1(patternsAsArray_1)));
375
- }
376
- return new RegExp(pattern);
377
- });
378
- // check value
379
- if (!regExpPatterns.some(function (regexp) {
380
- return tester_1.stringRegExpTester(value, regexp);
381
- })) {
382
- result = new interfaces_1.EjvError(constants_1.ErrorType.PATTERN_ONE_OF, constants_1.ErrorMsg.PATTERN_ONE_OF
383
- .replace(constants_1.ErrorMsgCursorA, createArrayErrorMsg_1(patternsAsArray_1)), _options.path, data, value);
384
- break;
385
- }
386
- }
387
- else {
388
- var patternAsOne = scheme.pattern;
389
- if (!isValidPattern_1(patternAsOne)) {
390
- throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
391
- .replace(constants_1.ErrorMsgCursorA, patternToString_1(patternAsOne)));
392
- }
393
- // check value
394
- var regExp = new RegExp(patternAsOne);
395
- if (!tester_1.stringRegExpTester(valueAsString, regExp)) {
396
- result = new interfaces_1.EjvError(constants_1.ErrorType.PATTERN, constants_1.ErrorMsg.PATTERN.replace(constants_1.ErrorMsgCursorA, patternToString_1(patternAsOne)), _options.path, data, value);
397
- break;
398
- }
399
- }
400
- }
401
- break;
402
- case constants_1.DataType.OBJECT:
403
- var valueAsObject = value;
404
- if (tester_1.definedTester(scheme.allowNoProperty)) {
405
- if (!tester_1.booleanTester(scheme.allowNoProperty)) {
406
- throw new Error(constants_1.ErrorMsg.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN);
407
- }
408
- if (scheme.allowNoProperty !== true && !tester_1.hasPropertyTester(valueAsObject)) {
409
- result = new interfaces_1.EjvError(constants_1.ErrorType.NO_PROPERTY, constants_1.ErrorMsg.NO_PROPERTY, _options.path, data, value);
410
- break;
411
- }
412
- }
413
- if (tester_1.definedTester(scheme.properties)) {
414
- if (!tester_1.arrayTester(scheme.properties)) {
415
- throw new Error(constants_1.ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY);
416
- }
417
- var properties = scheme.properties;
418
- if (!tester_1.minLengthTester(properties, 1)) {
419
- throw new Error(constants_1.ErrorMsg.PROPERTIES_SHOULD_HAVE_ITEMS);
420
- }
421
- if (!tester_1.arrayTypeOfTester(properties, constants_1.DataType.OBJECT)) {
422
- throw new Error(constants_1.ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT);
423
- }
424
- if (!tester_1.objectTester(value)) {
425
- result = new interfaces_1.EjvError(constants_1.ErrorType.TYPE_MISMATCH, constants_1.ErrorMsg.TYPE_MISMATCH.replace(constants_1.ErrorMsgCursorA, 'object'), _options.path, data, value);
426
- break;
427
- }
428
- var partialData = data[key];
429
- var partialScheme = scheme.properties;
430
- // call recursively
431
- result = _ejv(partialData, partialScheme, _options);
432
- if (!!result) {
433
- // inject original data
434
- result.data = data;
435
- }
436
- }
437
- break;
438
- case constants_1.DataType.DATE:
439
- var valueAsDate = value;
440
- if (tester_1.definedTester(scheme.min)) {
441
- if (!((tester_1.stringTester(scheme.min) && (tester_1.dateFormatTester(scheme.min) || tester_1.dateTimeFormatTester(scheme.min)))
442
- || tester_1.dateTester(scheme.min))) {
443
- throw new Error(constants_1.ErrorMsg.MIN_DATE_SHOULD_BE_DATE_OR_STRING);
444
- }
445
- if (tester_1.definedTester(scheme.exclusiveMin) && !tester_1.booleanTester(scheme.exclusiveMin)) {
446
- throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN);
447
- }
448
- var minDate = new Date(scheme.min);
449
- // adjust timezone
450
- if (tester_1.stringTester(scheme.min)) {
451
- // by minutes
452
- var timezoneOffset = minDate.getTimezoneOffset();
453
- minDate = new Date(+minDate + (timezoneOffset * 60 * 1000));
454
- }
455
- if (scheme.exclusiveMin !== true) {
456
- if (!tester_1.minDateTester(valueAsDate, minDate)) {
457
- result = new interfaces_1.EjvError(constants_1.ErrorType.AFTER_OR_SAME_DATE, constants_1.ErrorMsg.AFTER_OR_SAME_DATE.replace(constants_1.ErrorMsgCursorA, minDate.toISOString()), _options.path, data, value);
458
- break;
459
- }
460
- }
461
- else {
462
- if (!tester_1.exclusiveMinDateTester(valueAsDate, minDate)) {
463
- result = new interfaces_1.EjvError(constants_1.ErrorType.AFTER_DATE, constants_1.ErrorMsg.AFTER_DATE.replace(constants_1.ErrorMsgCursorA, minDate.toISOString()), _options.path, data, value);
464
- break;
465
- }
466
- }
467
- }
468
- if (tester_1.definedTester(scheme.max)) {
469
- if (!((tester_1.stringTester(scheme.max) && (tester_1.dateFormatTester(scheme.max) || tester_1.dateTimeFormatTester(scheme.max)))
470
- || tester_1.dateTester(scheme.max))) {
471
- throw new Error(constants_1.ErrorMsg.MAX_DATE_SHOULD_BE_DATE_OR_STRING);
472
- }
473
- if (tester_1.definedTester(scheme.exclusiveMax) && !tester_1.booleanTester(scheme.exclusiveMax)) {
474
- throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN);
475
- }
476
- var maxDate = new Date(scheme.max);
477
- // adjust timezone
478
- if (tester_1.stringTester(scheme.max)) {
479
- // by minutes
480
- var timezoneOffset = maxDate.getTimezoneOffset();
481
- maxDate = new Date(+maxDate + (timezoneOffset * 60 * 1000));
482
- }
483
- if (scheme.exclusiveMax !== true) {
484
- if (!tester_1.maxDateTester(valueAsDate, maxDate)) {
485
- result = new interfaces_1.EjvError(constants_1.ErrorType.BEFORE_OR_SAME_DATE, constants_1.ErrorMsg.BEFORE_OR_SAME_DATE.replace(constants_1.ErrorMsgCursorA, maxDate.toISOString()), _options.path, data, value);
486
- break;
487
- }
488
- }
489
- else {
490
- if (!tester_1.exclusiveMaxDateTester(valueAsDate, maxDate)) {
491
- result = new interfaces_1.EjvError(constants_1.ErrorType.BEFORE_DATE, constants_1.ErrorMsg.BEFORE_DATE.replace(constants_1.ErrorMsgCursorA, maxDate.toISOString()), _options.path, data, value);
492
- break;
493
- }
494
- }
495
- }
496
- break;
497
- case constants_1.DataType.ARRAY:
498
- var valueAsArray_1 = value;
499
- if (tester_1.definedTester(scheme.length)) {
500
- var length_2 = scheme.length;
501
- if (!(tester_1.numberTester(length_2) && tester_1.integerTester(length_2))) {
502
- throw new Error(constants_1.ErrorMsg.LENGTH_SHOULD_BE_INTEGER);
503
- }
504
- if (!tester_1.lengthTester(valueAsArray_1, length_2)) {
505
- result = new interfaces_1.EjvError(constants_1.ErrorType.LENGTH, constants_1.ErrorMsg.LENGTH.replace(constants_1.ErrorMsgCursorA, '' + length_2), _options.path, data, value);
506
- break;
507
- }
508
- }
509
- if (tester_1.definedTester(scheme.minLength)) {
510
- var minLength = scheme.minLength;
511
- if (!(tester_1.numberTester(scheme.minLength) && tester_1.integerTester(minLength))) {
512
- throw new Error(constants_1.ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
513
- }
514
- if (!tester_1.minLengthTester(valueAsArray_1, minLength)) {
515
- result = new interfaces_1.EjvError(constants_1.ErrorType.MIN_LENGTH, constants_1.ErrorMsg.MIN_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + minLength), _options.path, data, value);
516
- break;
517
- }
518
- }
519
- if (tester_1.definedTester(scheme.maxLength)) {
520
- var maxLength = scheme.maxLength;
521
- if (!(tester_1.numberTester(scheme.maxLength) && tester_1.integerTester(maxLength))) {
522
- throw new Error(constants_1.ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
523
- }
524
- if (!tester_1.maxLengthTester(valueAsArray_1, maxLength)) {
525
- result = new interfaces_1.EjvError(constants_1.ErrorType.MAX_LENGTH, constants_1.ErrorMsg.MAX_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + maxLength), _options.path, data, value);
526
- break;
527
- }
528
- }
529
- if (tester_1.definedTester(scheme.unique)) {
530
- if (!tester_1.booleanTester(scheme.unique)) {
531
- throw new Error(constants_1.ErrorMsg.UNIQUE_SHOULD_BE_BOOLEAN);
532
- }
533
- if (scheme.unique === true && !tester_1.uniqueItemsTester(valueAsArray_1)) {
534
- result = new interfaces_1.EjvError(constants_1.ErrorType.UNIQUE_ITEMS, constants_1.ErrorMsg.UNIQUE_ITEMS, _options.path, data, value);
535
- break;
536
- }
537
- }
538
- if (tester_1.definedTester(scheme.items)) {
539
- // convert array to object
540
- if (valueAsArray_1.length > 0) {
541
- var now_1 = new Date;
542
- var tempKeyArr = valueAsArray_1.map(function (value, i) {
543
- return '' + (+now_1 + i);
544
- });
545
- if (tester_1.stringTester(scheme.items) // by DataType
546
- || (tester_1.arrayTester(scheme.items) && tester_1.arrayTypeOfTester(scheme.items, constants_1.DataType.STRING)) // by DataType[]
547
- ) {
548
- var itemTypes_1 = (tester_1.arrayTester(scheme.items) ? scheme.items : [scheme.items]);
549
- var partialData_1 = {};
550
- var partialSchemes_1 = [];
551
- tempKeyArr.forEach(function (tempKey, i) {
552
- partialData_1[tempKey] = valueAsArray_1[i];
553
- partialSchemes_1.push({
554
- key: tempKey,
555
- type: itemTypes_1
556
- });
557
- });
558
- // call recursively
559
- var partialResult = _ejv(partialData_1, partialSchemes_1, _options);
560
- // convert new EjvError
561
- if (!!partialResult) {
562
- var errorMsg = void 0;
563
- if (tester_1.arrayTester(scheme.items)) {
564
- errorMsg = constants_1.ErrorMsg.ITEMS_TYPE.replace(constants_1.ErrorMsgCursorA, JSON.stringify(itemTypes_1));
565
- }
566
- else {
567
- errorMsg = constants_1.ErrorMsg.ITEMS_TYPE.replace(constants_1.ErrorMsgCursorA, scheme.items);
568
- }
569
- var partialKeys = partialResult.path.split('/');
570
- var partialKey_1 = partialKeys[partialKeys.length - 1];
571
- var partialScheme = partialSchemes_1.find(function (scheme) {
572
- return scheme.key === partialKey_1;
573
- });
574
- var partialKeyIndex = partialSchemes_1.indexOf(partialScheme);
575
- result = new interfaces_1.EjvError(constants_1.ErrorType.ITEMS_TYPE, errorMsg, __spreadArrays(_options.path, ['' + partialKeyIndex]), data, partialData_1[partialKey_1]);
576
- }
577
- break;
578
- }
579
- else if ((tester_1.objectTester(scheme.items) && scheme.items !== null) // by Scheme
580
- || (tester_1.arrayTester(scheme.items) && tester_1.arrayTypeOfTester(scheme.items, constants_1.DataType.OBJECT)) // by Scheme[]
581
- ) {
582
- var itemsAsSchemes = (tester_1.arrayTester(scheme.items) ? scheme.items : [scheme.items]);
583
- var partialError = null;
584
- // use for() instead of forEach() to break
585
- var valueLength = valueAsArray_1.length;
586
- var _loop_2 = function (arrIndex) {
587
- var oneValue = valueAsArray_1[arrIndex];
588
- var partialData = {};
589
- var partialSchemes = [];
590
- var tempKeyForThisValue = tempKeyArr[arrIndex];
591
- partialData[tempKeyForThisValue] = oneValue;
592
- partialSchemes.push.apply(partialSchemes, itemsAsSchemes.map(function (oneScheme) {
593
- var newScheme = util_1.clone(oneScheme); // divide instance
594
- newScheme.key = tempKeyForThisValue;
595
- return newScheme;
596
- }));
597
- var partialResults = partialSchemes.map(function (partialScheme) {
598
- // call recursively
599
- var partialResult = _ejv(partialData, [partialScheme], _options);
600
- if (!!partialResult) {
601
- partialResult.path = partialResult.path.replace(tempKeyForThisValue, '' + arrIndex);
602
- }
603
- return partialResult;
604
- });
605
- if (!partialResults.some(function (oneResult) { return oneResult === null; })) {
606
- partialError = partialResults.find(function (oneResult) {
607
- return !!oneResult;
608
- });
609
- return "break";
610
- }
611
- };
612
- for (var arrIndex = 0; arrIndex < valueLength; arrIndex++) {
613
- var state_2 = _loop_2(arrIndex);
614
- if (state_2 === "break")
615
- break;
616
- }
617
- if (!!partialError) {
618
- var errorType_1 = void 0;
619
- var errorMsg = void 0;
620
- if (!!itemsAsSchemes && itemsAsSchemes.length > 1) {
621
- errorType_1 = constants_1.ErrorType.ITEMS_SCHEMES;
622
- errorMsg = constants_1.ErrorMsg.ITEMS_SCHEMES.replace(constants_1.ErrorMsgCursorA, JSON.stringify(itemsAsSchemes));
623
- }
624
- else {
625
- errorType_1 = partialError.type;
626
- errorMsg = partialError.message;
627
- if (errorType_1 === constants_1.ErrorType.REQUIRED) {
628
- // REQUIRED in array is TYPE_MISMATCH except with nullable === true
629
- errorType_1 = constants_1.ErrorType.TYPE_MISMATCH;
630
- errorMsg = constants_1.ErrorMsg.TYPE_MISMATCH.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.items));
631
- }
632
- }
633
- result = new interfaces_1.EjvError(errorType_1, errorMsg, partialError.path.split('/'), data, partialError.errorData);
634
- break;
635
- }
636
- }
637
- else {
638
- throw new Error(constants_1.ErrorMsg.INVALID_ITEMS_SCHEME.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.items)));
639
- }
640
- }
641
- }
642
- break;
643
- }
644
- if (!!result) {
645
- return "break";
646
- }
647
- };
648
- for (var i = 0; i < schemeLength; i++) {
649
- var state_1 = _loop_1(i);
650
- if (state_1 === "break")
651
- break;
652
- }
653
- if (tester_1.definedTester(result) && tester_1.definedTester(options.customErrorMsg)) {
654
- var resultAsNotNull = result;
655
- var customErrorMsgObj = options.customErrorMsg;
656
- // override error message
657
- var customMsg = customErrorMsgObj[resultAsNotNull.type];
658
- if (tester_1.definedTester(customMsg)) {
659
- resultAsNotNull.message = customMsg;
660
- }
661
- }
662
- return result;
663
- };
664
- exports.ejv = function (data, schemes, options) {
665
- // check data itself
666
- if (!tester_1.definedTester(data) || !tester_1.objectTester(data) || data === null) {
667
- return new interfaces_1.EjvError(constants_1.ErrorType.REQUIRED, constants_1.ErrorMsg.NO_DATA, ['/'], data, undefined);
668
- }
669
- // check schemes itself
670
- if (!tester_1.definedTester(schemes) || schemes === null) {
671
- throw new Error(constants_1.ErrorMsg.NO_SCHEME);
672
- }
673
- if (!tester_1.arrayTester(schemes)) {
674
- throw new Error(constants_1.ErrorMsg.NO_ARRAY_SCHEME);
675
- }
676
- if (!tester_1.arrayTypeOfTester(schemes, constants_1.DataType.OBJECT)) {
677
- throw new Error(constants_1.ErrorMsg.NO_OBJECT_ARRAY_SCHEME);
678
- }
679
- if (!tester_1.minLengthTester(schemes, 1)) {
680
- throw new Error(constants_1.ErrorMsg.EMPTY_SCHEME);
681
- }
682
- return _ejv(data, schemes, options);
683
- };
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ejv = void 0;
13
+ var interfaces_1 = require("./interfaces");
14
+ var constants_1 = require("./constants");
15
+ var tester_1 = require("./tester");
16
+ var util_1 = require("./util");
17
+ var _ejv = function (data, schemes, options) {
18
+ if (options === void 0) { options = {
19
+ path: []
20
+ }; }
21
+ // check schemes
22
+ if (!(0, tester_1.arrayTester)(schemes)) {
23
+ throw new Error(constants_1.ErrorMsg.NO_ARRAY_SCHEME);
24
+ }
25
+ if (!(0, tester_1.arrayTypeOfTester)(schemes, constants_1.DataType.OBJECT)) {
26
+ throw new Error(constants_1.ErrorMsg.NO_OBJECT_ARRAY_SCHEME);
27
+ }
28
+ if (!(0, tester_1.minLengthTester)(schemes, 1)) {
29
+ throw new Error(constants_1.ErrorMsg.EMPTY_SCHEME);
30
+ }
31
+ // check data by schemes
32
+ var result = null;
33
+ // use for() instead of forEach() to stop
34
+ var schemeLength = schemes.length;
35
+ var _loop_1 = function (i) {
36
+ var _options = (0, util_1.clone)(options); // divide instance
37
+ if (!(0, tester_1.definedTester)(_options.path)) {
38
+ _options.path = [];
39
+ }
40
+ var scheme = schemes[i];
41
+ var key = scheme.key;
42
+ var value;
43
+ if (!!key) {
44
+ value = data[key];
45
+ _options.path.push(key);
46
+ }
47
+ var types = void 0;
48
+ if (!(0, tester_1.definedTester)(scheme.type)) {
49
+ throw new Error(constants_1.ErrorMsg.SCHEMES_SHOULD_HAVE_TYPE);
50
+ }
51
+ if (!(0, tester_1.arrayTester)(scheme.type)) {
52
+ types = [scheme.type];
53
+ }
54
+ else {
55
+ types = scheme.type;
56
+ }
57
+ var allDataType = Object.values(constants_1.DataType);
58
+ var errorType = types.find(function (type) {
59
+ return !((0, tester_1.stringTester)(type) && (0, tester_1.enumTester)(type, allDataType));
60
+ });
61
+ if (!!errorType) {
62
+ throw new Error(constants_1.ErrorMsg.SCHEMES_HAS_INVALID_TYPE.replace(constants_1.ErrorMsgCursorA, errorType));
63
+ }
64
+ if (!(0, tester_1.uniqueItemsTester)(types)) {
65
+ throw new Error(constants_1.ErrorMsg.SCHEMES_HAS_DUPLICATED_TYPE);
66
+ }
67
+ if (!(0, tester_1.definedTester)(value)) {
68
+ if (scheme.optional !== true) {
69
+ result = new interfaces_1.EjvError(constants_1.ErrorType.REQUIRED, constants_1.ErrorMsg.REQUIRED, _options.path, data, value);
70
+ return "break";
71
+ }
72
+ else {
73
+ return "continue";
74
+ }
75
+ }
76
+ if (value === null) {
77
+ if (scheme.nullable !== true) {
78
+ result = new interfaces_1.EjvError(constants_1.ErrorType.REQUIRED, constants_1.ErrorMsg.REQUIRED, _options.path, data, value);
79
+ return "break";
80
+ }
81
+ else {
82
+ return "continue";
83
+ }
84
+ }
85
+ var typeResolved = types.find(function (type) {
86
+ return (0, tester_1.typeTester)(value, type);
87
+ });
88
+ if (!typeResolved) {
89
+ if (!(0, tester_1.arrayTester)(scheme.type)) {
90
+ result = new interfaces_1.EjvError(constants_1.ErrorType.TYPE_MISMATCH, constants_1.ErrorMsg.TYPE_MISMATCH.replace(constants_1.ErrorMsgCursorA, scheme.type), _options.path, data, value);
91
+ }
92
+ else {
93
+ result = new interfaces_1.EjvError(constants_1.ErrorType.TYPE_MISMATCH_ONE_OF, constants_1.ErrorMsg.TYPE_MISMATCH_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.type)), _options.path, data, value);
94
+ }
95
+ return "break";
96
+ }
97
+ // additional check for type resolved
98
+ switch (typeResolved) {
99
+ case constants_1.DataType.NUMBER:
100
+ var valueAsNumber = value;
101
+ if ((0, tester_1.definedTester)(scheme.enum)) {
102
+ if (!(0, tester_1.arrayTester)(scheme.enum)) {
103
+ throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_ARRAY);
104
+ }
105
+ var enumArr = scheme.enum;
106
+ if (!(0, tester_1.arrayTypeOfTester)(enumArr, constants_1.DataType.NUMBER)) {
107
+ throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_NUMBERS);
108
+ }
109
+ if (!(0, tester_1.enumTester)(valueAsNumber, enumArr)) {
110
+ result = new interfaces_1.EjvError(constants_1.ErrorType.ONE_OF, constants_1.ErrorMsg.ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(enumArr)), _options.path, data, value);
111
+ break;
112
+ }
113
+ }
114
+ if ((0, tester_1.definedTester)(scheme.enumReverse)) {
115
+ var enumReverseArr = scheme.enumReverse;
116
+ if (!(0, tester_1.arrayTester)(enumReverseArr)) {
117
+ throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
118
+ }
119
+ if (!(0, tester_1.arrayTypeOfTester)(enumReverseArr, constants_1.DataType.NUMBER)) {
120
+ throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_NUMBERS);
121
+ }
122
+ if ((0, tester_1.enumTester)(valueAsNumber, enumReverseArr)) {
123
+ result = new interfaces_1.EjvError(constants_1.ErrorType.NOT_ONE_OF, constants_1.ErrorMsg.NOT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(enumReverseArr)), _options.path, data, value);
124
+ break;
125
+ }
126
+ }
127
+ if ((0, tester_1.definedTester)(scheme.min)) {
128
+ if (!(0, tester_1.numberTester)(scheme.min)) {
129
+ throw new Error(constants_1.ErrorMsg.MIN_SHOULD_BE_NUMBER);
130
+ }
131
+ if ((0, tester_1.definedTester)(scheme.exclusiveMin)) {
132
+ if (!(0, tester_1.booleanTester)(scheme.exclusiveMin)) {
133
+ throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN);
134
+ }
135
+ if (scheme.exclusiveMin === true) {
136
+ if (!(0, tester_1.exclusiveMinNumberTester)(valueAsNumber, scheme.min)) {
137
+ result = new interfaces_1.EjvError(constants_1.ErrorType.GREATER_THAN, constants_1.ErrorMsg.GREATER_THAN.replace(constants_1.ErrorMsgCursorA, '' + scheme.min), _options.path, data, value);
138
+ break;
139
+ }
140
+ }
141
+ else {
142
+ if (!(0, tester_1.minNumberTester)(valueAsNumber, scheme.min)) {
143
+ result = new interfaces_1.EjvError(constants_1.ErrorType.GREATER_THAN_OR_EQUAL, constants_1.ErrorMsg.GREATER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.min), _options.path, data, value);
144
+ break;
145
+ }
146
+ }
147
+ }
148
+ else {
149
+ if (!(0, tester_1.minNumberTester)(valueAsNumber, scheme.min)) {
150
+ result = new interfaces_1.EjvError(constants_1.ErrorType.GREATER_THAN_OR_EQUAL, constants_1.ErrorMsg.GREATER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.min), _options.path, data, value);
151
+ break;
152
+ }
153
+ }
154
+ }
155
+ if ((0, tester_1.definedTester)(scheme.max)) {
156
+ if (!(0, tester_1.numberTester)(scheme.max)) {
157
+ throw new Error(constants_1.ErrorMsg.MAX_SHOULD_BE_NUMBER);
158
+ }
159
+ if ((0, tester_1.definedTester)(scheme.exclusiveMax)) {
160
+ if (!(0, tester_1.booleanTester)(scheme.exclusiveMax)) {
161
+ throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN);
162
+ }
163
+ if (scheme.exclusiveMax === true) {
164
+ if (!(0, tester_1.exclusiveMaxNumberTester)(valueAsNumber, scheme.max)) {
165
+ result = new interfaces_1.EjvError(constants_1.ErrorType.SMALLER_THAN, constants_1.ErrorMsg.SMALLER_THAN.replace(constants_1.ErrorMsgCursorA, '' + scheme.max), _options.path, data, value);
166
+ break;
167
+ }
168
+ }
169
+ else {
170
+ if (!(0, tester_1.maxNumberTester)(valueAsNumber, scheme.max)) {
171
+ result = new interfaces_1.EjvError(constants_1.ErrorType.SMALLER_THAN_OR_EQUAL, constants_1.ErrorMsg.SMALLER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.max), _options.path, data, value);
172
+ break;
173
+ }
174
+ }
175
+ }
176
+ else {
177
+ if (!(0, tester_1.maxNumberTester)(valueAsNumber, scheme.max)) {
178
+ result = new interfaces_1.EjvError(constants_1.ErrorType.SMALLER_THAN_OR_EQUAL, constants_1.ErrorMsg.SMALLER_THAN_OR_EQUAL.replace(constants_1.ErrorMsgCursorA, '' + scheme.max), _options.path, data, value);
179
+ break;
180
+ }
181
+ }
182
+ }
183
+ if ((0, tester_1.definedTester)(scheme.format)) {
184
+ var formats = void 0;
185
+ var allNumberFormat_1 = Object.values(constants_1.NumberFormat);
186
+ if (!(0, tester_1.arrayTester)(scheme.format)) {
187
+ var formatAsString = scheme.format;
188
+ if (!(0, tester_1.enumTester)(formatAsString, allNumberFormat_1)) {
189
+ throw new Error(constants_1.ErrorMsg.INVALID_NUMBER_FORMAT.replace(constants_1.ErrorMsgCursorA, formatAsString));
190
+ }
191
+ formats = [scheme.format];
192
+ }
193
+ else {
194
+ var formatAsArray = scheme.format;
195
+ var errorFormat = formatAsArray.find(function (format) {
196
+ return !(0, tester_1.enumTester)(format, allNumberFormat_1);
197
+ });
198
+ if (!!errorFormat) {
199
+ throw new Error(constants_1.ErrorMsg.INVALID_NUMBER_FORMAT.replace(constants_1.ErrorMsgCursorA, errorFormat));
200
+ }
201
+ formats = scheme.format;
202
+ }
203
+ if (!formats.some(function (format) {
204
+ var valid = false;
205
+ switch (format) {
206
+ case constants_1.NumberFormat.INTEGER:
207
+ valid = (0, tester_1.integerTester)(value);
208
+ break;
209
+ case constants_1.NumberFormat.INDEX:
210
+ valid = (0, tester_1.indexTester)(value);
211
+ break;
212
+ }
213
+ return valid;
214
+ })) {
215
+ if (!(0, tester_1.arrayTester)(scheme.format)) {
216
+ result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT, constants_1.ErrorMsg.FORMAT.replace(constants_1.ErrorMsgCursorA, scheme.format), _options.path, data, value);
217
+ }
218
+ else {
219
+ result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT_ONE_OF, constants_1.ErrorMsg.FORMAT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.format)), _options.path, data, value);
220
+ }
221
+ break;
222
+ }
223
+ }
224
+ break;
225
+ case constants_1.DataType.STRING:
226
+ var valueAsString = value;
227
+ if ((0, tester_1.definedTester)(scheme.enum)) {
228
+ if (!(0, tester_1.arrayTester)(scheme.enum)) {
229
+ throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_ARRAY);
230
+ }
231
+ var enumArr = scheme.enum;
232
+ if (!(0, tester_1.arrayTypeOfTester)(enumArr, constants_1.DataType.STRING)) {
233
+ throw new Error(constants_1.ErrorMsg.ENUM_SHOULD_BE_STRINGS);
234
+ }
235
+ if (!(0, tester_1.enumTester)(valueAsString, enumArr)) {
236
+ result = new interfaces_1.EjvError(constants_1.ErrorType.ONE_OF, constants_1.ErrorMsg.ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.enum)), _options.path, data, value);
237
+ break;
238
+ }
239
+ }
240
+ if ((0, tester_1.definedTester)(scheme.enumReverse)) {
241
+ if (!(0, tester_1.arrayTester)(scheme.enumReverse)) {
242
+ throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
243
+ }
244
+ var enumReverseArr = scheme.enumReverse;
245
+ if (!(0, tester_1.arrayTypeOfTester)(enumReverseArr, constants_1.DataType.STRING)) {
246
+ throw new Error(constants_1.ErrorMsg.ENUM_REVERSE_SHOULD_BE_STRINGS);
247
+ }
248
+ if ((0, tester_1.enumTester)(valueAsString, enumReverseArr)) {
249
+ result = new interfaces_1.EjvError(constants_1.ErrorType.NOT_ONE_OF, constants_1.ErrorMsg.NOT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(enumReverseArr)), _options.path, data, value);
250
+ break;
251
+ }
252
+ }
253
+ if ((0, tester_1.definedTester)(scheme.length)) {
254
+ var length_1 = scheme.length;
255
+ if (!((0, tester_1.numberTester)(length_1) && (0, tester_1.integerTester)(length_1))) {
256
+ throw new Error(constants_1.ErrorMsg.LENGTH_SHOULD_BE_INTEGER);
257
+ }
258
+ if (!(0, tester_1.lengthTester)(valueAsString, length_1)) {
259
+ result = new interfaces_1.EjvError(constants_1.ErrorType.LENGTH, constants_1.ErrorMsg.LENGTH.replace(constants_1.ErrorMsgCursorA, '' + length_1), _options.path, data, value);
260
+ break;
261
+ }
262
+ }
263
+ if ((0, tester_1.definedTester)(scheme.minLength)) {
264
+ var minLength = scheme.minLength;
265
+ if (!((0, tester_1.numberTester)(minLength) && (0, tester_1.integerTester)(minLength))) {
266
+ throw new Error(constants_1.ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
267
+ }
268
+ if (!(0, tester_1.minLengthTester)(valueAsString, minLength)) {
269
+ result = new interfaces_1.EjvError(constants_1.ErrorType.MIN_LENGTH, constants_1.ErrorMsg.MIN_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + minLength), _options.path, data, value);
270
+ break;
271
+ }
272
+ }
273
+ if ((0, tester_1.definedTester)(scheme.maxLength)) {
274
+ var maxLength = scheme.maxLength;
275
+ if (!((0, tester_1.numberTester)(maxLength) && (0, tester_1.integerTester)(maxLength))) {
276
+ throw new Error(constants_1.ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
277
+ }
278
+ if (!(0, tester_1.maxLengthTester)(valueAsString, maxLength)) {
279
+ result = new interfaces_1.EjvError(constants_1.ErrorType.MAX_LENGTH, constants_1.ErrorMsg.MAX_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + maxLength), _options.path, data, value);
280
+ break;
281
+ }
282
+ }
283
+ if ((0, tester_1.definedTester)(scheme.format)) {
284
+ var formats = void 0;
285
+ var allStringFormat_1 = Object.values(constants_1.StringFormat);
286
+ if (!(0, tester_1.arrayTester)(scheme.format)) {
287
+ var formatAsString = scheme.format;
288
+ if (!(0, tester_1.enumTester)(formatAsString, allStringFormat_1)) {
289
+ throw new Error(constants_1.ErrorMsg.INVALID_STRING_FORMAT.replace(constants_1.ErrorMsgCursorA, formatAsString));
290
+ }
291
+ formats = [scheme.format];
292
+ }
293
+ else {
294
+ var formatAsArray = scheme.format;
295
+ var errorFormat = formatAsArray.find(function (format) {
296
+ return !(0, tester_1.enumTester)(format, allStringFormat_1);
297
+ });
298
+ if (!!errorFormat) {
299
+ throw new Error(constants_1.ErrorMsg.INVALID_STRING_FORMAT.replace(constants_1.ErrorMsgCursorA, errorFormat));
300
+ }
301
+ formats = scheme.format;
302
+ }
303
+ if (!formats.some(function (format) {
304
+ var valid = false;
305
+ switch (format) {
306
+ case constants_1.StringFormat.EMAIL:
307
+ valid = (0, tester_1.emailTester)(value);
308
+ break;
309
+ case constants_1.StringFormat.DATE:
310
+ valid = (0, tester_1.dateFormatTester)(value);
311
+ break;
312
+ case constants_1.StringFormat.TIME:
313
+ valid = (0, tester_1.timeFormatTester)(value);
314
+ break;
315
+ case constants_1.StringFormat.DATE_TIME:
316
+ valid = (0, tester_1.dateTimeFormatTester)(value);
317
+ break;
318
+ }
319
+ return valid;
320
+ })) {
321
+ if (!(0, tester_1.arrayTester)(scheme.format)) {
322
+ result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT, constants_1.ErrorMsg.FORMAT.replace(constants_1.ErrorMsgCursorA, scheme.format), _options.path, data, value);
323
+ }
324
+ else {
325
+ result = new interfaces_1.EjvError(constants_1.ErrorType.FORMAT_ONE_OF, constants_1.ErrorMsg.FORMAT_ONE_OF.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.format)), _options.path, data, value);
326
+ }
327
+ break;
328
+ }
329
+ }
330
+ if ((0, tester_1.definedTester)(scheme.pattern)) {
331
+ // check parameter
332
+ if (scheme.pattern === null) {
333
+ throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
334
+ .replace(constants_1.ErrorMsgCursorA, 'null'));
335
+ }
336
+ var isValidPattern_1 = function (pattern) {
337
+ return ((0, tester_1.stringTester)(pattern) && (0, tester_1.minLengthTester)(pattern, 1))
338
+ || ((0, tester_1.regExpTester)(pattern) && pattern.toString() !== '/(?:)/' && pattern.toString() !== '/null/');
339
+ };
340
+ var patternToString_1 = function (pattern) {
341
+ var regExpStr;
342
+ if (pattern === null) {
343
+ regExpStr = '/null/';
344
+ }
345
+ else if ((0, tester_1.stringTester)(pattern)) {
346
+ if ((0, tester_1.minLengthTester)(pattern, 1)) {
347
+ regExpStr = new RegExp(pattern).toString();
348
+ }
349
+ else {
350
+ regExpStr = '//';
351
+ }
352
+ }
353
+ else {
354
+ regExpStr = pattern.toString();
355
+ }
356
+ // empty regular expression
357
+ if (regExpStr === '/(?:)/') {
358
+ regExpStr = '//';
359
+ }
360
+ return regExpStr;
361
+ };
362
+ var createArrayErrorMsg_1 = function (patternsAsArray) {
363
+ return '[' + patternsAsArray.map(function (onePattern) {
364
+ return patternToString_1(onePattern);
365
+ }).join(', ') + ']';
366
+ };
367
+ if ((0, tester_1.arrayTester)(scheme.pattern)) {
368
+ var patternsAsArray_1 = scheme.pattern;
369
+ if (!(0, tester_1.minLengthTester)(patternsAsArray_1, 1)) { // empty array
370
+ throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
371
+ .replace(constants_1.ErrorMsgCursorA, createArrayErrorMsg_1(patternsAsArray_1)));
372
+ }
373
+ var regExpPatterns = patternsAsArray_1.map(function (pattern) {
374
+ if (!isValidPattern_1(pattern)) {
375
+ throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
376
+ .replace(constants_1.ErrorMsgCursorA, createArrayErrorMsg_1(patternsAsArray_1)));
377
+ }
378
+ return new RegExp(pattern);
379
+ });
380
+ // check value
381
+ if (!regExpPatterns.some(function (regexp) {
382
+ return (0, tester_1.stringRegExpTester)(value, regexp);
383
+ })) {
384
+ result = new interfaces_1.EjvError(constants_1.ErrorType.PATTERN_ONE_OF, constants_1.ErrorMsg.PATTERN_ONE_OF
385
+ .replace(constants_1.ErrorMsgCursorA, createArrayErrorMsg_1(patternsAsArray_1)), _options.path, data, value);
386
+ break;
387
+ }
388
+ }
389
+ else {
390
+ var patternAsOne = scheme.pattern;
391
+ if (!isValidPattern_1(patternAsOne)) {
392
+ throw new Error(constants_1.ErrorMsg.INVALID_STRING_PATTERN
393
+ .replace(constants_1.ErrorMsgCursorA, patternToString_1(patternAsOne)));
394
+ }
395
+ // check value
396
+ var regExp = new RegExp(patternAsOne);
397
+ if (!(0, tester_1.stringRegExpTester)(valueAsString, regExp)) {
398
+ result = new interfaces_1.EjvError(constants_1.ErrorType.PATTERN, constants_1.ErrorMsg.PATTERN.replace(constants_1.ErrorMsgCursorA, patternToString_1(patternAsOne)), _options.path, data, value);
399
+ break;
400
+ }
401
+ }
402
+ }
403
+ break;
404
+ case constants_1.DataType.OBJECT:
405
+ var valueAsObject = value;
406
+ if ((0, tester_1.definedTester)(scheme.allowNoProperty)) {
407
+ if (!(0, tester_1.booleanTester)(scheme.allowNoProperty)) {
408
+ throw new Error(constants_1.ErrorMsg.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN);
409
+ }
410
+ if (scheme.allowNoProperty !== true && !(0, tester_1.hasPropertyTester)(valueAsObject)) {
411
+ result = new interfaces_1.EjvError(constants_1.ErrorType.NO_PROPERTY, constants_1.ErrorMsg.NO_PROPERTY, _options.path, data, value);
412
+ break;
413
+ }
414
+ }
415
+ if ((0, tester_1.definedTester)(scheme.properties)) {
416
+ if (!(0, tester_1.arrayTester)(scheme.properties)) {
417
+ throw new Error(constants_1.ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY);
418
+ }
419
+ var properties = scheme.properties;
420
+ if (!(0, tester_1.minLengthTester)(properties, 1)) {
421
+ throw new Error(constants_1.ErrorMsg.PROPERTIES_SHOULD_HAVE_ITEMS);
422
+ }
423
+ if (!(0, tester_1.arrayTypeOfTester)(properties, constants_1.DataType.OBJECT)) {
424
+ throw new Error(constants_1.ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT);
425
+ }
426
+ if (!(0, tester_1.objectTester)(value)) {
427
+ result = new interfaces_1.EjvError(constants_1.ErrorType.TYPE_MISMATCH, constants_1.ErrorMsg.TYPE_MISMATCH.replace(constants_1.ErrorMsgCursorA, 'object'), _options.path, data, value);
428
+ break;
429
+ }
430
+ var partialData = data[key];
431
+ var partialScheme = scheme.properties;
432
+ // call recursively
433
+ result = _ejv(partialData, partialScheme, _options);
434
+ if (!!result) {
435
+ // inject original data
436
+ result.data = data;
437
+ }
438
+ }
439
+ break;
440
+ case constants_1.DataType.DATE:
441
+ var valueAsDate = value;
442
+ if ((0, tester_1.definedTester)(scheme.min)) {
443
+ if (!(((0, tester_1.stringTester)(scheme.min) && ((0, tester_1.dateFormatTester)(scheme.min) || (0, tester_1.dateTimeFormatTester)(scheme.min)))
444
+ || (0, tester_1.dateTester)(scheme.min))) {
445
+ throw new Error(constants_1.ErrorMsg.MIN_DATE_SHOULD_BE_DATE_OR_STRING);
446
+ }
447
+ if ((0, tester_1.definedTester)(scheme.exclusiveMin) && !(0, tester_1.booleanTester)(scheme.exclusiveMin)) {
448
+ throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN);
449
+ }
450
+ var minDate = new Date(scheme.min);
451
+ // adjust timezone
452
+ if ((0, tester_1.stringTester)(scheme.min)) {
453
+ // by minutes
454
+ var timezoneOffset = minDate.getTimezoneOffset();
455
+ minDate = new Date(+minDate + (timezoneOffset * 60 * 1000));
456
+ }
457
+ if (scheme.exclusiveMin !== true) {
458
+ if (!(0, tester_1.minDateTester)(valueAsDate, minDate)) {
459
+ result = new interfaces_1.EjvError(constants_1.ErrorType.AFTER_OR_SAME_DATE, constants_1.ErrorMsg.AFTER_OR_SAME_DATE.replace(constants_1.ErrorMsgCursorA, minDate.toISOString()), _options.path, data, value);
460
+ break;
461
+ }
462
+ }
463
+ else {
464
+ if (!(0, tester_1.exclusiveMinDateTester)(valueAsDate, minDate)) {
465
+ result = new interfaces_1.EjvError(constants_1.ErrorType.AFTER_DATE, constants_1.ErrorMsg.AFTER_DATE.replace(constants_1.ErrorMsgCursorA, minDate.toISOString()), _options.path, data, value);
466
+ break;
467
+ }
468
+ }
469
+ }
470
+ if ((0, tester_1.definedTester)(scheme.max)) {
471
+ if (!(((0, tester_1.stringTester)(scheme.max) && ((0, tester_1.dateFormatTester)(scheme.max) || (0, tester_1.dateTimeFormatTester)(scheme.max)))
472
+ || (0, tester_1.dateTester)(scheme.max))) {
473
+ throw new Error(constants_1.ErrorMsg.MAX_DATE_SHOULD_BE_DATE_OR_STRING);
474
+ }
475
+ if ((0, tester_1.definedTester)(scheme.exclusiveMax) && !(0, tester_1.booleanTester)(scheme.exclusiveMax)) {
476
+ throw new Error(constants_1.ErrorMsg.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN);
477
+ }
478
+ var maxDate = new Date(scheme.max);
479
+ // adjust timezone
480
+ if ((0, tester_1.stringTester)(scheme.max)) {
481
+ // by minutes
482
+ var timezoneOffset = maxDate.getTimezoneOffset();
483
+ maxDate = new Date(+maxDate + (timezoneOffset * 60 * 1000));
484
+ }
485
+ if (scheme.exclusiveMax !== true) {
486
+ if (!(0, tester_1.maxDateTester)(valueAsDate, maxDate)) {
487
+ result = new interfaces_1.EjvError(constants_1.ErrorType.BEFORE_OR_SAME_DATE, constants_1.ErrorMsg.BEFORE_OR_SAME_DATE.replace(constants_1.ErrorMsgCursorA, maxDate.toISOString()), _options.path, data, value);
488
+ break;
489
+ }
490
+ }
491
+ else {
492
+ if (!(0, tester_1.exclusiveMaxDateTester)(valueAsDate, maxDate)) {
493
+ result = new interfaces_1.EjvError(constants_1.ErrorType.BEFORE_DATE, constants_1.ErrorMsg.BEFORE_DATE.replace(constants_1.ErrorMsgCursorA, maxDate.toISOString()), _options.path, data, value);
494
+ break;
495
+ }
496
+ }
497
+ }
498
+ break;
499
+ case constants_1.DataType.ARRAY:
500
+ var valueAsArray_1 = value;
501
+ if ((0, tester_1.definedTester)(scheme.length)) {
502
+ var length_2 = scheme.length;
503
+ if (!((0, tester_1.numberTester)(length_2) && (0, tester_1.integerTester)(length_2))) {
504
+ throw new Error(constants_1.ErrorMsg.LENGTH_SHOULD_BE_INTEGER);
505
+ }
506
+ if (!(0, tester_1.lengthTester)(valueAsArray_1, length_2)) {
507
+ result = new interfaces_1.EjvError(constants_1.ErrorType.LENGTH, constants_1.ErrorMsg.LENGTH.replace(constants_1.ErrorMsgCursorA, '' + length_2), _options.path, data, value);
508
+ break;
509
+ }
510
+ }
511
+ if ((0, tester_1.definedTester)(scheme.minLength)) {
512
+ var minLength = scheme.minLength;
513
+ if (!((0, tester_1.numberTester)(scheme.minLength) && (0, tester_1.integerTester)(minLength))) {
514
+ throw new Error(constants_1.ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
515
+ }
516
+ if (!(0, tester_1.minLengthTester)(valueAsArray_1, minLength)) {
517
+ result = new interfaces_1.EjvError(constants_1.ErrorType.MIN_LENGTH, constants_1.ErrorMsg.MIN_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + minLength), _options.path, data, value);
518
+ break;
519
+ }
520
+ }
521
+ if ((0, tester_1.definedTester)(scheme.maxLength)) {
522
+ var maxLength = scheme.maxLength;
523
+ if (!((0, tester_1.numberTester)(scheme.maxLength) && (0, tester_1.integerTester)(maxLength))) {
524
+ throw new Error(constants_1.ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
525
+ }
526
+ if (!(0, tester_1.maxLengthTester)(valueAsArray_1, maxLength)) {
527
+ result = new interfaces_1.EjvError(constants_1.ErrorType.MAX_LENGTH, constants_1.ErrorMsg.MAX_LENGTH.replace(constants_1.ErrorMsgCursorA, '' + maxLength), _options.path, data, value);
528
+ break;
529
+ }
530
+ }
531
+ if ((0, tester_1.definedTester)(scheme.unique)) {
532
+ if (!(0, tester_1.booleanTester)(scheme.unique)) {
533
+ throw new Error(constants_1.ErrorMsg.UNIQUE_SHOULD_BE_BOOLEAN);
534
+ }
535
+ if (scheme.unique === true && !(0, tester_1.uniqueItemsTester)(valueAsArray_1)) {
536
+ result = new interfaces_1.EjvError(constants_1.ErrorType.UNIQUE_ITEMS, constants_1.ErrorMsg.UNIQUE_ITEMS, _options.path, data, value);
537
+ break;
538
+ }
539
+ }
540
+ if ((0, tester_1.definedTester)(scheme.items)) {
541
+ // convert array to object
542
+ if (valueAsArray_1.length > 0) {
543
+ var now_1 = new Date;
544
+ var tempKeyArr = valueAsArray_1.map(function (value, i) {
545
+ return '' + (+now_1 + i);
546
+ });
547
+ if ((0, tester_1.stringTester)(scheme.items) // by DataType
548
+ || ((0, tester_1.arrayTester)(scheme.items) && (0, tester_1.arrayTypeOfTester)(scheme.items, constants_1.DataType.STRING)) // by DataType[]
549
+ ) {
550
+ var itemTypes_1 = ((0, tester_1.arrayTester)(scheme.items) ? scheme.items : [scheme.items]);
551
+ var partialData_1 = {};
552
+ var partialSchemes_1 = [];
553
+ tempKeyArr.forEach(function (tempKey, i) {
554
+ partialData_1[tempKey] = valueAsArray_1[i];
555
+ partialSchemes_1.push({
556
+ key: tempKey,
557
+ type: itemTypes_1
558
+ });
559
+ });
560
+ // call recursively
561
+ var partialResult = _ejv(partialData_1, partialSchemes_1, _options);
562
+ // convert new EjvError
563
+ if (!!partialResult) {
564
+ var errorMsg = void 0;
565
+ if ((0, tester_1.arrayTester)(scheme.items)) {
566
+ errorMsg = constants_1.ErrorMsg.ITEMS_TYPE.replace(constants_1.ErrorMsgCursorA, JSON.stringify(itemTypes_1));
567
+ }
568
+ else {
569
+ errorMsg = constants_1.ErrorMsg.ITEMS_TYPE.replace(constants_1.ErrorMsgCursorA, scheme.items);
570
+ }
571
+ var partialKeys = partialResult.path.split('/');
572
+ var partialKey_1 = partialKeys[partialKeys.length - 1];
573
+ var partialScheme = partialSchemes_1.find(function (scheme) {
574
+ return scheme.key === partialKey_1;
575
+ });
576
+ var partialKeyIndex = partialSchemes_1.indexOf(partialScheme);
577
+ result = new interfaces_1.EjvError(constants_1.ErrorType.ITEMS_TYPE, errorMsg, __spreadArray(__spreadArray([], _options.path, true), ['' + partialKeyIndex], false), data, partialData_1[partialKey_1]);
578
+ }
579
+ break;
580
+ }
581
+ else if (((0, tester_1.objectTester)(scheme.items) && scheme.items !== null) // by Scheme
582
+ || ((0, tester_1.arrayTester)(scheme.items) && (0, tester_1.arrayTypeOfTester)(scheme.items, constants_1.DataType.OBJECT)) // by Scheme[]
583
+ ) {
584
+ var itemsAsSchemes = ((0, tester_1.arrayTester)(scheme.items) ? scheme.items : [scheme.items]);
585
+ var partialError = null;
586
+ // use for() instead of forEach() to break
587
+ var valueLength = valueAsArray_1.length;
588
+ var _loop_2 = function (arrIndex) {
589
+ var oneValue = valueAsArray_1[arrIndex];
590
+ var partialData = {};
591
+ var partialSchemes = [];
592
+ var tempKeyForThisValue = tempKeyArr[arrIndex];
593
+ partialData[tempKeyForThisValue] = oneValue;
594
+ partialSchemes.push.apply(partialSchemes, itemsAsSchemes.map(function (oneScheme) {
595
+ var newScheme = (0, util_1.clone)(oneScheme); // divide instance
596
+ newScheme.key = tempKeyForThisValue;
597
+ return newScheme;
598
+ }));
599
+ var partialResults = partialSchemes.map(function (partialScheme) {
600
+ // call recursively
601
+ var partialResult = _ejv(partialData, [partialScheme], _options);
602
+ if (!!partialResult) {
603
+ partialResult.path = partialResult.path.replace(tempKeyForThisValue, '' + arrIndex);
604
+ }
605
+ return partialResult;
606
+ });
607
+ if (!partialResults.some(function (oneResult) { return oneResult === null; })) {
608
+ partialError = partialResults.find(function (oneResult) {
609
+ return !!oneResult;
610
+ });
611
+ return "break";
612
+ }
613
+ };
614
+ for (var arrIndex = 0; arrIndex < valueLength; arrIndex++) {
615
+ var state_2 = _loop_2(arrIndex);
616
+ if (state_2 === "break")
617
+ break;
618
+ }
619
+ if (!!partialError) {
620
+ var errorType_1 = void 0;
621
+ var errorMsg = void 0;
622
+ if (!!itemsAsSchemes && itemsAsSchemes.length > 1) {
623
+ errorType_1 = constants_1.ErrorType.ITEMS_SCHEMES;
624
+ errorMsg = constants_1.ErrorMsg.ITEMS_SCHEMES.replace(constants_1.ErrorMsgCursorA, JSON.stringify(itemsAsSchemes));
625
+ }
626
+ else {
627
+ errorType_1 = partialError.type;
628
+ errorMsg = partialError.message;
629
+ if (errorType_1 === constants_1.ErrorType.REQUIRED) {
630
+ // REQUIRED in array is TYPE_MISMATCH except with nullable === true
631
+ errorType_1 = constants_1.ErrorType.TYPE_MISMATCH;
632
+ errorMsg = constants_1.ErrorMsg.TYPE_MISMATCH.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.items));
633
+ }
634
+ }
635
+ result = new interfaces_1.EjvError(errorType_1, errorMsg, partialError.path.split('/'), data, partialError.errorData);
636
+ break;
637
+ }
638
+ }
639
+ else {
640
+ throw new Error(constants_1.ErrorMsg.INVALID_ITEMS_SCHEME.replace(constants_1.ErrorMsgCursorA, JSON.stringify(scheme.items)));
641
+ }
642
+ }
643
+ }
644
+ break;
645
+ }
646
+ if (!!result) {
647
+ return "break";
648
+ }
649
+ };
650
+ for (var i = 0; i < schemeLength; i++) {
651
+ var state_1 = _loop_1(i);
652
+ if (state_1 === "break")
653
+ break;
654
+ }
655
+ if ((0, tester_1.definedTester)(result) && (0, tester_1.definedTester)(options.customErrorMsg)) {
656
+ var resultAsNotNull = result;
657
+ var customErrorMsgObj = options.customErrorMsg;
658
+ // override error message
659
+ var customMsg = customErrorMsgObj[resultAsNotNull.type];
660
+ if ((0, tester_1.definedTester)(customMsg)) {
661
+ resultAsNotNull.message = customMsg;
662
+ }
663
+ }
664
+ return result;
665
+ };
666
+ var ejv = function (data, schemes, options) {
667
+ // check data itself
668
+ if (!(0, tester_1.definedTester)(data) || !(0, tester_1.objectTester)(data) || data === null) {
669
+ return new interfaces_1.EjvError(constants_1.ErrorType.REQUIRED, constants_1.ErrorMsg.NO_DATA, ['/'], data, data);
670
+ }
671
+ // check schemes itself
672
+ if (!(0, tester_1.definedTester)(schemes) || schemes === null) {
673
+ throw new Error(constants_1.ErrorMsg.NO_SCHEME);
674
+ }
675
+ if (!(0, tester_1.arrayTester)(schemes)) {
676
+ throw new Error(constants_1.ErrorMsg.NO_ARRAY_SCHEME);
677
+ }
678
+ if (!(0, tester_1.arrayTypeOfTester)(schemes, constants_1.DataType.OBJECT)) {
679
+ throw new Error(constants_1.ErrorMsg.NO_OBJECT_ARRAY_SCHEME);
680
+ }
681
+ if (!(0, tester_1.minLengthTester)(schemes, 1)) {
682
+ throw new Error(constants_1.ErrorMsg.EMPTY_SCHEME);
683
+ }
684
+ return _ejv(data, schemes, options);
685
+ };
686
+ exports.ejv = ejv;
684
687
  //# sourceMappingURL=ejv.js.map