ejv 2.1.0 → 2.1.2

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 (62) hide show
  1. package/.mocharc.json +8 -8
  2. package/CHANGELOG.md +134 -127
  3. package/README-KR.md +597 -597
  4. package/README.md +603 -603
  5. package/build/cjs/constants.js +124 -0
  6. package/build/cjs/constants.js.map +1 -0
  7. package/build/cjs/ejv.js +1341 -0
  8. package/build/cjs/ejv.js.map +1 -0
  9. package/build/cjs/index.js +14 -0
  10. package/build/cjs/index.js.map +1 -0
  11. package/build/cjs/interfaces.js +29 -0
  12. package/build/cjs/interfaces.js.map +1 -0
  13. package/build/cjs/package.json +1 -0
  14. package/build/cjs/tester.js +288 -0
  15. package/build/cjs/tester.js.map +1 -0
  16. package/build/cjs/util.js +103 -0
  17. package/build/cjs/util.js.map +1 -0
  18. package/build/constants.d.ts +107 -0
  19. package/build/ejv.d.ts +2 -0
  20. package/build/esm/constants.js +121 -0
  21. package/build/esm/constants.js.map +1 -0
  22. package/build/esm/ejv.js +1339 -0
  23. package/build/esm/ejv.js.map +1 -0
  24. package/build/esm/index.js +4 -0
  25. package/build/esm/index.js.map +1 -0
  26. package/build/esm/interfaces.js +33 -0
  27. package/build/esm/interfaces.js.map +1 -0
  28. package/build/esm/package.json +1 -0
  29. package/build/esm/tester.js +252 -0
  30. package/build/esm/tester.js.map +1 -0
  31. package/build/esm/util.js +96 -0
  32. package/build/esm/util.js.map +1 -0
  33. package/build/index.d.ts +3 -0
  34. package/build/interfaces.d.ts +77 -0
  35. package/build/scripts/add-js-extensions.js +46 -0
  36. package/build/scripts/add-js-extensions.js.map +1 -0
  37. package/build/tester.d.ts +35 -0
  38. package/build/util.d.ts +7 -0
  39. package/eslint.config.mjs +66 -59
  40. package/package.json +54 -55
  41. package/scripts/add-js-extensions.ts +59 -59
  42. package/spec/ArrayScheme.ts +1021 -1021
  43. package/spec/CommonScheme.ts +251 -251
  44. package/spec/DateScheme.ts +472 -472
  45. package/spec/NumberScheme.ts +1160 -1160
  46. package/spec/ObjectScheme.ts +499 -499
  47. package/spec/RegExpScheme.ts +112 -112
  48. package/spec/StringScheme.ts +1407 -1336
  49. package/spec/common-test-util.ts +63 -63
  50. package/spec/ejv.spec.ts +235 -235
  51. package/spec/testers.spec.ts +833 -833
  52. package/src/constants.ts +164 -162
  53. package/src/ejv.ts +1751 -1746
  54. package/src/index.ts +14 -14
  55. package/src/interfaces.ts +144 -144
  56. package/src/tester.ts +323 -312
  57. package/src/util.ts +124 -124
  58. package/tsconfig.cjs.json +8 -8
  59. package/tsconfig.esm.json +7 -7
  60. package/tsconfig.json +19 -18
  61. package/tsconfig.scripts.json +14 -14
  62. package/tsconfig.types.json +9 -9
@@ -1,499 +1,499 @@
1
- import { describe, it } from 'mocha';
2
- import { expect } from 'chai';
3
-
4
- import { ejv } from '../src/ejv';
5
-
6
- import { EjvError, Scheme } from '../src/interfaces';
7
- import { ERROR_MESSAGE, ERROR_TYPE } from '../src/constants';
8
- import { createErrorMsg } from '../src/util';
9
- import { checkSchemeError, TypeTester, typeTesterArr } from './common-test-util';
10
-
11
-
12
- describe('ObjectScheme', () => {
13
- describe('type', () => {
14
- describe('mismatch', () => {
15
- typeTesterArr
16
- .filter((obj: TypeTester): boolean => {
17
- return !['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
18
- })
19
- .forEach((obj: TypeTester): void => {
20
- const data = {
21
- a: obj.value
22
- };
23
-
24
- it(obj.type, () => {
25
- const error: EjvError | null = ejv(data, [{
26
- key: 'a',
27
- type: 'object'
28
- }]);
29
-
30
- expect(error).to.be.instanceof(EjvError);
31
-
32
- if (!error) {
33
- throw new Error('spec failed');
34
- }
35
-
36
- expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH);
37
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
38
- placeholders: ['object']
39
- }));
40
- expect(error.path).to.be.eql('a');
41
- expect(error.data).to.be.deep.equal(data);
42
- expect(error.errorData).to.be.eql(obj.value);
43
- });
44
- });
45
-
46
- it('multiple types', () => {
47
- const value = {};
48
- const typeArr: string[] = ['boolean', 'number'];
49
-
50
- const data = {
51
- a: value
52
- };
53
-
54
- const error: EjvError | null = ejv(data, [{
55
- key: 'a',
56
- type: typeArr
57
- }]);
58
-
59
- expect(error).to.be.instanceof(EjvError);
60
-
61
- if (!error) {
62
- throw new Error('spec failed');
63
- }
64
-
65
- expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH_ONE_OF);
66
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
67
- placeholders: [JSON.stringify(typeArr)]
68
- }));
69
- expect(error.path).to.be.eql('a');
70
- expect(error.data).to.be.deep.equal(data);
71
- expect(error.errorData).to.be.eql(value);
72
- });
73
- });
74
-
75
- describe('match', () => {
76
- it('optional', () => {
77
- expect(ejv({
78
- a: undefined
79
- }, [{
80
- key: 'a',
81
- type: 'object',
82
- optional: true
83
- }])).to.be.null;
84
- });
85
-
86
- typeTesterArr
87
- .filter((obj: TypeTester): boolean => {
88
- return ['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
89
- })
90
- .forEach((obj: TypeTester): void => {
91
- it(obj.type, () => {
92
- expect(ejv({
93
- a: obj.value
94
- }, [{
95
- key: 'a',
96
- type: 'object'
97
- }])).to.be.null;
98
- });
99
- });
100
-
101
- it('single type', () => {
102
- expect(ejv({
103
- a: {
104
- b: 1
105
- }
106
- }, [{
107
- key: 'a',
108
- type: 'object'
109
- }])).to.be.null;
110
- });
111
-
112
- it('multiple types', () => {
113
- expect(ejv({
114
- a: {
115
- b: 1
116
- }
117
- }, [{
118
- key: 'a',
119
- type: ['object', 'number']
120
- }])).to.be.null;
121
- });
122
-
123
- it('multiple types', () => {
124
- expect(ejv({
125
- a: {
126
- b: 1
127
- }
128
- }, [{
129
- key: 'a',
130
- type: ['number', 'object']
131
- }])).to.be.null;
132
- });
133
- });
134
- });
135
-
136
- describe('properties', () => {
137
- describe('check parameter', () => {
138
- const data = {
139
- a: {
140
- b: 1
141
- }
142
- };
143
-
144
- it('undefined is ok', () => {
145
- expect(ejv(data, [{
146
- key: 'a',
147
- type: 'object',
148
- properties: undefined
149
- }])).to.be.null;
150
- });
151
-
152
- it('null', () => {
153
- const errorScheme: Scheme = {
154
- key: 'a',
155
- type: 'object',
156
- properties: null as unknown as Scheme[]
157
- };
158
-
159
- checkSchemeError({
160
- data,
161
- errorScheme,
162
- message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY)
163
- });
164
- });
165
-
166
- it('not array', () => {
167
- const errorScheme: Scheme = {
168
- key: 'a',
169
- type: 'object',
170
- properties: 'b' as unknown as Scheme[]
171
- };
172
-
173
- checkSchemeError({
174
- data,
175
- errorScheme,
176
- message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY)
177
- });
178
- });
179
-
180
- it('empty array', () => {
181
- const errorScheme: Scheme = {
182
- key: 'a',
183
- type: 'object',
184
- properties: []
185
- };
186
-
187
- checkSchemeError({
188
- data,
189
- errorScheme,
190
- message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_HAVE_ITEMS)
191
- });
192
- });
193
-
194
- it('not object array', () => {
195
- const errorScheme: Scheme = {
196
- key: 'a',
197
- type: 'object',
198
- properties: ['b'] as unknown as Scheme[]
199
- };
200
-
201
- checkSchemeError({
202
- data,
203
- errorScheme,
204
- message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT)
205
- });
206
- });
207
- });
208
-
209
- it('with single type', () => {
210
- const undefinedError: EjvError | null = ejv({
211
- a: {
212
- b: undefined
213
- }
214
- }, [{
215
- key: 'a',
216
- type: 'object',
217
- properties: [{
218
- key: 'b',
219
- type: 'string'
220
- }]
221
- }]);
222
-
223
- expect(undefinedError).to.be.instanceof(EjvError);
224
-
225
- if (!undefinedError) {
226
- throw new Error('spec failed');
227
- }
228
-
229
- expect(undefinedError.type).to.be.eql(ERROR_TYPE.REQUIRED);
230
- expect(undefinedError.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.REQUIRED));
231
- expect(undefinedError.path).to.be.eql('a/b');
232
-
233
- const nullError: EjvError | null = ejv({
234
- a: null
235
- }, [{
236
- key: 'a',
237
- type: 'object',
238
- properties: [{
239
- key: 'b',
240
- type: 'string'
241
- }]
242
- }]);
243
-
244
- expect(nullError).to.be.instanceof(EjvError);
245
-
246
- if (!nullError) {
247
- throw new Error('spec failed');
248
- }
249
-
250
- expect(nullError.type).to.be.eql(ERROR_TYPE.REQUIRED);
251
- expect(nullError.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.REQUIRED));
252
- expect(nullError.path).to.be.eql('a');
253
-
254
- const data = {
255
- a: {
256
- b: 1
257
- }
258
- };
259
-
260
- const error: EjvError | null = ejv(data, [{
261
- key: 'a',
262
- type: 'object',
263
- properties: [{
264
- key: 'b',
265
- type: 'string'
266
- }]
267
- }]);
268
-
269
- expect(error).to.be.instanceof(EjvError);
270
-
271
- if (!error) {
272
- throw new Error('spec failed');
273
- }
274
-
275
- expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH);
276
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
277
- placeholders: ['string']
278
- }));
279
- expect(error.path).to.be.eql('a/b');
280
- expect(error.data).to.be.deep.equal(data);
281
- expect(error.errorData).to.be.eql(1);
282
-
283
- expect(ejv({
284
- a: {
285
- b: 1
286
- }
287
- }, [{
288
- key: 'a',
289
- type: 'object',
290
- properties: [{
291
- key: 'b',
292
- type: 'number'
293
- }]
294
- }])).to.be.null;
295
- });
296
-
297
- it('with multiple types', () => {
298
- const typeArr: string[] = ['string', 'boolean'];
299
-
300
- const undefinedError: EjvError | null = ejv({
301
- a: {
302
- b: undefined
303
- }
304
- }, [{
305
- key: 'a',
306
- type: 'object',
307
- properties: [{
308
- key: 'b',
309
- type: typeArr
310
- }]
311
- }]);
312
-
313
- expect(undefinedError).to.be.instanceof(EjvError);
314
-
315
- if (!undefinedError) {
316
- throw new Error('spec failed');
317
- }
318
-
319
- expect(undefinedError.type).to.be.eql(ERROR_TYPE.REQUIRED);
320
- expect(undefinedError.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.REQUIRED));
321
- expect(undefinedError.path).to.be.eql('a/b');
322
-
323
- const data = {
324
- a: {
325
- b: 1
326
- }
327
- };
328
-
329
- const error: EjvError | null = ejv(data, [{
330
- key: 'a',
331
- type: 'object',
332
- properties: [{
333
- key: 'b',
334
- type: typeArr
335
- }]
336
- }]);
337
-
338
- expect(error).to.be.instanceof(EjvError);
339
-
340
- if (!error) {
341
- throw new Error('spec failed');
342
- }
343
-
344
- expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH_ONE_OF);
345
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
346
- placeholders: [JSON.stringify(typeArr)]
347
- }));
348
- expect(error.path).to.be.eql('a/b');
349
- expect(error.data).to.be.deep.equal(data);
350
- expect(error.errorData).to.be.eql(1);
351
-
352
- expect(ejv({
353
- a: {
354
- b: 1
355
- }
356
- }, [{
357
- key: 'a',
358
- type: 'object',
359
- properties: [{
360
- key: 'b',
361
- type: ['number', 'string']
362
- }]
363
- }])).to.be.null;
364
-
365
- expect(ejv({
366
- a: {
367
- b: 1
368
- }
369
- }, [{
370
- key: 'a',
371
- type: 'object',
372
- properties: [{
373
- key: 'b',
374
- type: ['string', 'number']
375
- }]
376
- }])).to.be.null;
377
- });
378
- });
379
-
380
- describe('allowNoProperty', () => {
381
- describe('check parameter', () => {
382
- const data = {
383
- a: {
384
- b: 1
385
- }
386
- };
387
- it('undefined is ok', () => {
388
- expect(ejv(data, [{
389
- key: 'a',
390
- type: 'object',
391
- allowNoProperty: undefined
392
- }])).to.be.null;
393
- });
394
-
395
- it('null', () => {
396
- const errorScheme: Scheme = {
397
- key: 'a',
398
- type: 'object',
399
- allowNoProperty: null as unknown as boolean
400
- };
401
-
402
- checkSchemeError({
403
- data,
404
- errorScheme,
405
- message: createErrorMsg(ERROR_MESSAGE.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN)
406
- });
407
- });
408
- });
409
-
410
- describe('default', () => {
411
- it('empty object', () => {
412
- const error: EjvError | null = ejv({
413
- a: {}
414
- }, [{
415
- key: 'a',
416
- type: 'object'
417
- }]);
418
-
419
- expect(error).to.be.null;
420
- });
421
-
422
- it('object has property', () => {
423
- const error: EjvError | null = ejv({
424
- a: {
425
- b: 1
426
- }
427
- }, [{
428
- key: 'a',
429
- type: 'object'
430
- }]);
431
-
432
- expect(error).to.be.null;
433
- });
434
- });
435
-
436
- describe('allowNoProperty === false', () => {
437
- it('empty object', () => {
438
- const error: EjvError | null = ejv({
439
- a: {}
440
- }, [{
441
- key: 'a',
442
- type: 'object',
443
- allowNoProperty: false
444
- }]);
445
-
446
- expect(error).to.be.instanceof(EjvError);
447
-
448
- if (!error) {
449
- throw new Error('spec failed');
450
- }
451
-
452
- expect(error.type).to.be.eql(ERROR_TYPE.PROPERTY);
453
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PROPERTY));
454
- expect(error.path).to.be.eql('a');
455
- expect(error.data).to.be.a('object');
456
- });
457
-
458
- it('object has property', () => {
459
- expect(ejv({
460
- a: {
461
- b: 1
462
- }
463
- }, [{
464
- key: 'a',
465
- type: 'object',
466
- allowNoProperty: false
467
- }])).to.be.null;
468
- });
469
- });
470
-
471
- describe('allowNoProperty === true', () => {
472
- it('empty object', () => {
473
- const error: EjvError | null = ejv({
474
- a: {}
475
- }, [{
476
- key: 'a',
477
- type: 'object',
478
- allowNoProperty: true
479
- }]);
480
-
481
- expect(error).to.be.null;
482
- });
483
-
484
- it('object has property', () => {
485
- const error: EjvError | null = ejv({
486
- a: {
487
- b: 1
488
- }
489
- }, [{
490
- key: 'a',
491
- type: 'object',
492
- allowNoProperty: true
493
- }]);
494
-
495
- expect(error).to.be.null;
496
- });
497
- });
498
- });
499
- });
1
+ import { describe, it } from 'mocha';
2
+ import { expect } from 'chai';
3
+
4
+ import { ejv } from '../src/ejv';
5
+
6
+ import { EjvError, Scheme } from '../src/interfaces';
7
+ import { ERROR_MESSAGE, ERROR_TYPE } from '../src/constants';
8
+ import { createErrorMsg } from '../src/util';
9
+ import { checkSchemeError, TypeTester, typeTesterArr } from './common-test-util';
10
+
11
+
12
+ describe('ObjectScheme', () => {
13
+ describe('type', () => {
14
+ describe('mismatch', () => {
15
+ typeTesterArr
16
+ .filter((obj: TypeTester): boolean => {
17
+ return !['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
18
+ })
19
+ .forEach((obj: TypeTester): void => {
20
+ const data = {
21
+ a: obj.value
22
+ };
23
+
24
+ it(obj.type, () => {
25
+ const error: EjvError | null = ejv(data, [{
26
+ key: 'a',
27
+ type: 'object'
28
+ }]);
29
+
30
+ expect(error).to.be.instanceof(EjvError);
31
+
32
+ if (!error) {
33
+ throw new Error('spec failed');
34
+ }
35
+
36
+ expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH);
37
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
38
+ placeholders: ['object']
39
+ }));
40
+ expect(error.path).to.be.eql('a');
41
+ expect(error.data).to.be.deep.equal(data);
42
+ expect(error.errorData).to.be.eql(obj.value);
43
+ });
44
+ });
45
+
46
+ it('multiple types', () => {
47
+ const value = {};
48
+ const typeArr: string[] = ['boolean', 'number'];
49
+
50
+ const data = {
51
+ a: value
52
+ };
53
+
54
+ const error: EjvError | null = ejv(data, [{
55
+ key: 'a',
56
+ type: typeArr
57
+ }]);
58
+
59
+ expect(error).to.be.instanceof(EjvError);
60
+
61
+ if (!error) {
62
+ throw new Error('spec failed');
63
+ }
64
+
65
+ expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH_ONE_OF);
66
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
67
+ placeholders: [JSON.stringify(typeArr)]
68
+ }));
69
+ expect(error.path).to.be.eql('a');
70
+ expect(error.data).to.be.deep.equal(data);
71
+ expect(error.errorData).to.be.eql(value);
72
+ });
73
+ });
74
+
75
+ describe('match', () => {
76
+ it('optional', () => {
77
+ expect(ejv({
78
+ a: undefined
79
+ }, [{
80
+ key: 'a',
81
+ type: 'object',
82
+ optional: true
83
+ }])).to.be.null;
84
+ });
85
+
86
+ typeTesterArr
87
+ .filter((obj: TypeTester): boolean => {
88
+ return ['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
89
+ })
90
+ .forEach((obj: TypeTester): void => {
91
+ it(obj.type, () => {
92
+ expect(ejv({
93
+ a: obj.value
94
+ }, [{
95
+ key: 'a',
96
+ type: 'object'
97
+ }])).to.be.null;
98
+ });
99
+ });
100
+
101
+ it('single type', () => {
102
+ expect(ejv({
103
+ a: {
104
+ b: 1
105
+ }
106
+ }, [{
107
+ key: 'a',
108
+ type: 'object'
109
+ }])).to.be.null;
110
+ });
111
+
112
+ it('multiple types', () => {
113
+ expect(ejv({
114
+ a: {
115
+ b: 1
116
+ }
117
+ }, [{
118
+ key: 'a',
119
+ type: ['object', 'number']
120
+ }])).to.be.null;
121
+ });
122
+
123
+ it('multiple types', () => {
124
+ expect(ejv({
125
+ a: {
126
+ b: 1
127
+ }
128
+ }, [{
129
+ key: 'a',
130
+ type: ['number', 'object']
131
+ }])).to.be.null;
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('properties', () => {
137
+ describe('check parameter', () => {
138
+ const data = {
139
+ a: {
140
+ b: 1
141
+ }
142
+ };
143
+
144
+ it('undefined is ok', () => {
145
+ expect(ejv(data, [{
146
+ key: 'a',
147
+ type: 'object',
148
+ properties: undefined
149
+ }])).to.be.null;
150
+ });
151
+
152
+ it('null', () => {
153
+ const errorScheme: Scheme = {
154
+ key: 'a',
155
+ type: 'object',
156
+ properties: null as unknown as Scheme[]
157
+ };
158
+
159
+ checkSchemeError({
160
+ data,
161
+ errorScheme,
162
+ message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY)
163
+ });
164
+ });
165
+
166
+ it('not array', () => {
167
+ const errorScheme: Scheme = {
168
+ key: 'a',
169
+ type: 'object',
170
+ properties: 'b' as unknown as Scheme[]
171
+ };
172
+
173
+ checkSchemeError({
174
+ data,
175
+ errorScheme,
176
+ message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY)
177
+ });
178
+ });
179
+
180
+ it('empty array', () => {
181
+ const errorScheme: Scheme = {
182
+ key: 'a',
183
+ type: 'object',
184
+ properties: []
185
+ };
186
+
187
+ checkSchemeError({
188
+ data,
189
+ errorScheme,
190
+ message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_HAVE_ITEMS)
191
+ });
192
+ });
193
+
194
+ it('not object array', () => {
195
+ const errorScheme: Scheme = {
196
+ key: 'a',
197
+ type: 'object',
198
+ properties: ['b'] as unknown as Scheme[]
199
+ };
200
+
201
+ checkSchemeError({
202
+ data,
203
+ errorScheme,
204
+ message: createErrorMsg(ERROR_MESSAGE.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT)
205
+ });
206
+ });
207
+ });
208
+
209
+ it('with single type', () => {
210
+ const undefinedError: EjvError | null = ejv({
211
+ a: {
212
+ b: undefined
213
+ }
214
+ }, [{
215
+ key: 'a',
216
+ type: 'object',
217
+ properties: [{
218
+ key: 'b',
219
+ type: 'string'
220
+ }]
221
+ }]);
222
+
223
+ expect(undefinedError).to.be.instanceof(EjvError);
224
+
225
+ if (!undefinedError) {
226
+ throw new Error('spec failed');
227
+ }
228
+
229
+ expect(undefinedError.type).to.be.eql(ERROR_TYPE.REQUIRED);
230
+ expect(undefinedError.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.REQUIRED));
231
+ expect(undefinedError.path).to.be.eql('a/b');
232
+
233
+ const nullError: EjvError | null = ejv({
234
+ a: null
235
+ }, [{
236
+ key: 'a',
237
+ type: 'object',
238
+ properties: [{
239
+ key: 'b',
240
+ type: 'string'
241
+ }]
242
+ }]);
243
+
244
+ expect(nullError).to.be.instanceof(EjvError);
245
+
246
+ if (!nullError) {
247
+ throw new Error('spec failed');
248
+ }
249
+
250
+ expect(nullError.type).to.be.eql(ERROR_TYPE.REQUIRED);
251
+ expect(nullError.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.REQUIRED));
252
+ expect(nullError.path).to.be.eql('a');
253
+
254
+ const data = {
255
+ a: {
256
+ b: 1
257
+ }
258
+ };
259
+
260
+ const error: EjvError | null = ejv(data, [{
261
+ key: 'a',
262
+ type: 'object',
263
+ properties: [{
264
+ key: 'b',
265
+ type: 'string'
266
+ }]
267
+ }]);
268
+
269
+ expect(error).to.be.instanceof(EjvError);
270
+
271
+ if (!error) {
272
+ throw new Error('spec failed');
273
+ }
274
+
275
+ expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH);
276
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
277
+ placeholders: ['string']
278
+ }));
279
+ expect(error.path).to.be.eql('a/b');
280
+ expect(error.data).to.be.deep.equal(data);
281
+ expect(error.errorData).to.be.eql(1);
282
+
283
+ expect(ejv({
284
+ a: {
285
+ b: 1
286
+ }
287
+ }, [{
288
+ key: 'a',
289
+ type: 'object',
290
+ properties: [{
291
+ key: 'b',
292
+ type: 'number'
293
+ }]
294
+ }])).to.be.null;
295
+ });
296
+
297
+ it('with multiple types', () => {
298
+ const typeArr: string[] = ['string', 'boolean'];
299
+
300
+ const undefinedError: EjvError | null = ejv({
301
+ a: {
302
+ b: undefined
303
+ }
304
+ }, [{
305
+ key: 'a',
306
+ type: 'object',
307
+ properties: [{
308
+ key: 'b',
309
+ type: typeArr
310
+ }]
311
+ }]);
312
+
313
+ expect(undefinedError).to.be.instanceof(EjvError);
314
+
315
+ if (!undefinedError) {
316
+ throw new Error('spec failed');
317
+ }
318
+
319
+ expect(undefinedError.type).to.be.eql(ERROR_TYPE.REQUIRED);
320
+ expect(undefinedError.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.REQUIRED));
321
+ expect(undefinedError.path).to.be.eql('a/b');
322
+
323
+ const data = {
324
+ a: {
325
+ b: 1
326
+ }
327
+ };
328
+
329
+ const error: EjvError | null = ejv(data, [{
330
+ key: 'a',
331
+ type: 'object',
332
+ properties: [{
333
+ key: 'b',
334
+ type: typeArr
335
+ }]
336
+ }]);
337
+
338
+ expect(error).to.be.instanceof(EjvError);
339
+
340
+ if (!error) {
341
+ throw new Error('spec failed');
342
+ }
343
+
344
+ expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH_ONE_OF);
345
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
346
+ placeholders: [JSON.stringify(typeArr)]
347
+ }));
348
+ expect(error.path).to.be.eql('a/b');
349
+ expect(error.data).to.be.deep.equal(data);
350
+ expect(error.errorData).to.be.eql(1);
351
+
352
+ expect(ejv({
353
+ a: {
354
+ b: 1
355
+ }
356
+ }, [{
357
+ key: 'a',
358
+ type: 'object',
359
+ properties: [{
360
+ key: 'b',
361
+ type: ['number', 'string']
362
+ }]
363
+ }])).to.be.null;
364
+
365
+ expect(ejv({
366
+ a: {
367
+ b: 1
368
+ }
369
+ }, [{
370
+ key: 'a',
371
+ type: 'object',
372
+ properties: [{
373
+ key: 'b',
374
+ type: ['string', 'number']
375
+ }]
376
+ }])).to.be.null;
377
+ });
378
+ });
379
+
380
+ describe('allowNoProperty', () => {
381
+ describe('check parameter', () => {
382
+ const data = {
383
+ a: {
384
+ b: 1
385
+ }
386
+ };
387
+ it('undefined is ok', () => {
388
+ expect(ejv(data, [{
389
+ key: 'a',
390
+ type: 'object',
391
+ allowNoProperty: undefined
392
+ }])).to.be.null;
393
+ });
394
+
395
+ it('null', () => {
396
+ const errorScheme: Scheme = {
397
+ key: 'a',
398
+ type: 'object',
399
+ allowNoProperty: null as unknown as boolean
400
+ };
401
+
402
+ checkSchemeError({
403
+ data,
404
+ errorScheme,
405
+ message: createErrorMsg(ERROR_MESSAGE.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN)
406
+ });
407
+ });
408
+ });
409
+
410
+ describe('default', () => {
411
+ it('empty object', () => {
412
+ const error: EjvError | null = ejv({
413
+ a: {}
414
+ }, [{
415
+ key: 'a',
416
+ type: 'object'
417
+ }]);
418
+
419
+ expect(error).to.be.null;
420
+ });
421
+
422
+ it('object has property', () => {
423
+ const error: EjvError | null = ejv({
424
+ a: {
425
+ b: 1
426
+ }
427
+ }, [{
428
+ key: 'a',
429
+ type: 'object'
430
+ }]);
431
+
432
+ expect(error).to.be.null;
433
+ });
434
+ });
435
+
436
+ describe('allowNoProperty === false', () => {
437
+ it('empty object', () => {
438
+ const error: EjvError | null = ejv({
439
+ a: {}
440
+ }, [{
441
+ key: 'a',
442
+ type: 'object',
443
+ allowNoProperty: false
444
+ }]);
445
+
446
+ expect(error).to.be.instanceof(EjvError);
447
+
448
+ if (!error) {
449
+ throw new Error('spec failed');
450
+ }
451
+
452
+ expect(error.type).to.be.eql(ERROR_TYPE.PROPERTY);
453
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PROPERTY));
454
+ expect(error.path).to.be.eql('a');
455
+ expect(error.data).to.be.a('object');
456
+ });
457
+
458
+ it('object has property', () => {
459
+ expect(ejv({
460
+ a: {
461
+ b: 1
462
+ }
463
+ }, [{
464
+ key: 'a',
465
+ type: 'object',
466
+ allowNoProperty: false
467
+ }])).to.be.null;
468
+ });
469
+ });
470
+
471
+ describe('allowNoProperty === true', () => {
472
+ it('empty object', () => {
473
+ const error: EjvError | null = ejv({
474
+ a: {}
475
+ }, [{
476
+ key: 'a',
477
+ type: 'object',
478
+ allowNoProperty: true
479
+ }]);
480
+
481
+ expect(error).to.be.null;
482
+ });
483
+
484
+ it('object has property', () => {
485
+ const error: EjvError | null = ejv({
486
+ a: {
487
+ b: 1
488
+ }
489
+ }, [{
490
+ key: 'a',
491
+ type: 'object',
492
+ allowNoProperty: true
493
+ }]);
494
+
495
+ expect(error).to.be.null;
496
+ });
497
+ });
498
+ });
499
+ });