ejv 1.1.10 → 2.0.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 (75) hide show
  1. package/.eslintrc.json +88 -0
  2. package/.mocharc.json +8 -0
  3. package/CHANGELOG.md +70 -29
  4. package/README-KR.md +6 -2
  5. package/README.md +6 -2
  6. package/build/{constants.js → cjs/constants.js} +117 -118
  7. package/build/cjs/constants.js.map +1 -0
  8. package/build/cjs/ejv.js +1263 -0
  9. package/build/cjs/ejv.js.map +1 -0
  10. package/build/{public_api.js → cjs/index.js} +14 -14
  11. package/build/cjs/index.js.map +1 -0
  12. package/build/cjs/interfaces.js +29 -0
  13. package/build/cjs/interfaces.js.map +1 -0
  14. package/build/cjs/package.json +1 -0
  15. package/build/{tester.js → cjs/tester.js} +273 -268
  16. package/build/cjs/tester.js.map +1 -0
  17. package/build/cjs/util.js +103 -0
  18. package/build/cjs/util.js.map +1 -0
  19. package/build/constants.d.ts +101 -104
  20. package/build/ejv.d.ts +2 -2
  21. package/build/esm/constants.js +115 -0
  22. package/build/esm/constants.js.map +1 -0
  23. package/build/esm/ejv.js +1261 -0
  24. package/build/esm/ejv.js.map +1 -0
  25. package/build/esm/index.js +4 -0
  26. package/build/esm/index.js.map +1 -0
  27. package/build/esm/interfaces.js +33 -0
  28. package/build/esm/interfaces.js.map +1 -0
  29. package/build/esm/package.json +1 -0
  30. package/build/esm/tester.js +240 -0
  31. package/build/esm/tester.js.map +1 -0
  32. package/build/esm/util.js +96 -0
  33. package/build/esm/util.js.map +1 -0
  34. package/build/index.d.ts +3 -0
  35. package/build/interfaces.d.ts +78 -38
  36. package/build/scripts/add-js-extensions.js +46 -0
  37. package/build/scripts/add-js-extensions.js.map +1 -0
  38. package/build/tester.d.ts +33 -34
  39. package/build/util.d.ts +7 -1
  40. package/package.json +48 -37
  41. package/scripts/add-js-extensions.ts +59 -0
  42. package/spec/ArrayScheme.ts +1021 -0
  43. package/spec/CommonScheme.ts +251 -0
  44. package/spec/DateScheme.ts +472 -0
  45. package/spec/NumberScheme.ts +1032 -0
  46. package/spec/ObjectScheme.ts +499 -0
  47. package/spec/RegExpScheme.ts +112 -0
  48. package/spec/StringScheme.ts +1239 -0
  49. package/spec/common-test-util.ts +63 -0
  50. package/spec/ejv.spec.ts +133 -4558
  51. package/spec/testers.spec.ts +17 -16
  52. package/src/constants.ts +41 -42
  53. package/src/ejv.ts +1141 -564
  54. package/src/index.ts +14 -0
  55. package/src/interfaces.ts +127 -41
  56. package/src/tester.ts +75 -69
  57. package/src/util.ts +106 -41
  58. package/tsconfig.cjs.json +8 -0
  59. package/tsconfig.esm.json +7 -0
  60. package/tsconfig.json +21 -18
  61. package/tsconfig.scripts.json +14 -0
  62. package/tsconfig.types.json +9 -0
  63. package/build/constants.js.map +0 -1
  64. package/build/ejv.js +0 -685
  65. package/build/ejv.js.map +0 -1
  66. package/build/interfaces.js +0 -15
  67. package/build/interfaces.js.map +0 -1
  68. package/build/public_api.d.ts +0 -3
  69. package/build/public_api.js.map +0 -1
  70. package/build/tester.js.map +0 -1
  71. package/build/util.js +0 -66
  72. package/build/util.js.map +0 -1
  73. package/spec/common-test-runner.ts +0 -17
  74. package/src/public_api.ts +0 -3
  75. package/tsconfig.spec.json +0 -19
package/build/ejv.js DELETED
@@ -1,685 +0,0 @@
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
- var 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
- };
684
- exports.ejv = ejv;
685
- //# sourceMappingURL=ejv.js.map