ejv 1.1.11 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +88 -0
- package/.mocharc.json +8 -0
- package/CHANGELOG.md +109 -68
- package/LICENSE +21 -21
- package/README-KR.md +596 -592
- package/README.md +599 -595
- package/build/{constants.js → cjs/constants.js} +45 -46
- package/build/cjs/constants.js.map +1 -0
- package/build/cjs/ejv.js +1263 -0
- package/build/cjs/ejv.js.map +1 -0
- package/build/{public_api.js → cjs/index.js} +4 -4
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/interfaces.js +29 -0
- package/build/cjs/interfaces.js.map +1 -0
- package/build/cjs/package.json +1 -0
- package/build/{tester.js → cjs/tester.js} +88 -83
- package/build/cjs/tester.js.map +1 -0
- package/build/cjs/util.js +103 -0
- package/build/cjs/util.js.map +1 -0
- package/build/constants.d.ts +37 -40
- package/build/esm/constants.js +115 -0
- package/build/esm/constants.js.map +1 -0
- package/build/esm/ejv.js +1261 -0
- package/build/esm/ejv.js.map +1 -0
- package/build/esm/index.js +4 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/interfaces.js +33 -0
- package/build/esm/interfaces.js.map +1 -0
- package/build/esm/package.json +1 -0
- package/build/esm/tester.js +240 -0
- package/build/esm/tester.js.map +1 -0
- package/build/esm/util.js +96 -0
- package/build/esm/util.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/interfaces.d.ts +51 -13
- package/build/scripts/add-js-extensions.js +46 -0
- package/build/scripts/add-js-extensions.js.map +1 -0
- package/build/tester.d.ts +16 -17
- package/build/util.d.ts +7 -1
- package/package.json +50 -39
- package/scripts/add-js-extensions.ts +59 -0
- package/spec/ArrayScheme.ts +1021 -0
- package/spec/CommonScheme.ts +251 -0
- package/spec/DateScheme.ts +472 -0
- package/spec/NumberScheme.ts +1032 -0
- package/spec/ObjectScheme.ts +499 -0
- package/spec/RegExpScheme.ts +112 -0
- package/spec/StringScheme.ts +1239 -0
- package/spec/common-test-util.ts +63 -0
- package/spec/ejv.spec.ts +209 -4634
- package/spec/testers.spec.ts +833 -832
- package/src/constants.ts +155 -156
- package/src/ejv.ts +1648 -1071
- package/src/index.ts +14 -0
- package/src/interfaces.ts +145 -63
- package/src/tester.ts +308 -302
- package/src/util.ts +124 -59
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +7 -0
- package/tsconfig.json +22 -19
- package/tsconfig.scripts.json +14 -0
- package/tsconfig.types.json +9 -0
- package/build/constants.js.map +0 -1
- package/build/ejv.js +0 -687
- package/build/ejv.js.map +0 -1
- package/build/interfaces.js +0 -15
- package/build/interfaces.js.map +0 -1
- package/build/public_api.d.ts +0 -3
- package/build/public_api.js.map +0 -1
- package/build/tester.js.map +0 -1
- package/build/util.js +0 -68
- package/build/util.js.map +0 -1
- package/spec/common-test-runner.ts +0 -17
- package/src/public_api.ts +0 -3
- package/tsconfig.spec.json +0 -19
package/build/ejv.js
DELETED
|
@@ -1,687 +0,0 @@
|
|
|
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;
|
|
687
|
-
//# sourceMappingURL=ejv.js.map
|