ejv 2.0.5 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +1 -1
- package/CHANGELOG.md +26 -0
- package/README-KR.md +166 -165
- package/README.md +182 -178
- package/build/cjs/constants.js +112 -107
- package/build/cjs/constants.js.map +1 -1
- package/build/cjs/ejv.js +245 -177
- package/build/cjs/ejv.js.map +1 -1
- package/build/cjs/index.js +6 -6
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/interfaces.js.map +1 -1
- package/build/cjs/tester.js +12 -8
- package/build/cjs/tester.js.map +1 -1
- package/build/cjs/util.js.map +1 -1
- package/build/constants.d.ts +10 -5
- package/build/esm/constants.js +111 -106
- package/build/esm/constants.js.map +1 -1
- package/build/esm/ejv.js +247 -179
- package/build/esm/ejv.js.map +1 -1
- package/build/esm/index.js +1 -1
- package/build/esm/index.js.map +1 -1
- package/build/esm/interfaces.js.map +1 -1
- package/build/esm/tester.js +11 -8
- package/build/esm/tester.js.map +1 -1
- package/build/esm/util.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/interfaces.d.ts +10 -9
- package/build/tester.d.ts +4 -3
- package/build/util.d.ts +2 -2
- package/eslint.config.mjs +59 -0
- package/package.json +17 -13
- package/spec/ArrayScheme.ts +46 -46
- package/spec/CommonScheme.ts +15 -15
- package/spec/DateScheme.ts +22 -22
- package/spec/NumberScheme.ts +229 -121
- package/spec/ObjectScheme.ts +22 -22
- package/spec/RegExpScheme.ts +5 -5
- package/spec/StringScheme.ts +223 -126
- package/spec/common-test-util.ts +2 -2
- package/spec/ejv.spec.ts +20 -20
- package/spec/testers.spec.ts +5 -5
- package/src/constants.ts +12 -5
- package/src/ejv.ts +291 -202
- package/src/index.ts +1 -1
- package/src/interfaces.ts +11 -12
- package/src/tester.ts +14 -10
- package/src/util.ts +2 -2
- package/tsconfig.json +2 -1
- package/.eslintrc.json +0 -88
package/build/esm/ejv.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EjvError } from './interfaces.js';
|
|
2
|
-
import {
|
|
3
|
-
import { arrayTester, arrayTypeOfTester, booleanTester, dateFormatTester, dateTester, dateTimeFormatTester, definedTester, emailTester, enumTester, exclusiveMaxDateTester, exclusiveMaxNumberTester, exclusiveMinDateTester, exclusiveMinNumberTester, hasPropertyTester, indexTester, integerTester, lengthTester, maxDateTester, maxLengthTester, maxNumberTester, minDateTester, minLengthTester, minNumberTester, numberTester, objectTester, regExpTester, stringRegExpTester, stringTester, timeFormatTester, typeTester, uniqueItemsTester } from './tester.js';
|
|
2
|
+
import { DATA_TYPE, ERROR_MESSAGE, ERROR_TYPE, NUMBER_FORMAT, STRING_FORMAT } from './constants.js';
|
|
3
|
+
import { arrayTester, arrayTypeOfTester, booleanTester, dateFormatTester, dateTester, dateTimeFormatTester, definedTester, emailTester, enumTester, exclusiveMaxDateTester, exclusiveMaxNumberTester, exclusiveMinDateTester, exclusiveMinNumberTester, hasPropertyTester, indexTester, integerTester, lengthTester, maxDateTester, maxLengthTester, maxNumberTester, minDateTester, minLengthTester, minNumberTester, notEnumTester, numberTester, objectTester, regExpTester, stringRegExpTester, stringTester, timeFormatTester, typeTester, uniqueItemsTester } from './tester.js';
|
|
4
4
|
import { clone, createErrorMsg, sift } from './util.js';
|
|
5
5
|
function _getEffectiveTypes(scheme) {
|
|
6
6
|
let result;
|
|
@@ -18,8 +18,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
18
18
|
// check schemes
|
|
19
19
|
if (!definedTester(schemes) || schemes === null) {
|
|
20
20
|
return new EjvError({
|
|
21
|
-
type:
|
|
22
|
-
message:
|
|
21
|
+
type: ERROR_TYPE.NO_SCHEME,
|
|
22
|
+
message: ERROR_MESSAGE.NO_SCHEME,
|
|
23
23
|
data: data,
|
|
24
24
|
errorScheme: schemes,
|
|
25
25
|
isSchemeError: true
|
|
@@ -27,17 +27,17 @@ const _ejv = (data, schemes, options) => {
|
|
|
27
27
|
}
|
|
28
28
|
if (!arrayTester(schemes)) {
|
|
29
29
|
return new EjvError({
|
|
30
|
-
type:
|
|
31
|
-
message:
|
|
30
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
31
|
+
message: ERROR_MESSAGE.NO_ARRAY_SCHEME,
|
|
32
32
|
data: data,
|
|
33
33
|
errorScheme: schemes,
|
|
34
34
|
isSchemeError: true
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
-
if (!arrayTypeOfTester(schemes,
|
|
37
|
+
if (!arrayTypeOfTester(schemes, DATA_TYPE.OBJECT)) {
|
|
38
38
|
return new EjvError({
|
|
39
|
-
type:
|
|
40
|
-
message:
|
|
39
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
40
|
+
message: ERROR_MESSAGE.NO_OBJECT_ARRAY_SCHEME,
|
|
41
41
|
data: data,
|
|
42
42
|
errorScheme: schemes,
|
|
43
43
|
isSchemeError: true
|
|
@@ -45,8 +45,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
45
45
|
}
|
|
46
46
|
if (!minLengthTester(schemes, 1)) {
|
|
47
47
|
return new EjvError({
|
|
48
|
-
type:
|
|
49
|
-
message:
|
|
48
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
49
|
+
message: ERROR_MESSAGE.EMPTY_SCHEME,
|
|
50
50
|
data: data,
|
|
51
51
|
errorScheme: schemes,
|
|
52
52
|
isSchemeError: true
|
|
@@ -66,14 +66,14 @@ const _ejv = (data, schemes, options) => {
|
|
|
66
66
|
const types = _getEffectiveTypes(scheme);
|
|
67
67
|
if (!definedTester(types)) {
|
|
68
68
|
return new EjvError({
|
|
69
|
-
type:
|
|
70
|
-
message:
|
|
69
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
70
|
+
message: ERROR_MESSAGE.SCHEMES_SHOULD_HAVE_TYPE,
|
|
71
71
|
data: data,
|
|
72
72
|
errorScheme: scheme,
|
|
73
73
|
isSchemeError: true
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
|
-
const allDataType = Object.values(
|
|
76
|
+
const allDataType = Object.values(DATA_TYPE);
|
|
77
77
|
const typeError = types.find((type) => {
|
|
78
78
|
return !definedTester(type)
|
|
79
79
|
|| !stringTester(type)
|
|
@@ -81,8 +81,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
81
81
|
});
|
|
82
82
|
if (typeError) {
|
|
83
83
|
return new EjvError({
|
|
84
|
-
type:
|
|
85
|
-
message: createErrorMsg(
|
|
84
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
85
|
+
message: createErrorMsg(ERROR_MESSAGE.SCHEMES_HAS_INVALID_TYPE, {
|
|
86
86
|
placeholders: [typeError]
|
|
87
87
|
}),
|
|
88
88
|
data: data,
|
|
@@ -96,8 +96,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
96
96
|
});
|
|
97
97
|
const notUniqueItemsSifted = sift(notUniqueItems);
|
|
98
98
|
return new EjvError({
|
|
99
|
-
type:
|
|
100
|
-
message: createErrorMsg(
|
|
99
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
100
|
+
message: createErrorMsg(ERROR_MESSAGE.SCHEMES_HAS_DUPLICATED_TYPE, {
|
|
101
101
|
placeholders: [notUniqueItemsSifted.join(', ')]
|
|
102
102
|
}),
|
|
103
103
|
data: data,
|
|
@@ -108,8 +108,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
108
108
|
if (!definedTester(value)) {
|
|
109
109
|
if (scheme.optional !== true) {
|
|
110
110
|
result = new EjvError({
|
|
111
|
-
type:
|
|
112
|
-
message: createErrorMsg(
|
|
111
|
+
type: ERROR_TYPE.REQUIRED,
|
|
112
|
+
message: createErrorMsg(ERROR_MESSAGE.REQUIRED),
|
|
113
113
|
data,
|
|
114
114
|
path: _options.path,
|
|
115
115
|
errorScheme: scheme,
|
|
@@ -124,8 +124,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
124
124
|
if (value === null) {
|
|
125
125
|
if (scheme.nullable !== true) {
|
|
126
126
|
result = new EjvError({
|
|
127
|
-
type:
|
|
128
|
-
message: createErrorMsg(
|
|
127
|
+
type: ERROR_TYPE.REQUIRED,
|
|
128
|
+
message: createErrorMsg(ERROR_MESSAGE.REQUIRED),
|
|
129
129
|
data,
|
|
130
130
|
path: _options.path,
|
|
131
131
|
errorScheme: scheme,
|
|
@@ -145,8 +145,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
145
145
|
if (!arrayTester(scheme.type)) {
|
|
146
146
|
if (scheme.type !== typeResolved) {
|
|
147
147
|
result = new EjvError({
|
|
148
|
-
type:
|
|
149
|
-
message: createErrorMsg(
|
|
148
|
+
type: ERROR_TYPE.TYPE_MISMATCH,
|
|
149
|
+
message: createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
|
|
150
150
|
placeholders: [scheme.type]
|
|
151
151
|
}),
|
|
152
152
|
data,
|
|
@@ -159,8 +159,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
159
159
|
else {
|
|
160
160
|
if (!scheme.type.includes(typeResolved)) {
|
|
161
161
|
result = new EjvError({
|
|
162
|
-
type:
|
|
163
|
-
message: createErrorMsg(
|
|
162
|
+
type: ERROR_TYPE.TYPE_MISMATCH_ONE_OF,
|
|
163
|
+
message: createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
|
|
164
164
|
placeholders: [JSON.stringify(scheme.type)]
|
|
165
165
|
}),
|
|
166
166
|
data,
|
|
@@ -178,8 +178,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
178
178
|
const typesForMsg = scheme.type || scheme.parent?.type;
|
|
179
179
|
if (!arrayTester(typesForMsg)) {
|
|
180
180
|
result = new EjvError({
|
|
181
|
-
type:
|
|
182
|
-
message: createErrorMsg(
|
|
181
|
+
type: ERROR_TYPE.TYPE_MISMATCH,
|
|
182
|
+
message: createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
|
|
183
183
|
placeholders: [typesForMsg]
|
|
184
184
|
}),
|
|
185
185
|
data,
|
|
@@ -190,8 +190,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
190
190
|
}
|
|
191
191
|
else {
|
|
192
192
|
result = new EjvError({
|
|
193
|
-
type:
|
|
194
|
-
message: createErrorMsg(
|
|
193
|
+
type: ERROR_TYPE.TYPE_MISMATCH_ONE_OF,
|
|
194
|
+
message: createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
|
|
195
195
|
placeholders: [JSON.stringify(typesForMsg)]
|
|
196
196
|
}),
|
|
197
197
|
data,
|
|
@@ -204,24 +204,24 @@ const _ejv = (data, schemes, options) => {
|
|
|
204
204
|
}
|
|
205
205
|
// additional check for type resolved
|
|
206
206
|
switch (typeResolved) {
|
|
207
|
-
case
|
|
207
|
+
case DATA_TYPE.NUMBER: {
|
|
208
208
|
const valueAsNumber = value;
|
|
209
209
|
const numberScheme = scheme;
|
|
210
210
|
if (definedTester(numberScheme.enum)) {
|
|
211
211
|
if (!arrayTester(numberScheme.enum)) {
|
|
212
212
|
return new EjvError({
|
|
213
|
-
type:
|
|
214
|
-
message: createErrorMsg(
|
|
213
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
214
|
+
message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY),
|
|
215
215
|
data: data,
|
|
216
216
|
errorScheme: numberScheme,
|
|
217
217
|
isSchemeError: true
|
|
218
218
|
});
|
|
219
219
|
}
|
|
220
220
|
const enumArr = numberScheme.enum;
|
|
221
|
-
if (!arrayTypeOfTester(enumArr,
|
|
221
|
+
if (!arrayTypeOfTester(enumArr, DATA_TYPE.NUMBER)) {
|
|
222
222
|
return new EjvError({
|
|
223
|
-
type:
|
|
224
|
-
message: createErrorMsg(
|
|
223
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
224
|
+
message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_NUMBERS),
|
|
225
225
|
data: data,
|
|
226
226
|
errorScheme: numberScheme,
|
|
227
227
|
isSchemeError: true
|
|
@@ -229,8 +229,42 @@ const _ejv = (data, schemes, options) => {
|
|
|
229
229
|
}
|
|
230
230
|
if (!enumTester(valueAsNumber, enumArr)) {
|
|
231
231
|
result = new EjvError({
|
|
232
|
-
type:
|
|
233
|
-
message: createErrorMsg(
|
|
232
|
+
type: ERROR_TYPE.ONE_VALUE_OF,
|
|
233
|
+
message: createErrorMsg(ERROR_MESSAGE.ONE_VALUE_OF, {
|
|
234
|
+
placeholders: [JSON.stringify(enumArr)]
|
|
235
|
+
}),
|
|
236
|
+
data,
|
|
237
|
+
path: _options.path,
|
|
238
|
+
errorScheme: numberScheme,
|
|
239
|
+
errorData: value
|
|
240
|
+
});
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (definedTester(numberScheme.notEnum)) {
|
|
245
|
+
if (!arrayTester(numberScheme.notEnum)) {
|
|
246
|
+
return new EjvError({
|
|
247
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
248
|
+
message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY),
|
|
249
|
+
data: data,
|
|
250
|
+
errorScheme: numberScheme,
|
|
251
|
+
isSchemeError: true
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
const enumArr = numberScheme.notEnum;
|
|
255
|
+
if (!arrayTypeOfTester(enumArr, DATA_TYPE.NUMBER)) {
|
|
256
|
+
return new EjvError({
|
|
257
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
258
|
+
message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_NUMBERS),
|
|
259
|
+
data: data,
|
|
260
|
+
errorScheme: numberScheme,
|
|
261
|
+
isSchemeError: true
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (!notEnumTester(valueAsNumber, enumArr)) {
|
|
265
|
+
result = new EjvError({
|
|
266
|
+
type: ERROR_TYPE.NOT_ONE_VALUE_OF,
|
|
267
|
+
message: createErrorMsg(ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
|
|
234
268
|
placeholders: [JSON.stringify(enumArr)]
|
|
235
269
|
}),
|
|
236
270
|
data,
|
|
@@ -248,8 +282,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
248
282
|
: scheme.parent?.min;
|
|
249
283
|
if (!numberTester(effectiveMin)) {
|
|
250
284
|
return new EjvError({
|
|
251
|
-
type:
|
|
252
|
-
message: createErrorMsg(
|
|
285
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
286
|
+
message: createErrorMsg(ERROR_MESSAGE.MIN_SHOULD_BE_NUMBER),
|
|
253
287
|
data: data,
|
|
254
288
|
errorScheme: numberScheme,
|
|
255
289
|
isSchemeError: true
|
|
@@ -258,8 +292,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
258
292
|
if (definedTester(numberScheme.exclusiveMin)) {
|
|
259
293
|
if (!booleanTester(numberScheme.exclusiveMin)) {
|
|
260
294
|
return new EjvError({
|
|
261
|
-
type:
|
|
262
|
-
message: createErrorMsg(
|
|
295
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
296
|
+
message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN),
|
|
263
297
|
data: data,
|
|
264
298
|
errorScheme: numberScheme,
|
|
265
299
|
isSchemeError: true
|
|
@@ -269,8 +303,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
269
303
|
if (numberScheme.exclusiveMin) {
|
|
270
304
|
if (!exclusiveMinNumberTester(valueAsNumber, effectiveMin)) {
|
|
271
305
|
result = new EjvError({
|
|
272
|
-
type:
|
|
273
|
-
message: createErrorMsg(
|
|
306
|
+
type: ERROR_TYPE.BIGGER_THAN,
|
|
307
|
+
message: createErrorMsg(ERROR_MESSAGE.BIGGER_THAN, {
|
|
274
308
|
placeholders: [effectiveMin]
|
|
275
309
|
}),
|
|
276
310
|
data,
|
|
@@ -284,8 +318,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
284
318
|
else {
|
|
285
319
|
if (!minNumberTester(valueAsNumber, effectiveMin)) {
|
|
286
320
|
result = new EjvError({
|
|
287
|
-
type:
|
|
288
|
-
message: createErrorMsg(
|
|
321
|
+
type: ERROR_TYPE.BIGGER_THAN_OR_EQUAL,
|
|
322
|
+
message: createErrorMsg(ERROR_MESSAGE.BIGGER_THAN_OR_EQUAL, {
|
|
289
323
|
placeholders: [effectiveMin]
|
|
290
324
|
}),
|
|
291
325
|
data,
|
|
@@ -304,8 +338,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
304
338
|
: scheme.parent?.max;
|
|
305
339
|
if (!numberTester(effectiveMax)) {
|
|
306
340
|
return new EjvError({
|
|
307
|
-
type:
|
|
308
|
-
message: createErrorMsg(
|
|
341
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
342
|
+
message: createErrorMsg(ERROR_MESSAGE.MAX_SHOULD_BE_NUMBER),
|
|
309
343
|
data: data,
|
|
310
344
|
errorScheme: numberScheme,
|
|
311
345
|
isSchemeError: true
|
|
@@ -314,8 +348,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
314
348
|
if (definedTester(numberScheme.exclusiveMax)) {
|
|
315
349
|
if (!booleanTester(numberScheme.exclusiveMax)) {
|
|
316
350
|
return new EjvError({
|
|
317
|
-
type:
|
|
318
|
-
message: createErrorMsg(
|
|
351
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
352
|
+
message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN),
|
|
319
353
|
data: data,
|
|
320
354
|
errorScheme: numberScheme,
|
|
321
355
|
isSchemeError: true
|
|
@@ -325,8 +359,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
325
359
|
if (numberScheme.exclusiveMax) {
|
|
326
360
|
if (!exclusiveMaxNumberTester(valueAsNumber, effectiveMax)) {
|
|
327
361
|
result = new EjvError({
|
|
328
|
-
type:
|
|
329
|
-
message: createErrorMsg(
|
|
362
|
+
type: ERROR_TYPE.SMALLER_THAN,
|
|
363
|
+
message: createErrorMsg(ERROR_MESSAGE.SMALLER_THAN, {
|
|
330
364
|
placeholders: [effectiveMax]
|
|
331
365
|
}),
|
|
332
366
|
data,
|
|
@@ -340,8 +374,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
340
374
|
else {
|
|
341
375
|
if (!maxNumberTester(valueAsNumber, effectiveMax)) {
|
|
342
376
|
result = new EjvError({
|
|
343
|
-
type:
|
|
344
|
-
message: createErrorMsg(
|
|
377
|
+
type: ERROR_TYPE.SMALLER_THAN_OR_EQUAL,
|
|
378
|
+
message: createErrorMsg(ERROR_MESSAGE.SMALLER_THAN_OR_EQUAL, {
|
|
345
379
|
placeholders: [effectiveMax]
|
|
346
380
|
}),
|
|
347
381
|
data,
|
|
@@ -355,13 +389,13 @@ const _ejv = (data, schemes, options) => {
|
|
|
355
389
|
}
|
|
356
390
|
if (definedTester(numberScheme.format)) {
|
|
357
391
|
let formats;
|
|
358
|
-
const allNumberFormat = Object.values(
|
|
392
|
+
const allNumberFormat = Object.values(NUMBER_FORMAT);
|
|
359
393
|
if (!arrayTester(numberScheme.format)) {
|
|
360
394
|
const formatAsString = numberScheme.format;
|
|
361
395
|
if (!enumTester(formatAsString, allNumberFormat)) {
|
|
362
396
|
return new EjvError({
|
|
363
|
-
type:
|
|
364
|
-
message: createErrorMsg(
|
|
397
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
398
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
|
|
365
399
|
placeholders: [formatAsString]
|
|
366
400
|
}),
|
|
367
401
|
data: data,
|
|
@@ -378,8 +412,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
378
412
|
});
|
|
379
413
|
if (errorFormat) {
|
|
380
414
|
return new EjvError({
|
|
381
|
-
type:
|
|
382
|
-
message: createErrorMsg(
|
|
415
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
416
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
|
|
383
417
|
placeholders: [errorFormat]
|
|
384
418
|
}),
|
|
385
419
|
data: data,
|
|
@@ -392,10 +426,10 @@ const _ejv = (data, schemes, options) => {
|
|
|
392
426
|
const someFormatIsWrong = formats.some((format) => {
|
|
393
427
|
let valid = false;
|
|
394
428
|
switch (format) {
|
|
395
|
-
case
|
|
429
|
+
case NUMBER_FORMAT.INTEGER:
|
|
396
430
|
valid = integerTester(valueAsNumber);
|
|
397
431
|
break;
|
|
398
|
-
case
|
|
432
|
+
case NUMBER_FORMAT.INDEX:
|
|
399
433
|
valid = indexTester(valueAsNumber);
|
|
400
434
|
break;
|
|
401
435
|
}
|
|
@@ -404,8 +438,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
404
438
|
if (!someFormatIsWrong) {
|
|
405
439
|
if (!arrayTester(numberScheme.format)) {
|
|
406
440
|
result = new EjvError({
|
|
407
|
-
type:
|
|
408
|
-
message: createErrorMsg(
|
|
441
|
+
type: ERROR_TYPE.FORMAT,
|
|
442
|
+
message: createErrorMsg(ERROR_MESSAGE.FORMAT, {
|
|
409
443
|
placeholders: [numberScheme.format]
|
|
410
444
|
}),
|
|
411
445
|
data,
|
|
@@ -416,8 +450,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
416
450
|
}
|
|
417
451
|
else {
|
|
418
452
|
result = new EjvError({
|
|
419
|
-
type:
|
|
420
|
-
message: createErrorMsg(
|
|
453
|
+
type: ERROR_TYPE.FORMAT_ONE_OF,
|
|
454
|
+
message: createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
|
|
421
455
|
placeholders: [JSON.stringify(numberScheme.format)]
|
|
422
456
|
}),
|
|
423
457
|
data,
|
|
@@ -431,24 +465,24 @@ const _ejv = (data, schemes, options) => {
|
|
|
431
465
|
}
|
|
432
466
|
break;
|
|
433
467
|
}
|
|
434
|
-
case
|
|
468
|
+
case DATA_TYPE.STRING: {
|
|
435
469
|
const valueAsString = value;
|
|
436
470
|
const stringScheme = scheme;
|
|
437
471
|
if (definedTester(stringScheme.enum)) {
|
|
438
472
|
if (!arrayTester(stringScheme.enum)) {
|
|
439
473
|
return new EjvError({
|
|
440
|
-
type:
|
|
441
|
-
message: createErrorMsg(
|
|
474
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
475
|
+
message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY),
|
|
442
476
|
data: data,
|
|
443
477
|
errorScheme: stringScheme,
|
|
444
478
|
isSchemeError: true
|
|
445
479
|
});
|
|
446
480
|
}
|
|
447
481
|
const enumArr = stringScheme.enum;
|
|
448
|
-
if (!arrayTypeOfTester(enumArr,
|
|
482
|
+
if (!arrayTypeOfTester(enumArr, DATA_TYPE.STRING)) {
|
|
449
483
|
return new EjvError({
|
|
450
|
-
type:
|
|
451
|
-
message: createErrorMsg(
|
|
484
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
485
|
+
message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_STRINGS),
|
|
452
486
|
data: data,
|
|
453
487
|
errorScheme: stringScheme,
|
|
454
488
|
isSchemeError: true
|
|
@@ -456,8 +490,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
456
490
|
}
|
|
457
491
|
if (!enumTester(valueAsString, enumArr)) {
|
|
458
492
|
result = new EjvError({
|
|
459
|
-
type:
|
|
460
|
-
message: createErrorMsg(
|
|
493
|
+
type: ERROR_TYPE.ONE_VALUE_OF,
|
|
494
|
+
message: createErrorMsg(ERROR_MESSAGE.ONE_VALUE_OF, {
|
|
461
495
|
placeholders: [JSON.stringify(stringScheme.enum)]
|
|
462
496
|
}),
|
|
463
497
|
data,
|
|
@@ -468,12 +502,46 @@ const _ejv = (data, schemes, options) => {
|
|
|
468
502
|
break;
|
|
469
503
|
}
|
|
470
504
|
}
|
|
505
|
+
if (definedTester(stringScheme.notEnum)) {
|
|
506
|
+
if (!arrayTester(stringScheme.notEnum)) {
|
|
507
|
+
return new EjvError({
|
|
508
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
509
|
+
message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY),
|
|
510
|
+
data: data,
|
|
511
|
+
errorScheme: stringScheme,
|
|
512
|
+
isSchemeError: true
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
const enumArr = stringScheme.notEnum;
|
|
516
|
+
if (!arrayTypeOfTester(enumArr, DATA_TYPE.STRING)) {
|
|
517
|
+
return new EjvError({
|
|
518
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
519
|
+
message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_STRINGS),
|
|
520
|
+
data: data,
|
|
521
|
+
errorScheme: stringScheme,
|
|
522
|
+
isSchemeError: true
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
if (!notEnumTester(valueAsString, enumArr)) {
|
|
526
|
+
result = new EjvError({
|
|
527
|
+
type: ERROR_TYPE.NOT_ONE_VALUE_OF,
|
|
528
|
+
message: createErrorMsg(ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
|
|
529
|
+
placeholders: [JSON.stringify(stringScheme.notEnum)]
|
|
530
|
+
}),
|
|
531
|
+
data,
|
|
532
|
+
path: _options.path,
|
|
533
|
+
errorScheme: stringScheme,
|
|
534
|
+
errorData: value
|
|
535
|
+
});
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
471
539
|
if (definedTester(stringScheme.length)) {
|
|
472
540
|
const length = stringScheme.length;
|
|
473
541
|
if (!(numberTester(length) && integerTester(length))) {
|
|
474
542
|
return new EjvError({
|
|
475
|
-
type:
|
|
476
|
-
message: createErrorMsg(
|
|
543
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
544
|
+
message: createErrorMsg(ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER),
|
|
477
545
|
data: data,
|
|
478
546
|
errorScheme: stringScheme,
|
|
479
547
|
isSchemeError: true
|
|
@@ -481,8 +549,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
481
549
|
}
|
|
482
550
|
if (!lengthTester(valueAsString, length)) {
|
|
483
551
|
result = new EjvError({
|
|
484
|
-
type:
|
|
485
|
-
message: createErrorMsg(
|
|
552
|
+
type: ERROR_TYPE.LENGTH,
|
|
553
|
+
message: createErrorMsg(ERROR_MESSAGE.LENGTH, {
|
|
486
554
|
placeholders: [length]
|
|
487
555
|
}),
|
|
488
556
|
data,
|
|
@@ -497,8 +565,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
497
565
|
const minLength = stringScheme.minLength;
|
|
498
566
|
if (!(numberTester(minLength) && integerTester(minLength))) {
|
|
499
567
|
return new EjvError({
|
|
500
|
-
type:
|
|
501
|
-
message: createErrorMsg(
|
|
568
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
569
|
+
message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER),
|
|
502
570
|
data: data,
|
|
503
571
|
errorScheme: stringScheme,
|
|
504
572
|
isSchemeError: true
|
|
@@ -506,8 +574,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
506
574
|
}
|
|
507
575
|
if (!minLengthTester(valueAsString, minLength)) {
|
|
508
576
|
result = new EjvError({
|
|
509
|
-
type:
|
|
510
|
-
message: createErrorMsg(
|
|
577
|
+
type: ERROR_TYPE.MIN_LENGTH,
|
|
578
|
+
message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH, {
|
|
511
579
|
placeholders: ['' + minLength]
|
|
512
580
|
}),
|
|
513
581
|
data,
|
|
@@ -522,8 +590,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
522
590
|
const maxLength = stringScheme.maxLength;
|
|
523
591
|
if (!(numberTester(maxLength) && integerTester(maxLength))) {
|
|
524
592
|
return new EjvError({
|
|
525
|
-
type:
|
|
526
|
-
message: createErrorMsg(
|
|
593
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
594
|
+
message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER),
|
|
527
595
|
data: data,
|
|
528
596
|
errorScheme: stringScheme,
|
|
529
597
|
isSchemeError: true
|
|
@@ -531,8 +599,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
531
599
|
}
|
|
532
600
|
if (!maxLengthTester(valueAsString, maxLength)) {
|
|
533
601
|
result = new EjvError({
|
|
534
|
-
type:
|
|
535
|
-
message: createErrorMsg(
|
|
602
|
+
type: ERROR_TYPE.MAX_LENGTH,
|
|
603
|
+
message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH, {
|
|
536
604
|
placeholders: ['' + maxLength]
|
|
537
605
|
}),
|
|
538
606
|
data,
|
|
@@ -545,13 +613,13 @@ const _ejv = (data, schemes, options) => {
|
|
|
545
613
|
}
|
|
546
614
|
if (definedTester(stringScheme.format)) {
|
|
547
615
|
let formats;
|
|
548
|
-
const allStringFormat = Object.values(
|
|
616
|
+
const allStringFormat = Object.values(STRING_FORMAT);
|
|
549
617
|
if (!arrayTester(stringScheme.format)) {
|
|
550
618
|
const formatAsString = stringScheme.format;
|
|
551
619
|
if (!enumTester(formatAsString, allStringFormat)) {
|
|
552
620
|
return new EjvError({
|
|
553
|
-
type:
|
|
554
|
-
message: createErrorMsg(
|
|
621
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
622
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
|
|
555
623
|
placeholders: [formatAsString]
|
|
556
624
|
}),
|
|
557
625
|
data: data,
|
|
@@ -568,8 +636,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
568
636
|
});
|
|
569
637
|
if (errorFormat) {
|
|
570
638
|
return new EjvError({
|
|
571
|
-
type:
|
|
572
|
-
message: createErrorMsg(
|
|
639
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
640
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
|
|
573
641
|
placeholders: [errorFormat]
|
|
574
642
|
}),
|
|
575
643
|
data: data,
|
|
@@ -582,16 +650,16 @@ const _ejv = (data, schemes, options) => {
|
|
|
582
650
|
const foundFormatMatching = formats.some((format) => {
|
|
583
651
|
let valid = false;
|
|
584
652
|
switch (format) {
|
|
585
|
-
case
|
|
653
|
+
case STRING_FORMAT.EMAIL:
|
|
586
654
|
valid = emailTester(valueAsString);
|
|
587
655
|
break;
|
|
588
|
-
case
|
|
656
|
+
case STRING_FORMAT.DATE:
|
|
589
657
|
valid = dateFormatTester(valueAsString);
|
|
590
658
|
break;
|
|
591
|
-
case
|
|
659
|
+
case STRING_FORMAT.TIME:
|
|
592
660
|
valid = timeFormatTester(valueAsString);
|
|
593
661
|
break;
|
|
594
|
-
case
|
|
662
|
+
case STRING_FORMAT.DATE_TIME:
|
|
595
663
|
valid = dateTimeFormatTester(valueAsString);
|
|
596
664
|
break;
|
|
597
665
|
}
|
|
@@ -600,8 +668,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
600
668
|
if (!foundFormatMatching) {
|
|
601
669
|
if (!arrayTester(stringScheme.format)) {
|
|
602
670
|
result = new EjvError({
|
|
603
|
-
type:
|
|
604
|
-
message: createErrorMsg(
|
|
671
|
+
type: ERROR_TYPE.FORMAT,
|
|
672
|
+
message: createErrorMsg(ERROR_MESSAGE.FORMAT, {
|
|
605
673
|
placeholders: [stringScheme.format]
|
|
606
674
|
}),
|
|
607
675
|
data,
|
|
@@ -612,8 +680,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
612
680
|
}
|
|
613
681
|
else {
|
|
614
682
|
result = new EjvError({
|
|
615
|
-
type:
|
|
616
|
-
message: createErrorMsg(
|
|
683
|
+
type: ERROR_TYPE.FORMAT_ONE_OF,
|
|
684
|
+
message: createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
|
|
617
685
|
placeholders: [JSON.stringify(stringScheme.format)]
|
|
618
686
|
}),
|
|
619
687
|
data,
|
|
@@ -629,8 +697,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
629
697
|
// check parameter
|
|
630
698
|
if (stringScheme.pattern === null) {
|
|
631
699
|
return new EjvError({
|
|
632
|
-
type:
|
|
633
|
-
message: createErrorMsg(
|
|
700
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
701
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
|
|
634
702
|
placeholders: ['null']
|
|
635
703
|
}),
|
|
636
704
|
data: data,
|
|
@@ -673,8 +741,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
673
741
|
const patternsAsArray = stringScheme.pattern;
|
|
674
742
|
if (!minLengthTester(patternsAsArray, 1)) { // empty array
|
|
675
743
|
return new EjvError({
|
|
676
|
-
type:
|
|
677
|
-
message: createErrorMsg(
|
|
744
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
745
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
|
|
678
746
|
placeholders: [createArrayErrorMsg(patternsAsArray)]
|
|
679
747
|
}),
|
|
680
748
|
data: data,
|
|
@@ -685,7 +753,7 @@ const _ejv = (data, schemes, options) => {
|
|
|
685
753
|
try {
|
|
686
754
|
const regExpPatterns = patternsAsArray.map((pattern) => {
|
|
687
755
|
if (!isValidPattern(pattern)) {
|
|
688
|
-
throw new Error(createErrorMsg(
|
|
756
|
+
throw new Error(createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
|
|
689
757
|
placeholders: [createArrayErrorMsg(patternsAsArray)]
|
|
690
758
|
}));
|
|
691
759
|
}
|
|
@@ -697,8 +765,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
697
765
|
});
|
|
698
766
|
if (!foundMatchPattern) {
|
|
699
767
|
result = new EjvError({
|
|
700
|
-
type:
|
|
701
|
-
message: createErrorMsg(
|
|
768
|
+
type: ERROR_TYPE.PATTERN_ONE_OF,
|
|
769
|
+
message: createErrorMsg(ERROR_MESSAGE.PATTERN_ONE_OF, {
|
|
702
770
|
placeholders: [createArrayErrorMsg(patternsAsArray)]
|
|
703
771
|
}),
|
|
704
772
|
data,
|
|
@@ -709,10 +777,10 @@ const _ejv = (data, schemes, options) => {
|
|
|
709
777
|
break;
|
|
710
778
|
}
|
|
711
779
|
}
|
|
712
|
-
catch (e) {
|
|
780
|
+
catch (e) { // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
713
781
|
return new EjvError({
|
|
714
|
-
type:
|
|
715
|
-
message: createErrorMsg(
|
|
782
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
783
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
|
|
716
784
|
placeholders: [createArrayErrorMsg(patternsAsArray)]
|
|
717
785
|
}),
|
|
718
786
|
data: data,
|
|
@@ -725,8 +793,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
725
793
|
const patternAsOne = stringScheme.pattern;
|
|
726
794
|
if (!isValidPattern(patternAsOne)) {
|
|
727
795
|
return new EjvError({
|
|
728
|
-
type:
|
|
729
|
-
message: createErrorMsg(
|
|
796
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
797
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
|
|
730
798
|
placeholders: [patternToString(patternAsOne)]
|
|
731
799
|
}),
|
|
732
800
|
data: data,
|
|
@@ -738,8 +806,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
738
806
|
const regExp = new RegExp(patternAsOne);
|
|
739
807
|
if (!stringRegExpTester(valueAsString, regExp)) {
|
|
740
808
|
result = new EjvError({
|
|
741
|
-
type:
|
|
742
|
-
message: createErrorMsg(
|
|
809
|
+
type: ERROR_TYPE.PATTERN,
|
|
810
|
+
message: createErrorMsg(ERROR_MESSAGE.PATTERN, {
|
|
743
811
|
placeholders: [patternToString(patternAsOne)]
|
|
744
812
|
}),
|
|
745
813
|
data,
|
|
@@ -753,14 +821,14 @@ const _ejv = (data, schemes, options) => {
|
|
|
753
821
|
}
|
|
754
822
|
break;
|
|
755
823
|
}
|
|
756
|
-
case
|
|
824
|
+
case DATA_TYPE.OBJECT: {
|
|
757
825
|
const valueAsObject = value;
|
|
758
826
|
const objectScheme = scheme;
|
|
759
827
|
if (definedTester(objectScheme.allowNoProperty)) {
|
|
760
828
|
if (!booleanTester(objectScheme.allowNoProperty)) {
|
|
761
829
|
return new EjvError({
|
|
762
|
-
type:
|
|
763
|
-
message: createErrorMsg(
|
|
830
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
831
|
+
message: createErrorMsg(ERROR_MESSAGE.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN),
|
|
764
832
|
data: data,
|
|
765
833
|
errorScheme: objectScheme,
|
|
766
834
|
isSchemeError: true
|
|
@@ -768,8 +836,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
768
836
|
}
|
|
769
837
|
if (!objectScheme.allowNoProperty && !hasPropertyTester(valueAsObject)) {
|
|
770
838
|
result = new EjvError({
|
|
771
|
-
type:
|
|
772
|
-
message:
|
|
839
|
+
type: ERROR_TYPE.PROPERTY,
|
|
840
|
+
message: ERROR_MESSAGE.PROPERTY,
|
|
773
841
|
data,
|
|
774
842
|
path: _options.path,
|
|
775
843
|
errorScheme: objectScheme,
|
|
@@ -781,8 +849,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
781
849
|
if (definedTester(objectScheme.properties)) {
|
|
782
850
|
if (!arrayTester(objectScheme.properties)) {
|
|
783
851
|
return new EjvError({
|
|
784
|
-
type:
|
|
785
|
-
message: createErrorMsg(
|
|
852
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
853
|
+
message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY),
|
|
786
854
|
data: data,
|
|
787
855
|
errorScheme: objectScheme,
|
|
788
856
|
isSchemeError: true
|
|
@@ -791,17 +859,17 @@ const _ejv = (data, schemes, options) => {
|
|
|
791
859
|
const properties = objectScheme.properties;
|
|
792
860
|
if (!minLengthTester(properties, 1)) {
|
|
793
861
|
return new EjvError({
|
|
794
|
-
type:
|
|
795
|
-
message: createErrorMsg(
|
|
862
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
863
|
+
message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_HAVE_ITEMS),
|
|
796
864
|
data: data,
|
|
797
865
|
errorScheme: objectScheme,
|
|
798
866
|
isSchemeError: true
|
|
799
867
|
});
|
|
800
868
|
}
|
|
801
|
-
if (!arrayTypeOfTester(properties,
|
|
869
|
+
if (!arrayTypeOfTester(properties, DATA_TYPE.OBJECT)) {
|
|
802
870
|
return new EjvError({
|
|
803
|
-
type:
|
|
804
|
-
message: createErrorMsg(
|
|
871
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
872
|
+
message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT),
|
|
805
873
|
data: data,
|
|
806
874
|
errorScheme: objectScheme,
|
|
807
875
|
isSchemeError: true
|
|
@@ -809,8 +877,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
809
877
|
}
|
|
810
878
|
if (!objectTester(value)) {
|
|
811
879
|
result = new EjvError({
|
|
812
|
-
type:
|
|
813
|
-
message: createErrorMsg(
|
|
880
|
+
type: ERROR_TYPE.TYPE_MISMATCH,
|
|
881
|
+
message: createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
|
|
814
882
|
placeholders: ['object']
|
|
815
883
|
}),
|
|
816
884
|
data,
|
|
@@ -832,7 +900,7 @@ const _ejv = (data, schemes, options) => {
|
|
|
832
900
|
}
|
|
833
901
|
break;
|
|
834
902
|
}
|
|
835
|
-
case
|
|
903
|
+
case DATA_TYPE.DATE: {
|
|
836
904
|
const valueAsDate = value;
|
|
837
905
|
const dateScheme = scheme;
|
|
838
906
|
const parentDateScheme = scheme.parent;
|
|
@@ -844,8 +912,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
844
912
|
|| dateTimeFormatTester(minDateCandidate)))
|
|
845
913
|
|| dateTester(minDateCandidate))) {
|
|
846
914
|
return new EjvError({
|
|
847
|
-
type:
|
|
848
|
-
message: createErrorMsg(
|
|
915
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
916
|
+
message: createErrorMsg(ERROR_MESSAGE.MIN_DATE_SHOULD_BE_DATE_OR_STRING),
|
|
849
917
|
data: data,
|
|
850
918
|
errorScheme: dateScheme,
|
|
851
919
|
isSchemeError: true
|
|
@@ -855,8 +923,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
855
923
|
if (definedTester(dateScheme.exclusiveMin)) {
|
|
856
924
|
if (!booleanTester(dateScheme.exclusiveMin)) {
|
|
857
925
|
return new EjvError({
|
|
858
|
-
type:
|
|
859
|
-
message: createErrorMsg(
|
|
926
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
927
|
+
message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN),
|
|
860
928
|
data: data,
|
|
861
929
|
errorScheme: dateScheme,
|
|
862
930
|
isSchemeError: true
|
|
@@ -865,8 +933,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
865
933
|
if (dateScheme.exclusiveMin) {
|
|
866
934
|
if (!exclusiveMinDateTester(valueAsDate, effectiveMin)) {
|
|
867
935
|
result = new EjvError({
|
|
868
|
-
type:
|
|
869
|
-
message: createErrorMsg(
|
|
936
|
+
type: ERROR_TYPE.AFTER_DATE,
|
|
937
|
+
message: createErrorMsg(ERROR_MESSAGE.AFTER_DATE, {
|
|
870
938
|
placeholders: [effectiveMin.toISOString()]
|
|
871
939
|
}),
|
|
872
940
|
data,
|
|
@@ -880,8 +948,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
880
948
|
else {
|
|
881
949
|
if (!minDateTester(valueAsDate, effectiveMin)) {
|
|
882
950
|
result = new EjvError({
|
|
883
|
-
type:
|
|
884
|
-
message: createErrorMsg(
|
|
951
|
+
type: ERROR_TYPE.AFTER_OR_SAME_DATE,
|
|
952
|
+
message: createErrorMsg(ERROR_MESSAGE.AFTER_OR_SAME_DATE, {
|
|
885
953
|
placeholders: [effectiveMin.toISOString()]
|
|
886
954
|
}),
|
|
887
955
|
data,
|
|
@@ -896,8 +964,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
896
964
|
else {
|
|
897
965
|
if (!minDateTester(valueAsDate, effectiveMin)) {
|
|
898
966
|
result = new EjvError({
|
|
899
|
-
type:
|
|
900
|
-
message: createErrorMsg(
|
|
967
|
+
type: ERROR_TYPE.AFTER_OR_SAME_DATE,
|
|
968
|
+
message: createErrorMsg(ERROR_MESSAGE.AFTER_OR_SAME_DATE, {
|
|
901
969
|
placeholders: [effectiveMin.toISOString()]
|
|
902
970
|
}),
|
|
903
971
|
data,
|
|
@@ -917,8 +985,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
917
985
|
|| dateTimeFormatTester(maxDateCandidate)))
|
|
918
986
|
|| dateTester(maxDateCandidate))) {
|
|
919
987
|
return new EjvError({
|
|
920
|
-
type:
|
|
921
|
-
message: createErrorMsg(
|
|
988
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
989
|
+
message: createErrorMsg(ERROR_MESSAGE.MAX_DATE_SHOULD_BE_DATE_OR_STRING),
|
|
922
990
|
data: data,
|
|
923
991
|
errorScheme: dateScheme,
|
|
924
992
|
isSchemeError: true
|
|
@@ -928,8 +996,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
928
996
|
if (definedTester(dateScheme.exclusiveMax)) {
|
|
929
997
|
if (!booleanTester(dateScheme.exclusiveMax)) {
|
|
930
998
|
return new EjvError({
|
|
931
|
-
type:
|
|
932
|
-
message: createErrorMsg(
|
|
999
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1000
|
+
message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN),
|
|
933
1001
|
data: data,
|
|
934
1002
|
errorScheme: dateScheme,
|
|
935
1003
|
isSchemeError: true
|
|
@@ -939,8 +1007,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
939
1007
|
if (dateScheme.exclusiveMax) {
|
|
940
1008
|
if (!exclusiveMaxDateTester(valueAsDate, effectiveMax)) {
|
|
941
1009
|
result = new EjvError({
|
|
942
|
-
type:
|
|
943
|
-
message: createErrorMsg(
|
|
1010
|
+
type: ERROR_TYPE.BEFORE_DATE,
|
|
1011
|
+
message: createErrorMsg(ERROR_MESSAGE.BEFORE_DATE, {
|
|
944
1012
|
placeholders: [effectiveMax.toISOString()]
|
|
945
1013
|
}),
|
|
946
1014
|
data,
|
|
@@ -954,8 +1022,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
954
1022
|
else {
|
|
955
1023
|
if (!maxDateTester(valueAsDate, effectiveMax)) {
|
|
956
1024
|
result = new EjvError({
|
|
957
|
-
type:
|
|
958
|
-
message: createErrorMsg(
|
|
1025
|
+
type: ERROR_TYPE.BEFORE_OR_SAME_DATE,
|
|
1026
|
+
message: createErrorMsg(ERROR_MESSAGE.BEFORE_OR_SAME_DATE, {
|
|
959
1027
|
placeholders: [effectiveMax.toISOString()]
|
|
960
1028
|
}),
|
|
961
1029
|
data,
|
|
@@ -969,15 +1037,15 @@ const _ejv = (data, schemes, options) => {
|
|
|
969
1037
|
}
|
|
970
1038
|
break;
|
|
971
1039
|
}
|
|
972
|
-
case
|
|
1040
|
+
case DATA_TYPE.ARRAY: {
|
|
973
1041
|
const valueAsArray = value;
|
|
974
1042
|
const arrayScheme = scheme;
|
|
975
1043
|
if (definedTester(arrayScheme.length)) {
|
|
976
1044
|
const length = arrayScheme.length;
|
|
977
1045
|
if (!(numberTester(length) && integerTester(length))) {
|
|
978
1046
|
return new EjvError({
|
|
979
|
-
type:
|
|
980
|
-
message: createErrorMsg(
|
|
1047
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1048
|
+
message: createErrorMsg(ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER),
|
|
981
1049
|
data: data,
|
|
982
1050
|
errorScheme: arrayScheme,
|
|
983
1051
|
isSchemeError: true
|
|
@@ -985,8 +1053,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
985
1053
|
}
|
|
986
1054
|
if (!lengthTester(valueAsArray, length)) {
|
|
987
1055
|
result = new EjvError({
|
|
988
|
-
type:
|
|
989
|
-
message: createErrorMsg(
|
|
1056
|
+
type: ERROR_TYPE.LENGTH,
|
|
1057
|
+
message: createErrorMsg(ERROR_MESSAGE.LENGTH, {
|
|
990
1058
|
placeholders: ['' + length]
|
|
991
1059
|
}),
|
|
992
1060
|
data,
|
|
@@ -1001,8 +1069,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1001
1069
|
const minLength = arrayScheme.minLength;
|
|
1002
1070
|
if (!(numberTester(arrayScheme.minLength) && integerTester(minLength))) {
|
|
1003
1071
|
return new EjvError({
|
|
1004
|
-
type:
|
|
1005
|
-
message: createErrorMsg(
|
|
1072
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1073
|
+
message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER),
|
|
1006
1074
|
data: data,
|
|
1007
1075
|
errorScheme: arrayScheme,
|
|
1008
1076
|
isSchemeError: true
|
|
@@ -1010,8 +1078,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1010
1078
|
}
|
|
1011
1079
|
if (!minLengthTester(valueAsArray, minLength)) {
|
|
1012
1080
|
result = new EjvError({
|
|
1013
|
-
type:
|
|
1014
|
-
message: createErrorMsg(
|
|
1081
|
+
type: ERROR_TYPE.MIN_LENGTH,
|
|
1082
|
+
message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH, {
|
|
1015
1083
|
placeholders: ['' + minLength]
|
|
1016
1084
|
}),
|
|
1017
1085
|
data,
|
|
@@ -1026,8 +1094,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1026
1094
|
const maxLength = arrayScheme.maxLength;
|
|
1027
1095
|
if (!(numberTester(arrayScheme.maxLength) && integerTester(maxLength))) {
|
|
1028
1096
|
return new EjvError({
|
|
1029
|
-
type:
|
|
1030
|
-
message: createErrorMsg(
|
|
1097
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1098
|
+
message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER),
|
|
1031
1099
|
data: data,
|
|
1032
1100
|
errorScheme: arrayScheme,
|
|
1033
1101
|
isSchemeError: true
|
|
@@ -1035,8 +1103,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1035
1103
|
}
|
|
1036
1104
|
if (!maxLengthTester(valueAsArray, maxLength)) {
|
|
1037
1105
|
result = new EjvError({
|
|
1038
|
-
type:
|
|
1039
|
-
message: createErrorMsg(
|
|
1106
|
+
type: ERROR_TYPE.MAX_LENGTH,
|
|
1107
|
+
message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH, {
|
|
1040
1108
|
placeholders: ['' + maxLength]
|
|
1041
1109
|
}),
|
|
1042
1110
|
data,
|
|
@@ -1050,8 +1118,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1050
1118
|
if (definedTester(arrayScheme.unique)) {
|
|
1051
1119
|
if (!booleanTester(arrayScheme.unique)) {
|
|
1052
1120
|
return new EjvError({
|
|
1053
|
-
type:
|
|
1054
|
-
message: createErrorMsg(
|
|
1121
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1122
|
+
message: createErrorMsg(ERROR_MESSAGE.UNIQUE_SHOULD_BE_BOOLEAN),
|
|
1055
1123
|
data: data,
|
|
1056
1124
|
errorScheme: arrayScheme,
|
|
1057
1125
|
isSchemeError: true
|
|
@@ -1059,8 +1127,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1059
1127
|
}
|
|
1060
1128
|
if (arrayScheme.unique && !uniqueItemsTester(valueAsArray)) {
|
|
1061
1129
|
result = new EjvError({
|
|
1062
|
-
type:
|
|
1063
|
-
message:
|
|
1130
|
+
type: ERROR_TYPE.UNIQUE_ITEMS,
|
|
1131
|
+
message: ERROR_MESSAGE.UNIQUE_ITEMS,
|
|
1064
1132
|
data,
|
|
1065
1133
|
path: _options.path,
|
|
1066
1134
|
errorScheme: arrayScheme,
|
|
@@ -1077,7 +1145,7 @@ const _ejv = (data, schemes, options) => {
|
|
|
1077
1145
|
return '' + (+now + i);
|
|
1078
1146
|
});
|
|
1079
1147
|
if (stringTester(arrayScheme.items) // by DataType
|
|
1080
|
-
|| (arrayTester(arrayScheme.items) && arrayTypeOfTester(arrayScheme.items,
|
|
1148
|
+
|| (arrayTester(arrayScheme.items) && arrayTypeOfTester(arrayScheme.items, DATA_TYPE.STRING)) // by DataType[]
|
|
1081
1149
|
) {
|
|
1082
1150
|
const itemTypes = (arrayTester(arrayScheme.items)
|
|
1083
1151
|
? arrayScheme.items
|
|
@@ -1089,8 +1157,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1089
1157
|
});
|
|
1090
1158
|
if (itemTypeError) {
|
|
1091
1159
|
return new EjvError({
|
|
1092
|
-
type:
|
|
1093
|
-
message: createErrorMsg(
|
|
1160
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1161
|
+
message: createErrorMsg(ERROR_MESSAGE.SCHEMES_HAS_INVALID_TYPE, {
|
|
1094
1162
|
placeholders: [itemTypeError]
|
|
1095
1163
|
}),
|
|
1096
1164
|
data: data,
|
|
@@ -1114,12 +1182,12 @@ const _ejv = (data, schemes, options) => {
|
|
|
1114
1182
|
if (partialResult) {
|
|
1115
1183
|
let errorMsg;
|
|
1116
1184
|
if (arrayTester(arrayScheme.items)) {
|
|
1117
|
-
errorMsg = createErrorMsg(
|
|
1185
|
+
errorMsg = createErrorMsg(ERROR_MESSAGE.ITEMS_TYPE, {
|
|
1118
1186
|
placeholders: [JSON.stringify(itemTypes)]
|
|
1119
1187
|
});
|
|
1120
1188
|
}
|
|
1121
1189
|
else {
|
|
1122
|
-
errorMsg = createErrorMsg(
|
|
1190
|
+
errorMsg = createErrorMsg(ERROR_MESSAGE.ITEMS_TYPE, {
|
|
1123
1191
|
placeholders: [arrayScheme.items]
|
|
1124
1192
|
});
|
|
1125
1193
|
}
|
|
@@ -1130,7 +1198,7 @@ const _ejv = (data, schemes, options) => {
|
|
|
1130
1198
|
});
|
|
1131
1199
|
const partialKeyIndex = partialSchemes.indexOf(partialScheme);
|
|
1132
1200
|
result = new EjvError({
|
|
1133
|
-
type:
|
|
1201
|
+
type: ERROR_TYPE.ITEMS_TYPE,
|
|
1134
1202
|
message: errorMsg,
|
|
1135
1203
|
data,
|
|
1136
1204
|
path: [..._options.path, '' + partialKeyIndex],
|
|
@@ -1141,7 +1209,7 @@ const _ejv = (data, schemes, options) => {
|
|
|
1141
1209
|
break;
|
|
1142
1210
|
}
|
|
1143
1211
|
else if ((objectTester(arrayScheme.items) && arrayScheme.items !== null) // by Scheme
|
|
1144
|
-
|| (arrayTester(arrayScheme.items) && arrayTypeOfTester(arrayScheme.items,
|
|
1212
|
+
|| (arrayTester(arrayScheme.items) && arrayTypeOfTester(arrayScheme.items, DATA_TYPE.OBJECT)) // by Scheme[]
|
|
1145
1213
|
) {
|
|
1146
1214
|
const itemsAsSchemes = arrayTester(arrayScheme.items)
|
|
1147
1215
|
? arrayScheme.items
|
|
@@ -1179,18 +1247,18 @@ const _ejv = (data, schemes, options) => {
|
|
|
1179
1247
|
let errorType;
|
|
1180
1248
|
let errorMsg;
|
|
1181
1249
|
if (!!itemsAsSchemes && itemsAsSchemes.length > 1) {
|
|
1182
|
-
errorType =
|
|
1183
|
-
errorMsg = createErrorMsg(
|
|
1250
|
+
errorType = ERROR_TYPE.ITEMS_SCHEMES;
|
|
1251
|
+
errorMsg = createErrorMsg(ERROR_MESSAGE.ITEMS_SCHEMES, {
|
|
1184
1252
|
placeholders: [JSON.stringify(itemsAsSchemes)]
|
|
1185
1253
|
});
|
|
1186
1254
|
}
|
|
1187
1255
|
else {
|
|
1188
1256
|
errorType = partialError.type;
|
|
1189
1257
|
errorMsg = partialError.message;
|
|
1190
|
-
if (errorType ===
|
|
1258
|
+
if (errorType === ERROR_TYPE.REQUIRED) {
|
|
1191
1259
|
// REQUIRED in array is TYPE_MISMATCH except with nullable === true
|
|
1192
|
-
errorType =
|
|
1193
|
-
errorMsg = createErrorMsg(
|
|
1260
|
+
errorType = ERROR_TYPE.TYPE_MISMATCH;
|
|
1261
|
+
errorMsg = createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
|
|
1194
1262
|
placeholders: [JSON.stringify(arrayScheme.items)]
|
|
1195
1263
|
});
|
|
1196
1264
|
}
|
|
@@ -1200,7 +1268,7 @@ const _ejv = (data, schemes, options) => {
|
|
|
1200
1268
|
message: errorMsg,
|
|
1201
1269
|
data
|
|
1202
1270
|
});
|
|
1203
|
-
if (errorType ===
|
|
1271
|
+
if (errorType === ERROR_TYPE.INVALID_SCHEMES) {
|
|
1204
1272
|
result.errorScheme = arrayScheme;
|
|
1205
1273
|
result.isSchemeError = true;
|
|
1206
1274
|
result.isDataError = false;
|
|
@@ -1214,8 +1282,8 @@ const _ejv = (data, schemes, options) => {
|
|
|
1214
1282
|
}
|
|
1215
1283
|
else {
|
|
1216
1284
|
return new EjvError({
|
|
1217
|
-
type:
|
|
1218
|
-
message: createErrorMsg(
|
|
1285
|
+
type: ERROR_TYPE.INVALID_SCHEMES,
|
|
1286
|
+
message: createErrorMsg(ERROR_MESSAGE.INVALID_ITEMS_SCHEME, {
|
|
1219
1287
|
placeholders: [JSON.stringify(arrayScheme.items)]
|
|
1220
1288
|
}),
|
|
1221
1289
|
data: data,
|
|
@@ -1246,8 +1314,8 @@ export const ejv = (data, schemes, options) => {
|
|
|
1246
1314
|
// check data itself
|
|
1247
1315
|
if (!definedTester(data) || !objectTester(data) || data === null) {
|
|
1248
1316
|
return new EjvError({
|
|
1249
|
-
type:
|
|
1250
|
-
message:
|
|
1317
|
+
type: ERROR_TYPE.NO_DATA,
|
|
1318
|
+
message: ERROR_MESSAGE.NO_DATA,
|
|
1251
1319
|
data: data,
|
|
1252
1320
|
path: undefined,
|
|
1253
1321
|
errorScheme: undefined,
|