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.
Files changed (75) hide show
  1. package/.eslintrc.json +88 -0
  2. package/.mocharc.json +8 -0
  3. package/CHANGELOG.md +109 -68
  4. package/LICENSE +21 -21
  5. package/README-KR.md +596 -592
  6. package/README.md +599 -595
  7. package/build/{constants.js → cjs/constants.js} +45 -46
  8. package/build/cjs/constants.js.map +1 -0
  9. package/build/cjs/ejv.js +1263 -0
  10. package/build/cjs/ejv.js.map +1 -0
  11. package/build/{public_api.js → cjs/index.js} +4 -4
  12. package/build/cjs/index.js.map +1 -0
  13. package/build/cjs/interfaces.js +29 -0
  14. package/build/cjs/interfaces.js.map +1 -0
  15. package/build/cjs/package.json +1 -0
  16. package/build/{tester.js → cjs/tester.js} +88 -83
  17. package/build/cjs/tester.js.map +1 -0
  18. package/build/cjs/util.js +103 -0
  19. package/build/cjs/util.js.map +1 -0
  20. package/build/constants.d.ts +37 -40
  21. package/build/esm/constants.js +115 -0
  22. package/build/esm/constants.js.map +1 -0
  23. package/build/esm/ejv.js +1261 -0
  24. package/build/esm/ejv.js.map +1 -0
  25. package/build/esm/index.js +4 -0
  26. package/build/esm/index.js.map +1 -0
  27. package/build/esm/interfaces.js +33 -0
  28. package/build/esm/interfaces.js.map +1 -0
  29. package/build/esm/package.json +1 -0
  30. package/build/esm/tester.js +240 -0
  31. package/build/esm/tester.js.map +1 -0
  32. package/build/esm/util.js +96 -0
  33. package/build/esm/util.js.map +1 -0
  34. package/build/index.d.ts +3 -0
  35. package/build/interfaces.d.ts +51 -13
  36. package/build/scripts/add-js-extensions.js +46 -0
  37. package/build/scripts/add-js-extensions.js.map +1 -0
  38. package/build/tester.d.ts +16 -17
  39. package/build/util.d.ts +7 -1
  40. package/package.json +50 -39
  41. package/scripts/add-js-extensions.ts +59 -0
  42. package/spec/ArrayScheme.ts +1021 -0
  43. package/spec/CommonScheme.ts +251 -0
  44. package/spec/DateScheme.ts +472 -0
  45. package/spec/NumberScheme.ts +1032 -0
  46. package/spec/ObjectScheme.ts +499 -0
  47. package/spec/RegExpScheme.ts +112 -0
  48. package/spec/StringScheme.ts +1239 -0
  49. package/spec/common-test-util.ts +63 -0
  50. package/spec/ejv.spec.ts +209 -4634
  51. package/spec/testers.spec.ts +833 -832
  52. package/src/constants.ts +155 -156
  53. package/src/ejv.ts +1648 -1071
  54. package/src/index.ts +14 -0
  55. package/src/interfaces.ts +145 -63
  56. package/src/tester.ts +308 -302
  57. package/src/util.ts +124 -59
  58. package/tsconfig.cjs.json +8 -0
  59. package/tsconfig.esm.json +7 -0
  60. package/tsconfig.json +22 -19
  61. package/tsconfig.scripts.json +14 -0
  62. package/tsconfig.types.json +9 -0
  63. package/build/constants.js.map +0 -1
  64. package/build/ejv.js +0 -687
  65. package/build/ejv.js.map +0 -1
  66. package/build/interfaces.js +0 -15
  67. package/build/interfaces.js.map +0 -1
  68. package/build/public_api.d.ts +0 -3
  69. package/build/public_api.js.map +0 -1
  70. package/build/tester.js.map +0 -1
  71. package/build/util.js +0 -68
  72. package/build/util.js.map +0 -1
  73. package/spec/common-test-runner.ts +0 -17
  74. package/src/public_api.ts +0 -3
  75. package/tsconfig.spec.json +0 -19
package/spec/ejv.spec.ts CHANGED
@@ -1,4634 +1,209 @@
1
- import { expect } from 'chai';
2
-
3
- import { ejv } from '../src/ejv';
4
- import { ErrorMsg, ErrorMsgCursorA, ErrorType } from '../src/constants';
5
- import { EjvError, Scheme } from '../src/interfaces';
6
-
7
- const typeTester : {
8
- type : string,
9
- value : any
10
- }[] = [
11
- { type : 'boolean', value : true },
12
- { type : 'number', value : 123 },
13
- { type : 'string', value : 'ejv' },
14
- { type : 'object', value : {} },
15
- { type : 'date', value : new Date },
16
- { type : 'regexp', value : new RegExp('ejv') },
17
- { type : 'array', value : [1, 2, 3] }
18
- ];
19
-
20
- describe('ejv()', () => {
21
- describe('ejv() itself', () => {
22
- describe('data', () => {
23
- it('no data', () => {
24
- const error : EjvError = ejv(undefined, undefined);
25
-
26
- expect(error).to.be.instanceof(EjvError);
27
- expect(error).to.have.property('type', ErrorType.REQUIRED);
28
- expect(error).to.have.property('message', ErrorMsg.NO_DATA);
29
- expect(error).to.have.property('path', '/');
30
- });
31
-
32
- it('null data', () => {
33
- const error : EjvError = ejv(null, undefined);
34
-
35
- expect(error).to.be.instanceof(EjvError);
36
- expect(error).to.have.property('type', ErrorType.REQUIRED);
37
- expect(error).to.have.property('message', ErrorMsg.NO_DATA);
38
- expect(error).to.have.property('path', '/');
39
- });
40
- });
41
-
42
- describe('scheme', () => {
43
- it('no scheme', () => {
44
- expect(() => ejv({
45
- a : 'hello'
46
- }, undefined)).to.throw(ErrorMsg.NO_SCHEME);
47
- });
48
-
49
- it('null scheme', () => {
50
- expect(() => ejv({
51
- a : 'hello'
52
- }, null)).to.throw(ErrorMsg.NO_SCHEME);
53
- });
54
-
55
- it('empty scheme array', () => {
56
- expect(() => ejv({
57
- a : 'hello'
58
- }, [])).to.throw(ErrorMsg.EMPTY_SCHEME);
59
- });
60
-
61
- it('invalid scheme object', () => {
62
- expect(() => ejv({
63
- a : 'hello'
64
- }, ['string' as any as Scheme])).to.throw(ErrorMsg.NO_OBJECT_ARRAY_SCHEME);
65
- });
66
-
67
- it('no type', () => {
68
- expect(() => ejv({
69
- a : 'hello'
70
- }, [{
71
- key : 'a'
72
- } as any as Scheme])).to.throw(ErrorMsg.SCHEMES_SHOULD_HAVE_TYPE);
73
- });
74
-
75
- it('invalid type', () => {
76
- expect(() => ejv({
77
- a : 'hello'
78
- }, [{
79
- key : 'a',
80
- type : 'invalidType'
81
- }])).to.throw(ErrorMsg.SCHEMES_HAS_INVALID_TYPE.replace(ErrorMsgCursorA, 'invalidType'));
82
-
83
- expect(() => ejv({
84
- a : 'hello'
85
- }, [{
86
- key : 'a',
87
- type : ['string', 'invalidType']
88
- }])).to.throw(ErrorMsg.SCHEMES_HAS_INVALID_TYPE.replace(ErrorMsgCursorA, 'invalidType'));
89
- });
90
-
91
- it('duplicated type', () => {
92
- expect(() => ejv({
93
- a : 'hello'
94
- }, [{
95
- key : 'a',
96
- type : ['string', 'string']
97
- }])).to.throw(ErrorMsg.SCHEMES_HAS_DUPLICATED_TYPE);
98
- });
99
- });
100
-
101
- describe('options', () => {
102
- describe('customErrorMsg', () => {
103
- it('override required error', () => {
104
- const customErrorMsg : string = 'property \'a\' required';
105
-
106
- const error : EjvError = ejv({
107
- // empty
108
- }, [{
109
- key : 'a',
110
- type : 'number'
111
- }], {
112
- customErrorMsg : {
113
- [ErrorType.REQUIRED] : customErrorMsg
114
- }
115
- });
116
-
117
- expect(error).to.be.instanceof(EjvError);
118
- expect(error.type).to.be.eql(ErrorType.REQUIRED);
119
- expect(error.message).to.be.eql(customErrorMsg);
120
- });
121
-
122
- it('override type matching error', () => {
123
- const customErrorMsg : string = 'property \'a\' should be a number';
124
-
125
- const error : EjvError = ejv({
126
- a : 'a'
127
- }, [{
128
- key : 'a',
129
- type : 'number'
130
- }], {
131
- customErrorMsg : {
132
- [ErrorType.TYPE_MISMATCH] : customErrorMsg
133
- }
134
- });
135
-
136
- expect(error).to.be.instanceof(EjvError);
137
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
138
- expect(error.message).to.be.eql(customErrorMsg);
139
- });
140
- });
141
- });
142
- });
143
-
144
- describe('common', () => {
145
- describe('optional', () => {
146
- describe('default', () => {
147
- it('undefined value', () => {
148
- const data = {
149
- a : undefined
150
- };
151
-
152
- const error : EjvError = ejv(data, [{
153
- key : 'a',
154
- type : 'string'
155
- }]);
156
-
157
- expect(error).to.be.instanceof(EjvError);
158
- expect(error.type).to.be.eql(ErrorType.REQUIRED);
159
- expect(error.message).to.be.eql(ErrorMsg.REQUIRED);
160
- expect(error.path).to.be.eql('a');
161
- expect(error.data).to.be.deep.equal(data);
162
- expect(error.errorData).to.be.undefined;
163
- });
164
-
165
- it('correct type', () => {
166
- const error : EjvError = ejv({
167
- a : 'abc'
168
- }, [{
169
- key : 'a',
170
- type : 'string'
171
- }]);
172
-
173
- expect(error).to.be.null;
174
- });
175
-
176
- it('incorrect type', () => {
177
- const data = {
178
- a : 123
179
- };
180
-
181
- const error : EjvError = ejv(data, [{
182
- key : 'a',
183
- type : 'string'
184
- }]);
185
-
186
- expect(error).to.be.instanceof(EjvError);
187
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
188
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
189
- .replace(ErrorMsgCursorA, 'string'));
190
- expect(error.path).to.be.eql('a');
191
- expect(error.data).to.be.deep.equal(data);
192
- expect(error.errorData).to.be.eql(123);
193
- });
194
- });
195
-
196
- describe('optional === false', () => {
197
- it('undefined value', () => {
198
- const data = {
199
- a : undefined
200
- };
201
-
202
- const error : EjvError = ejv(data, [{
203
- key : 'a',
204
- type : 'string',
205
- optional : false
206
- }]);
207
-
208
- expect(error).to.be.instanceof(EjvError);
209
- expect(error.type).to.be.eql(ErrorType.REQUIRED);
210
- expect(error.message).to.be.eql(ErrorMsg.REQUIRED);
211
- expect(error.path).to.be.eql('a');
212
- expect(error.data).to.be.deep.equal(data);
213
- expect(error.errorData).to.be.undefined;
214
- });
215
-
216
- it('correct type', () => {
217
- const error : EjvError = ejv({
218
- a : 'abc'
219
- }, [{
220
- key : 'a',
221
- type : 'string',
222
- optional : false
223
- }]);
224
-
225
- expect(error).to.be.null;
226
- });
227
-
228
- it('incorrect type', () => {
229
- const data = {
230
- a : 123
231
- };
232
-
233
- const error : EjvError = ejv(data, [{
234
- key : 'a',
235
- type : 'string',
236
- optional : false
237
- }]);
238
-
239
- expect(error).to.be.instanceof(EjvError);
240
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
241
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
242
- .replace(ErrorMsgCursorA, 'string'));
243
- expect(error.path).to.be.eql('a');
244
- expect(error.data).to.be.deep.equal(data);
245
- expect(error.errorData).to.be.eql(123);
246
- });
247
- });
248
-
249
- describe('optional === true', () => {
250
- it('undefined value', () => {
251
- const error : EjvError = ejv({
252
- a : undefined
253
- }, [{
254
- key : 'a',
255
- type : 'string',
256
- optional : true
257
- }]);
258
-
259
- expect(error).to.be.null;
260
- });
261
-
262
- it('correct type', () => {
263
- const error : EjvError = ejv({
264
- a : 'abc'
265
- }, [{
266
- key : 'a',
267
- type : 'string',
268
- optional : true
269
- }]);
270
-
271
- expect(error).to.be.null;
272
- });
273
-
274
- it('incorrect type', () => {
275
- const data = {
276
- a : 123
277
- };
278
-
279
- const error : EjvError = ejv(data, [{
280
- key : 'a',
281
- type : 'string',
282
- optional : true
283
- }]);
284
-
285
- expect(error).to.be.instanceof(EjvError);
286
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
287
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
288
- .replace(ErrorMsgCursorA, 'string'));
289
- expect(error.path).to.be.eql('a');
290
- expect(error.data).to.be.deep.equal(data);
291
- expect(error.errorData).to.be.eql(123);
292
- });
293
- });
294
- });
295
-
296
- describe('nullable', () => {
297
- it('default', () => {
298
- const data = {
299
- a : null
300
- };
301
-
302
- const error : EjvError = ejv(data, [{
303
- key : 'a',
304
- type : 'string'
305
- }]);
306
-
307
- expect(error).to.be.instanceof(EjvError);
308
- expect(error.type).to.be.eql(ErrorType.REQUIRED);
309
- expect(error.message).to.be.eql(ErrorMsg.REQUIRED);
310
- expect(error.path).to.be.eql('a');
311
- expect(error.data).to.be.deep.equal(data);
312
- expect(error.errorData).to.be.null;
313
- });
314
-
315
- it('nullable === false', () => {
316
- const data = {
317
- a : null
318
- };
319
-
320
- const error : EjvError = ejv(data, [{
321
- key : 'a',
322
- type : 'string',
323
- nullable : false
324
- }]);
325
-
326
- expect(error).to.be.instanceof(EjvError);
327
- expect(error.type).to.be.eql(ErrorType.REQUIRED);
328
- expect(error.message).to.be.eql(ErrorMsg.REQUIRED);
329
- expect(error.path).to.be.eql('a');
330
- expect(error.data).to.be.deep.equal(data);
331
- expect(error.errorData).to.be.null;
332
- });
333
-
334
- it('nullable === true', () => {
335
- const error : EjvError = ejv({
336
- a : null
337
- }, [{
338
- key : 'a',
339
- type : 'string',
340
- nullable : true
341
- }]);
342
-
343
- expect(error).to.be.null;
344
- });
345
- });
346
- });
347
-
348
- describe('number', () => {
349
- describe('type', () => {
350
- describe('mismatch', () => {
351
- typeTester.filter(obj => obj.type !== 'number')
352
- .forEach((obj) => {
353
- const data = {
354
- a : obj.value
355
- };
356
-
357
- it(obj.type, () => {
358
- const error : EjvError = ejv(data, [{
359
- key : 'a',
360
- type : 'number'
361
- }]);
362
-
363
- expect(error).to.be.instanceof(EjvError);
364
-
365
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
366
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
367
- .replace(ErrorMsgCursorA, 'number')
368
- );
369
- expect(error.path).to.be.eql('a');
370
- expect(error.data).to.be.deep.equal(data);
371
- expect(error.errorData).to.be.eql(obj.value);
372
- });
373
- });
374
-
375
- it('multiple types', () => {
376
- const value = 123;
377
- const typeArr : string[] = ['boolean', 'string'];
378
-
379
- const data = {
380
- a : value
381
- };
382
-
383
- const error : EjvError = ejv(data, [{
384
- key : 'a',
385
- type : typeArr
386
- }]);
387
-
388
- expect(error).to.be.instanceof(EjvError);
389
-
390
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
391
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
392
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
393
- expect(error.path).to.be.eql('a');
394
- expect(error.data).to.be.deep.equal(data);
395
- expect(error.errorData).to.be.eql(value);
396
- });
397
- });
398
-
399
- describe('match', () => {
400
- it('optional', () => {
401
- expect(ejv({
402
- a : undefined
403
- }, [{
404
- key : 'a',
405
- type : 'number',
406
- optional : true
407
- }])).to.be.null;
408
- });
409
-
410
- it('single type', () => {
411
- expect(ejv({
412
- a : 123
413
- }, [{
414
- key : 'a',
415
- type : 'number'
416
- }])).to.be.null;
417
- });
418
-
419
- it('multiple types', () => {
420
- expect(ejv({
421
- a : 123
422
- }, [{
423
- key : 'a',
424
- type : ['number', 'string']
425
- }])).to.be.null;
426
- });
427
-
428
- it('multiple types', () => {
429
- expect(ejv({
430
- a : 123
431
- }, [{
432
- key : 'a',
433
- type : ['string', 'number']
434
- }])).to.be.null;
435
- });
436
- });
437
- });
438
-
439
- describe('enum', () => {
440
- describe('check parameter', () => {
441
- it('undefined is ok', () => {
442
- expect(ejv({
443
- a : 1
444
- }, [{
445
- key : 'a',
446
- type : 'number',
447
- enum : undefined
448
- }])).to.be.null;
449
- });
450
-
451
- it('null', () => {
452
- expect(() => ejv({
453
- a : 1
454
- }, [{
455
- key : 'a',
456
- type : 'number',
457
- enum : null
458
- }])).to.be.throw(ErrorMsg.ENUM_SHOULD_BE_ARRAY);
459
- });
460
-
461
- it('not array', () => {
462
- expect(() => ejv({
463
- a : 10
464
- }, [{
465
- key : 'a',
466
- type : 'number',
467
- enum : 1 as any
468
- }])).to.throw(ErrorMsg.ENUM_SHOULD_BE_ARRAY);
469
- });
470
-
471
- it('not number', () => {
472
- expect(() => ejv({
473
- a : 10
474
- }, [{
475
- key : 'a',
476
- type : 'number',
477
- enum : ['10']
478
- }])).to.throw(ErrorMsg.ENUM_SHOULD_BE_NUMBERS);
479
- });
480
- });
481
-
482
- it('fail', () => {
483
- const enumArr : number[] = [9, 11];
484
-
485
- const data = {
486
- a : 10
487
- };
488
-
489
- const error : EjvError = ejv(data, [{
490
- key : 'a',
491
- type : 'number',
492
- enum : enumArr
493
- }]);
494
-
495
- expect(error).to.be.instanceof(EjvError);
496
- expect(error.type).to.be.eql(ErrorType.ONE_OF);
497
- expect(error.message).to.be.eql(ErrorMsg.ONE_OF
498
- .replace(ErrorMsgCursorA, JSON.stringify(enumArr))
499
- );
500
- expect(error.path).to.be.eql('a');
501
- expect(error.data).to.be.deep.equal(data);
502
- expect(error.errorData).to.be.eql(10);
503
- });
504
-
505
- it('ok', () => {
506
- expect(ejv({
507
- a : 10
508
- }, [{
509
- key : 'a',
510
- type : 'number',
511
- enum : [9, 10, 11]
512
- }])).to.be.null;
513
- });
514
- });
515
-
516
- describe('enumReverse', () => {
517
- describe('check parameter', () => {
518
- it('undefined is ok', () => {
519
- expect(ejv({
520
- a : 1
521
- }, [{
522
- key : 'a',
523
- type : 'number',
524
- enumReverse : undefined
525
- }])).to.be.null;
526
- });
527
-
528
- it('null', () => {
529
- expect(() => ejv({
530
- a : 1
531
- }, [{
532
- key : 'a',
533
- type : 'number',
534
- enumReverse : null
535
- }])).to.be.throw(ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
536
- });
537
-
538
- it('not array', () => {
539
- expect(() => ejv({
540
- a : 10
541
- }, [{
542
- key : 'a',
543
- type : 'number',
544
- enumReverse : 1 as any
545
- }])).to.throw(ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
546
- });
547
-
548
- it('not number', () => {
549
- expect(() => ejv({
550
- a : 10
551
- }, [{
552
- key : 'a',
553
- type : 'number',
554
- enumReverse : ['10']
555
- }])).to.throw(ErrorMsg.ENUM_REVERSE_SHOULD_BE_NUMBERS);
556
- });
557
- });
558
-
559
- it('fail', () => {
560
- const enumArr : number[] = [9, 10];
561
-
562
- const data = {
563
- a : 10
564
- };
565
-
566
- const error : EjvError = ejv(data, [{
567
- key : 'a',
568
- type : 'number',
569
- enumReverse : enumArr
570
- }]);
571
-
572
- expect(error).to.be.instanceof(EjvError);
573
- expect(error.type).to.be.eql(ErrorType.NOT_ONE_OF);
574
- expect(error.message).to.be.eql(ErrorMsg.NOT_ONE_OF
575
- .replace(ErrorMsgCursorA, JSON.stringify(enumArr))
576
- );
577
- expect(error.path).to.be.eql('a');
578
- expect(error.data).to.be.deep.equal(data);
579
- expect(error.errorData).to.be.eql(10);
580
- });
581
-
582
- it('ok', () => {
583
- expect(ejv({
584
- a : 10
585
- }, [{
586
- key : 'a',
587
- type : 'number',
588
- enumReverse : [9, 11]
589
- }])).to.be.null;
590
- });
591
- });
592
-
593
- describe('min & exclusiveMin', () => {
594
- describe('check parameter', () => {
595
- it('undefined is ok', () => {
596
- expect(ejv({
597
- a : 1
598
- }, [{
599
- key : 'a',
600
- type : 'number',
601
- min : undefined
602
- }])).to.be.null;
603
- });
604
-
605
- it('null', () => {
606
- expect(() => ejv({
607
- a : 3
608
- }, [{
609
- key : 'a',
610
- type : 'number',
611
- min : null
612
- }])).to.throw(ErrorMsg.MIN_SHOULD_BE_NUMBER);
613
- });
614
-
615
- it('min type', () => {
616
- expect(() => ejv({
617
- a : 3
618
- }, [{
619
- key : 'a',
620
- type : 'number',
621
- min : '3'
622
- }])).to.throw(ErrorMsg.MIN_SHOULD_BE_NUMBER);
623
- });
624
-
625
- it('exclusiveMin type', () => {
626
- expect(() => ejv({
627
- a : 3
628
- }, [{
629
- key : 'a',
630
- type : 'number',
631
- min : 3,
632
- exclusiveMin : '3' as any
633
- }])).to.throw(ErrorMsg.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN);
634
- });
635
- });
636
-
637
- it('without exclusiveMin', () => {
638
- expect(ejv({
639
- a : 1
640
- }, [{
641
- key : 'a',
642
- type : 'number',
643
- min : 1,
644
- exclusiveMin : undefined
645
- }])).to.be.null;
646
-
647
- const error1 : EjvError = ejv({
648
- a : 9
649
- }, [{
650
- key : 'a',
651
- type : 'number',
652
- min : 10
653
- }]);
654
-
655
- expect(error1).to.be.instanceof(EjvError);
656
- expect(error1.type).to.be.eql(ErrorType.GREATER_THAN_OR_EQUAL);
657
- expect(error1.message).to.be.eql(ErrorMsg.GREATER_THAN_OR_EQUAL
658
- .replace(ErrorMsgCursorA, '10')
659
- );
660
-
661
- expect(ejv({
662
- a : 10
663
- }, [{
664
- key : 'a',
665
- type : 'number',
666
- min : 10
667
- }])).to.be.null;
668
-
669
- expect(ejv({
670
- a : 11
671
- }, [{
672
- key : 'a',
673
- type : 'number',
674
- min : 10
675
- }])).to.be.null;
676
- });
677
-
678
- it('exclusiveMin === true', () => {
679
- const error1 : EjvError = ejv({
680
- a : 9
681
- }, [{
682
- key : 'a',
683
- type : 'number',
684
- min : 10,
685
- exclusiveMin : true
686
- }]);
687
-
688
- expect(error1).to.be.instanceof(EjvError);
689
- expect(error1.type).to.be.eql(ErrorType.GREATER_THAN);
690
- expect(error1.message).to.be.eql(ErrorMsg.GREATER_THAN
691
- .replace(ErrorMsgCursorA, '10')
692
- );
693
-
694
- const error2 : EjvError = ejv({
695
- a : 10
696
- }, [{
697
- key : 'a',
698
- type : 'number',
699
- min : 10,
700
- exclusiveMin : true
701
- }]);
702
-
703
- expect(error2).to.be.instanceof(EjvError);
704
- expect(error2.type).to.be.eql(ErrorType.GREATER_THAN);
705
- expect(error2.message).to.be.eql(ErrorMsg.GREATER_THAN
706
- .replace(ErrorMsgCursorA, '10')
707
- );
708
-
709
- expect(ejv({
710
- a : 11
711
- }, [{
712
- key : 'a',
713
- type : 'number',
714
- min : 10,
715
- exclusiveMin : true
716
- }])).to.be.null;
717
- });
718
-
719
- it('exclusiveMin === false', () => {
720
- const error1 : EjvError = ejv({
721
- a : 9
722
- }, [{
723
- key : 'a',
724
- type : 'number',
725
- min : 10,
726
- exclusiveMin : false
727
- }]);
728
-
729
- expect(error1).to.be.instanceof(EjvError);
730
- expect(error1.type).to.be.eql(ErrorType.GREATER_THAN_OR_EQUAL);
731
- expect(error1.message).to.be.eql(ErrorMsg.GREATER_THAN_OR_EQUAL
732
- .replace(ErrorMsgCursorA, '10')
733
- );
734
-
735
- expect(ejv({
736
- a : 10
737
- }, [{
738
- key : 'a',
739
- type : 'number',
740
- min : 10,
741
- exclusiveMin : false
742
- }])).to.be.null;
743
-
744
- expect(ejv({
745
- a : 11
746
- }, [{
747
- key : 'a',
748
- type : 'number',
749
- min : 10,
750
- exclusiveMin : false
751
- }])).to.be.null;
752
- });
753
- });
754
-
755
- describe('max & exclusiveMax', () => {
756
- describe('check parameter', () => {
757
- it('undefined is ok', () => {
758
- expect(ejv({
759
- a : 1
760
- }, [{
761
- key : 'a',
762
- type : 'number',
763
- max : undefined
764
- }])).to.be.null;
765
- });
766
-
767
- it('null', () => {
768
- expect(() => ejv({
769
- a : 3
770
- }, [{
771
- key : 'a',
772
- type : 'number',
773
- max : null
774
- }])).to.throw(ErrorMsg.MAX_SHOULD_BE_NUMBER);
775
- });
776
-
777
- it('max type', () => {
778
- expect(() => ejv({
779
- a : 3
780
- }, [{
781
- key : 'a',
782
- type : 'number',
783
- max : '3'
784
- }])).to.throw(ErrorMsg.MAX_SHOULD_BE_NUMBER);
785
- });
786
-
787
- it('exclusiveMax type', () => {
788
- expect(() => ejv({
789
- a : 3
790
- }, [{
791
- key : 'a',
792
- type : 'number',
793
- max : 3,
794
- exclusiveMax : '3' as any
795
- }])).to.throw(ErrorMsg.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN);
796
- });
797
- });
798
-
799
- it('without exclusiveMax', () => {
800
- expect(ejv({
801
- a : 1
802
- }, [{
803
- key : 'a',
804
- type : 'number',
805
- max : 1,
806
- exclusiveMax : undefined
807
- }])).to.be.null;
808
-
809
- expect(ejv({
810
- a : 9
811
- }, [{
812
- key : 'a',
813
- type : 'number',
814
- max : 10
815
- }])).to.be.null;
816
-
817
- expect(ejv({
818
- a : 10
819
- }, [{
820
- key : 'a',
821
- type : 'number',
822
- max : 10
823
- }])).to.be.null;
824
-
825
- const error1 : EjvError = ejv({
826
- a : 11
827
- }, [{
828
- key : 'a',
829
- type : 'number',
830
- max : 10
831
- }]);
832
-
833
- expect(error1).to.be.instanceof(EjvError);
834
- expect(error1.type).to.be.eql(ErrorType.SMALLER_THAN_OR_EQUAL);
835
- expect(error1.message).to.be.eql(ErrorMsg.SMALLER_THAN_OR_EQUAL
836
- .replace(ErrorMsgCursorA, '10')
837
- );
838
- });
839
-
840
- it('exclusiveMax === true', () => {
841
- expect(ejv({
842
- a : 9
843
- }, [{
844
- key : 'a',
845
- type : 'number',
846
- max : 10,
847
- exclusiveMax : true
848
- }])).to.be.null;
849
-
850
- const error1 : EjvError = ejv({
851
- a : 10
852
- }, [{
853
- key : 'a',
854
- type : 'number',
855
- max : 10,
856
- exclusiveMax : true
857
- }]);
858
-
859
- expect(error1).to.be.instanceof(EjvError);
860
- expect(error1.type).to.be.eql(ErrorType.SMALLER_THAN);
861
- expect(error1.message).to.be.eql(ErrorMsg.SMALLER_THAN
862
- .replace(ErrorMsgCursorA, '10')
863
- );
864
-
865
- const error2 : EjvError = ejv({
866
- a : 11
867
- }, [{
868
- key : 'a',
869
- type : 'number',
870
- max : 10,
871
- exclusiveMax : true
872
- }]);
873
-
874
- expect(error2).to.be.instanceof(EjvError);
875
- expect(error2.type).to.be.eql(ErrorType.SMALLER_THAN);
876
- expect(error2.message).to.be.eql(ErrorMsg.SMALLER_THAN
877
- .replace(ErrorMsgCursorA, '10')
878
- );
879
- });
880
-
881
- it('exclusiveMax === false', () => {
882
- expect(ejv({
883
- a : 9
884
- }, [{
885
- key : 'a',
886
- type : 'number',
887
- max : 10,
888
- exclusiveMax : false
889
- }])).to.be.null;
890
-
891
- expect(ejv({
892
- a : 10
893
- }, [{
894
- key : 'a',
895
- type : 'number',
896
- max : 10,
897
- exclusiveMax : false
898
- }])).to.be.null;
899
-
900
- const error1 : EjvError = ejv({
901
- a : 11
902
- }, [{
903
- key : 'a',
904
- type : 'number',
905
- max : 10,
906
- exclusiveMax : false
907
- }]);
908
-
909
- expect(error1).to.be.instanceof(EjvError);
910
- expect(error1.type).to.be.eql(ErrorType.SMALLER_THAN_OR_EQUAL);
911
- expect(error1.message).to.be.eql(ErrorMsg.SMALLER_THAN_OR_EQUAL
912
- .replace(ErrorMsgCursorA, '10')
913
- );
914
- });
915
- });
916
-
917
- describe('format', () => {
918
- describe('check parameter', () => {
919
- it('undefined is ok', () => {
920
- expect(ejv({
921
- a : 123.5
922
- }, [{
923
- key : 'a',
924
- type : 'number',
925
- format : undefined
926
- }])).to.be.null;
927
- });
928
-
929
- it('null', () => {
930
- expect(() => ejv({
931
- a : 123.5
932
- }, [{
933
- key : 'a',
934
- type : 'number',
935
- format : null
936
- }])).to.be.throw(ErrorMsg.INVALID_NUMBER_FORMAT
937
- .replace(ErrorMsgCursorA, 'null'));
938
- });
939
-
940
- describe('invalid number format', () => {
941
- it('single', () => {
942
- expect(() => ejv({
943
- a : 1
944
- }, [{
945
- key : 'a',
946
- type : 'number',
947
- format : 'invalidNumberFormat'
948
- }])).to.throw(ErrorMsg.INVALID_NUMBER_FORMAT
949
- .replace(ErrorMsgCursorA, 'invalidNumberFormat'));
950
- });
951
-
952
- it('multiple', () => {
953
- expect(() => ejv({
954
- a : 1
955
- }, [{
956
- key : 'a',
957
- type : 'number',
958
- format : ['index', 'invalidNumberFormat']
959
- }])).to.throw(ErrorMsg.INVALID_NUMBER_FORMAT
960
- .replace(ErrorMsgCursorA, 'invalidNumberFormat'));
961
- });
962
- });
963
- });
964
-
965
- describe('integer', () => {
966
- describe('single format', () => {
967
- it('fail', () => {
968
- const error : EjvError = ejv({
969
- a : 123.5
970
- }, [{
971
- key : 'a',
972
- type : 'number',
973
- format : 'integer'
974
- }]);
975
-
976
- expect(error).to.be.instanceof(EjvError);
977
- expect(error.type).to.be.eql(ErrorType.FORMAT);
978
- expect(error.message).to.be.eql(ErrorMsg.FORMAT
979
- .replace(ErrorMsgCursorA, 'integer')
980
- );
981
- });
982
-
983
- it('ok', () => {
984
- expect(ejv({
985
- a : 123
986
- }, [{
987
- key : 'a',
988
- type : 'number',
989
- format : 'integer'
990
- }])).to.be.null;
991
- });
992
- });
993
-
994
- describe('multiple formats', () => {
995
- it('fail', () => {
996
- const formatArr : string[] = ['integer'];
997
-
998
- const error : EjvError = ejv({
999
- a : 123.5
1000
- }, [{
1001
- key : 'a',
1002
- type : 'number',
1003
- format : formatArr
1004
- }]);
1005
-
1006
- expect(error).to.be.instanceof(EjvError);
1007
- expect(error.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1008
- expect(error.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1009
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1010
- );
1011
- });
1012
-
1013
- it('ok', () => {
1014
- expect(ejv({
1015
- a : -7
1016
- }, [{
1017
- key : 'a',
1018
- type : 'number',
1019
- format : ['integer']
1020
- }])).to.be.null;
1021
-
1022
- expect(ejv({
1023
- a : 0
1024
- }, [{
1025
- key : 'a',
1026
- type : 'number',
1027
- format : ['integer']
1028
- }])).to.be.null;
1029
-
1030
- expect(ejv({
1031
- a : 123
1032
- }, [{
1033
- key : 'a',
1034
- type : 'number',
1035
- format : ['integer']
1036
- }])).to.be.null;
1037
- });
1038
-
1039
- it('ok - with others', () => {
1040
- expect(ejv({
1041
- a : -7
1042
- }, [{
1043
- key : 'a',
1044
- type : 'number',
1045
- format : ['integer', 'index']
1046
- }])).to.be.null;
1047
-
1048
- expect(ejv({
1049
- a : 0
1050
- }, [{
1051
- key : 'a',
1052
- type : 'number',
1053
- format : ['integer', 'index']
1054
- }])).to.be.null;
1055
-
1056
- expect(ejv({
1057
- a : 123
1058
- }, [{
1059
- key : 'a',
1060
- type : 'number',
1061
- format : ['integer', 'index']
1062
- }])).to.be.null;
1063
- });
1064
-
1065
- it('ok - with others', () => {
1066
- expect(ejv({
1067
- a : -7
1068
- }, [{
1069
- key : 'a',
1070
- type : 'number',
1071
- format : ['index', 'integer']
1072
- }])).to.be.null;
1073
-
1074
- expect(ejv({
1075
- a : 0
1076
- }, [{
1077
- key : 'a',
1078
- type : 'number',
1079
- format : ['index', 'integer']
1080
- }])).to.be.null;
1081
-
1082
- expect(ejv({
1083
- a : 123
1084
- }, [{
1085
- key : 'a',
1086
- type : 'number',
1087
- format : ['index', 'integer']
1088
- }])).to.be.null;
1089
- });
1090
- });
1091
- });
1092
-
1093
- describe('index', () => {
1094
- describe('single format', () => {
1095
- it('fail', () => {
1096
- const error1 : EjvError = ejv({
1097
- a : 1.5
1098
- }, [{
1099
- key : 'a',
1100
- type : 'number',
1101
- format : 'index'
1102
- }]);
1103
-
1104
- expect(error1).to.be.instanceof(EjvError);
1105
- expect(error1.type).to.be.eql(ErrorType.FORMAT);
1106
- expect(error1.message).to.be.eql(ErrorMsg.FORMAT
1107
- .replace(ErrorMsgCursorA, 'index')
1108
- );
1109
-
1110
- const error2 : EjvError = ejv({
1111
- a : -1
1112
- }, [{
1113
- key : 'a',
1114
- type : 'number',
1115
- format : 'index'
1116
- }]);
1117
-
1118
- expect(error2).to.be.instanceof(EjvError);
1119
- expect(error2.type).to.be.eql(ErrorType.FORMAT);
1120
- expect(error2.message).to.be.eql(ErrorMsg.FORMAT
1121
- .replace(ErrorMsgCursorA, 'index')
1122
- );
1123
-
1124
- const error3 : EjvError = ejv({
1125
- a : -1.6
1126
- }, [{
1127
- key : 'a',
1128
- type : 'number',
1129
- format : 'index'
1130
- }]);
1131
-
1132
- expect(error3).to.be.instanceof(EjvError);
1133
- expect(error3.type).to.be.eql(ErrorType.FORMAT);
1134
- expect(error3.message).to.be.eql(ErrorMsg.FORMAT
1135
- .replace(ErrorMsgCursorA, 'index')
1136
- );
1137
- });
1138
-
1139
- it('ok', () => {
1140
- expect(ejv({
1141
- a : 0
1142
- }, [{
1143
- key : 'a',
1144
- type : 'number',
1145
- format : 'index'
1146
- }])).to.be.null;
1147
-
1148
- expect(ejv({
1149
- a : 6
1150
- }, [{
1151
- key : 'a',
1152
- type : 'number',
1153
- format : 'index'
1154
- }])).to.be.null;
1155
- });
1156
- });
1157
-
1158
- describe('multiple formats', () => {
1159
- it('fail', () => {
1160
- const formatArr : string[] = ['index'];
1161
-
1162
- const error1 : EjvError = ejv({
1163
- a : 1.5
1164
- }, [{
1165
- key : 'a',
1166
- type : 'number',
1167
- format : formatArr
1168
- }]);
1169
-
1170
- expect(error1).to.be.instanceof(EjvError);
1171
- expect(error1.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1172
- expect(error1.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1173
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1174
- );
1175
-
1176
- const error2 : EjvError = ejv({
1177
- a : -1
1178
- }, [{
1179
- key : 'a',
1180
- type : 'number',
1181
- format : formatArr
1182
- }]);
1183
-
1184
- expect(error2).to.be.instanceof(EjvError);
1185
- expect(error2.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1186
- expect(error2.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1187
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1188
- );
1189
-
1190
- const error3 : EjvError = ejv({
1191
- a : -1.6
1192
- }, [{
1193
- key : 'a',
1194
- type : 'number',
1195
- format : formatArr
1196
- }]);
1197
-
1198
- expect(error3).to.be.instanceof(EjvError);
1199
- expect(error3.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1200
- expect(error3.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1201
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1202
- );
1203
- });
1204
-
1205
- it('ok', () => {
1206
- expect(ejv({
1207
- a : 0
1208
- }, [{
1209
- key : 'a',
1210
- type : 'number',
1211
- format : ['index']
1212
- }])).to.be.null;
1213
-
1214
- expect(ejv({
1215
- a : 6
1216
- }, [{
1217
- key : 'a',
1218
- type : 'number',
1219
- format : ['index']
1220
- }])).to.be.null;
1221
- });
1222
-
1223
- it('ok - with others', () => {
1224
- expect(ejv({
1225
- a : 0
1226
- }, [{
1227
- key : 'a',
1228
- type : 'number',
1229
- format : ['index', 'integer']
1230
- }])).to.be.null;
1231
-
1232
- expect(ejv({
1233
- a : 6
1234
- }, [{
1235
- key : 'a',
1236
- type : 'number',
1237
- format : ['index', 'integer']
1238
- }])).to.be.null;
1239
- });
1240
-
1241
- it('ok - with others', () => {
1242
- expect(ejv({
1243
- a : 0
1244
- }, [{
1245
- key : 'a',
1246
- type : 'number',
1247
- format : ['integer', 'index']
1248
- }])).to.be.null;
1249
-
1250
- expect(ejv({
1251
- a : 6
1252
- }, [{
1253
- key : 'a',
1254
- type : 'number',
1255
- format : ['integer', 'index']
1256
- }])).to.be.null;
1257
- });
1258
- });
1259
- });
1260
- });
1261
- });
1262
-
1263
- describe('string', () => {
1264
- describe('type', () => {
1265
- describe('mismatch', () => {
1266
- typeTester.filter(obj => obj.type !== 'string')
1267
- .forEach((obj) => {
1268
- const data = {
1269
- a : obj.value
1270
- };
1271
-
1272
- it(obj.type, () => {
1273
- const error : EjvError = ejv(data, [{
1274
- key : 'a',
1275
- type : 'string'
1276
- }]);
1277
-
1278
- expect(error).to.be.instanceof(EjvError);
1279
-
1280
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
1281
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
1282
- .replace(ErrorMsgCursorA, 'string')
1283
- );
1284
- expect(error.path).to.be.eql('a');
1285
- expect(error.data).to.be.deep.equal(data);
1286
- expect(error.errorData).to.be.eql(obj.value);
1287
- });
1288
- });
1289
-
1290
- it('multiple types', () => {
1291
- const value = 'ejv';
1292
- const typeArr : string[] = ['boolean', 'number'];
1293
-
1294
- const data = {
1295
- a : value
1296
- };
1297
-
1298
- const error : EjvError = ejv(data, [{
1299
- key : 'a',
1300
- type : typeArr
1301
- }]);
1302
-
1303
- expect(error).to.be.instanceof(EjvError);
1304
-
1305
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
1306
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
1307
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
1308
- expect(error.path).to.be.eql('a');
1309
- expect(error.data).to.be.deep.equal(data);
1310
- expect(error.errorData).to.be.eql(value);
1311
- });
1312
- });
1313
-
1314
- describe('match', () => {
1315
- it('optional', () => {
1316
- expect(ejv({
1317
- a : undefined
1318
- }, [{
1319
- key : 'a',
1320
- type : 'string',
1321
- optional : true
1322
- }])).to.be.null;
1323
- });
1324
-
1325
- it('single type', () => {
1326
- expect(ejv({
1327
- a : 'ejv'
1328
- }, [{
1329
- key : 'a',
1330
- type : 'string'
1331
- }])).to.be.null;
1332
- });
1333
-
1334
- it('multiple types', () => {
1335
- expect(ejv({
1336
- a : 'ejv'
1337
- }, [{
1338
- key : 'a',
1339
- type : ['string', 'number']
1340
- }])).to.be.null;
1341
- });
1342
-
1343
- it('multiple types', () => {
1344
- expect(ejv({
1345
- a : 'ejv'
1346
- }, [{
1347
- key : 'a',
1348
- type : ['number', 'string']
1349
- }])).to.be.null;
1350
- });
1351
- });
1352
- });
1353
-
1354
- describe('enum', () => {
1355
- describe('check parameter', () => {
1356
- it('undefined is ok', () => {
1357
- expect(ejv({
1358
- a : 'a'
1359
- }, [{
1360
- key : 'a',
1361
- type : 'string',
1362
- enum : undefined
1363
- }])).to.be.null;
1364
- });
1365
-
1366
- it('null', () => {
1367
- expect(() => ejv({
1368
- a : 'a'
1369
- }, [{
1370
- key : 'a',
1371
- type : 'string',
1372
- enum : null
1373
- }])).to.be.throw(ErrorMsg.ENUM_SHOULD_BE_ARRAY);
1374
- });
1375
-
1376
- it('not array', () => {
1377
- expect(() => ejv({
1378
- a : 'a'
1379
- }, [{
1380
- key : 'a',
1381
- type : 'string',
1382
- enum : 'a' as any
1383
- }])).to.throw(ErrorMsg.ENUM_SHOULD_BE_ARRAY);
1384
- });
1385
-
1386
- it('not string', () => {
1387
- expect(() => ejv({
1388
- a : 'a'
1389
- }, [{
1390
- key : 'a',
1391
- type : 'string',
1392
- enum : [10]
1393
- }])).to.throw(ErrorMsg.ENUM_SHOULD_BE_STRINGS);
1394
- });
1395
- });
1396
-
1397
- it('fail', () => {
1398
- const enumArr : string[] = ['b', 'c'];
1399
-
1400
- const data = {
1401
- a : 'a'
1402
- };
1403
-
1404
- const error : EjvError = ejv(data, [{
1405
- key : 'a',
1406
- type : 'string',
1407
- enum : enumArr
1408
- }]);
1409
-
1410
- expect(error).to.be.instanceof(EjvError);
1411
- expect(error.type).to.be.eql(ErrorType.ONE_OF);
1412
- expect(error.message).to.be.eql(ErrorMsg.ONE_OF
1413
- .replace(ErrorMsgCursorA, JSON.stringify(enumArr))
1414
- );
1415
- expect(error.path).to.be.eql('a');
1416
- expect(error.data).to.be.deep.equal(data);
1417
- expect(error.errorData).to.be.eql('a');
1418
- });
1419
-
1420
- it('ok', () => {
1421
- expect(ejv({
1422
- a : 'a'
1423
- }, [{
1424
- key : 'a',
1425
- type : 'string',
1426
- enum : ['a', 'b', 'c']
1427
- }])).to.be.null;
1428
- });
1429
- });
1430
-
1431
- describe('enumReverse', () => {
1432
- describe('check parameter', () => {
1433
- it('undefined is ok', () => {
1434
- expect(ejv({
1435
- a : 'a'
1436
- }, [{
1437
- key : 'a',
1438
- type : 'string',
1439
- enumReverse : undefined
1440
- }])).to.be.null;
1441
- });
1442
-
1443
- it('null', () => {
1444
- expect(() => ejv({
1445
- a : 'a'
1446
- }, [{
1447
- key : 'a',
1448
- type : 'string',
1449
- enumReverse : null
1450
- }])).to.be.throw(ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
1451
- });
1452
-
1453
- it('not array', () => {
1454
- expect(() => ejv({
1455
- a : 'a'
1456
- }, [{
1457
- key : 'a',
1458
- type : 'string',
1459
- enumReverse : 'a' as any
1460
- }])).to.throw(ErrorMsg.ENUM_REVERSE_SHOULD_BE_ARRAY);
1461
- });
1462
-
1463
- it('not string', () => {
1464
- expect(() => ejv({
1465
- a : 'a'
1466
- }, [{
1467
- key : 'a',
1468
- type : 'string',
1469
- enumReverse : [10]
1470
- }])).to.throw(ErrorMsg.ENUM_REVERSE_SHOULD_BE_STRINGS);
1471
- });
1472
- });
1473
-
1474
- it('fail', () => {
1475
- const enumArr : string[] = ['a', 'c'];
1476
-
1477
- const data = {
1478
- a : 'a'
1479
- };
1480
-
1481
- const error : EjvError = ejv(data, [{
1482
- key : 'a',
1483
- type : 'string',
1484
- enumReverse : enumArr
1485
- }]);
1486
-
1487
- expect(error).to.be.instanceof(EjvError);
1488
- expect(error.type).to.be.eql(ErrorType.NOT_ONE_OF);
1489
- expect(error.message).to.be.eql(ErrorMsg.NOT_ONE_OF
1490
- .replace(ErrorMsgCursorA, JSON.stringify(enumArr))
1491
- );
1492
- expect(error.path).to.be.eql('a');
1493
- expect(error.data).to.be.deep.equal(data);
1494
- expect(error.errorData).to.be.eql('a');
1495
- });
1496
-
1497
- it('ok', () => {
1498
- expect(ejv({
1499
- a : 'a'
1500
- }, [{
1501
- key : 'a',
1502
- type : 'string',
1503
- enumReverse : ['b', 'c']
1504
- }])).to.be.null;
1505
- });
1506
- });
1507
-
1508
- describe('minLength', () => {
1509
- describe('check parameter', () => {
1510
- it('undefined is ok', () => {
1511
- expect(ejv({
1512
- a : 'ejv'
1513
- }, [{
1514
- key : 'a',
1515
- type : 'string',
1516
- minLength : undefined
1517
- }])).to.be.null;
1518
- });
1519
-
1520
- it('null', () => {
1521
- expect(() => ejv({
1522
- a : 'a'
1523
- }, [{
1524
- key : 'a',
1525
- type : 'string',
1526
- minLength : null
1527
- }])).to.throw(ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
1528
- });
1529
-
1530
- it('float number', () => {
1531
- expect(() => ejv({
1532
- a : 'a'
1533
- }, [{
1534
- key : 'a',
1535
- type : 'string',
1536
- minLength : 1.5
1537
- }])).to.throw(ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
1538
- });
1539
-
1540
- it('string', () => {
1541
- expect(() => ejv({
1542
- a : 'a'
1543
- }, [{
1544
- key : 'a',
1545
- type : 'string',
1546
- minLength : '1' as any
1547
- }])).to.throw(ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
1548
- });
1549
- });
1550
-
1551
- it('fail', () => {
1552
- const data = {
1553
- a : 'ejv'
1554
- };
1555
-
1556
- const error : EjvError = ejv(data, [{
1557
- key : 'a',
1558
- type : 'string',
1559
- minLength : 4
1560
- }]);
1561
-
1562
- expect(error).to.be.instanceof(EjvError);
1563
- expect(error.type).to.be.eql(ErrorType.MIN_LENGTH);
1564
- expect(error.message).to.be.eql(ErrorMsg.MIN_LENGTH
1565
- .replace(ErrorMsgCursorA, '4'));
1566
- expect(error.path).to.be.eql('a');
1567
- expect(error.data).to.be.deep.equal(data);
1568
- expect(error.errorData).to.be.eql('ejv');
1569
- });
1570
-
1571
- it('ok', () => {
1572
- expect(ejv({
1573
- a : 'ejv'
1574
- }, [{
1575
- key : 'a',
1576
- type : 'string',
1577
- minLength : 2
1578
- }])).to.be.null;
1579
-
1580
- expect(ejv({
1581
- a : 'ejv'
1582
- }, [{
1583
- key : 'a',
1584
- type : 'string',
1585
- minLength : 3
1586
- }])).to.be.null;
1587
- });
1588
- });
1589
-
1590
- describe('maxLength', () => {
1591
- it('check parameter', () => {
1592
- it('undefined is ok', () => {
1593
- expect(ejv({
1594
- a : 'ejv'
1595
- }, [{
1596
- key : 'a',
1597
- type : 'string',
1598
- maxLength : undefined
1599
- }])).to.be.null;
1600
- });
1601
-
1602
- it('null', () => {
1603
- expect(() => ejv({
1604
- a : 'a'
1605
- }, [{
1606
- key : 'a',
1607
- type : 'string',
1608
- maxLength : null
1609
- }])).to.throw(ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
1610
- });
1611
-
1612
- it('float number', () => {
1613
- expect(() => ejv({
1614
- a : 'a'
1615
- }, [{
1616
- key : 'a',
1617
- type : 'string',
1618
- maxLength : 1.5
1619
- }])).to.throw(ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
1620
- });
1621
-
1622
- it('string', () => {
1623
- expect(() => ejv({
1624
- a : 'a'
1625
- }, [{
1626
- key : 'a',
1627
- type : 'string',
1628
- maxLength : '1' as any
1629
- }])).to.throw(ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
1630
- });
1631
- });
1632
-
1633
- it('fail', () => {
1634
- const data = {
1635
- a : 'ejv'
1636
- };
1637
-
1638
- const error : EjvError = ejv(data, [{
1639
- key : 'a',
1640
- type : 'string',
1641
- maxLength : 2
1642
- }]);
1643
-
1644
- expect(error).to.be.instanceof(EjvError);
1645
- expect(error.type).to.be.eql(ErrorType.MAX_LENGTH);
1646
- expect(error.message).to.be.eql(ErrorMsg.MAX_LENGTH
1647
- .replace(ErrorMsgCursorA, '2'));
1648
- expect(error.path).to.be.eql('a');
1649
- expect(error.data).to.be.deep.equal(data);
1650
- expect(error.errorData).to.be.eql('ejv');
1651
- });
1652
-
1653
- it('ok', () => {
1654
- expect(ejv({
1655
- a : 'ejv'
1656
- }, [{
1657
- key : 'a',
1658
- type : 'string',
1659
- maxLength : 3
1660
- }])).to.be.null;
1661
-
1662
- expect(ejv({
1663
- a : 'ejv'
1664
- }, [{
1665
- key : 'a',
1666
- type : 'string',
1667
- maxLength : 4
1668
- }])).to.be.null;
1669
- });
1670
- });
1671
-
1672
- describe('format', () => {
1673
- describe('check parameter', () => {
1674
- it('undefined is ok', () => {
1675
- expect(ejv({
1676
- a : 'ejv@ejv.com'
1677
- }, [{
1678
- key : 'a',
1679
- type : 'string',
1680
- format : undefined
1681
- }])).to.be.null;
1682
- });
1683
-
1684
- it('null', () => {
1685
- expect(() => ejv({
1686
- a : 'ejv@ejv.com'
1687
- }, [{
1688
- key : 'a',
1689
- type : 'string',
1690
- format : null
1691
- }])).to.be.throw(ErrorMsg.INVALID_STRING_FORMAT
1692
- .replace(ErrorMsgCursorA, 'null'));
1693
- });
1694
-
1695
- describe('invalid string format', () => {
1696
- it('single format', () => {
1697
- expect(() => ejv({
1698
- a : 'a'
1699
- }, [{
1700
- key : 'a',
1701
- type : 'string',
1702
- format : 'invalidStringFormat'
1703
- }])).to.throw(ErrorMsg.INVALID_STRING_FORMAT
1704
- .replace(ErrorMsgCursorA, 'invalidStringFormat'));
1705
- });
1706
-
1707
- it('multiple format', () => {
1708
- expect(() => ejv({
1709
- a : 'a'
1710
- }, [{
1711
- key : 'a',
1712
- type : 'string',
1713
- format : ['invalidStringFormat']
1714
- }])).to.throw(ErrorMsg.INVALID_STRING_FORMAT
1715
- .replace(ErrorMsgCursorA, 'invalidStringFormat'));
1716
- });
1717
- });
1718
- });
1719
-
1720
- describe('email', () => {
1721
- it('single format', () => {
1722
- const data = {
1723
- a : 'ejv'
1724
- };
1725
-
1726
- const error : EjvError = ejv(data, [{
1727
- key : 'a',
1728
- type : 'string',
1729
- format : 'email'
1730
- }]);
1731
-
1732
- expect(error).to.be.instanceof(EjvError);
1733
- expect(error.type).to.be.eql(ErrorType.FORMAT);
1734
- expect(error.message).to.be.eql(ErrorMsg.FORMAT
1735
- .replace(ErrorMsgCursorA, 'email')
1736
- );
1737
- expect(error.path).to.be.eql('a');
1738
- expect(error.data).to.be.deep.equal(data);
1739
- expect(error.errorData).to.be.eql('ejv');
1740
-
1741
- expect(ejv({
1742
- a : 'ejv@ejv.com'
1743
- }, [{
1744
- key : 'a',
1745
- type : 'string',
1746
- format : 'email'
1747
- }])).to.be.null;
1748
- });
1749
-
1750
- it('multiple format', () => {
1751
- const formatArr : string[] = ['email', 'date'];
1752
-
1753
- const data = {
1754
- a : 'ejv'
1755
- };
1756
-
1757
- const error : EjvError = ejv(data, [{
1758
- key : 'a',
1759
- type : 'string',
1760
- format : formatArr
1761
- }]);
1762
-
1763
- expect(error).to.be.instanceof(EjvError);
1764
- expect(error.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1765
- expect(error.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1766
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1767
- );
1768
- expect(error.path).to.be.eql('a');
1769
- expect(error.data).to.be.deep.equal(data);
1770
- expect(error.errorData).to.be.eql('ejv');
1771
-
1772
- expect(ejv({
1773
- a : 'ejv@ejv.com'
1774
- }, [{
1775
- key : 'a',
1776
- type : 'string',
1777
- format : formatArr
1778
- }])).to.be.null;
1779
- });
1780
- });
1781
-
1782
- describe('date', () => {
1783
- it('single format', () => {
1784
- const data = {
1785
- a : 'ejv'
1786
- };
1787
-
1788
- const error : EjvError = ejv(data, [{
1789
- key : 'a',
1790
- type : 'string',
1791
- format : 'date'
1792
- }]);
1793
-
1794
- expect(error).to.be.instanceof(EjvError);
1795
- expect(error.type).to.be.eql(ErrorType.FORMAT);
1796
- expect(error.message).to.be.eql(ErrorMsg.FORMAT
1797
- .replace(ErrorMsgCursorA, 'date')
1798
- );
1799
- expect(error.path).to.be.eql('a');
1800
- expect(error.data).to.be.deep.equal(data);
1801
- expect(error.errorData).to.be.eql('ejv');
1802
-
1803
- expect(ejv({
1804
- a : '2018-12-19'
1805
- }, [{
1806
- key : 'a',
1807
- type : 'string',
1808
- format : 'date'
1809
- }])).to.be.null;
1810
- });
1811
-
1812
- it('multiple format', () => {
1813
- const formatArr : string[] = ['date'];
1814
-
1815
- const data = {
1816
- a : 'ejv'
1817
- };
1818
-
1819
- const error : EjvError = ejv(data, [{
1820
- key : 'a',
1821
- type : 'string',
1822
- format : formatArr
1823
- }]);
1824
-
1825
- expect(error).to.be.instanceof(EjvError);
1826
- expect(error.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1827
- expect(error.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1828
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1829
- );
1830
- expect(error.path).to.be.eql('a');
1831
- expect(error.data).to.be.deep.equal(data);
1832
- expect(error.errorData).to.be.eql('ejv');
1833
-
1834
- expect(ejv({
1835
- a : '2018-12-19'
1836
- }, [{
1837
- key : 'a',
1838
- type : 'string',
1839
- format : formatArr
1840
- }])).to.be.null;
1841
- });
1842
- });
1843
-
1844
- describe('time', () => {
1845
- it('single format', () => {
1846
- const data = {
1847
- a : 'ejv'
1848
- };
1849
-
1850
- const error : EjvError = ejv(data, [{
1851
- key : 'a',
1852
- type : 'string',
1853
- format : 'time'
1854
- }]);
1855
-
1856
- expect(error).to.be.instanceof(EjvError);
1857
- expect(error.type).to.be.eql(ErrorType.FORMAT);
1858
- expect(error.message).to.be.eql(ErrorMsg.FORMAT
1859
- .replace(ErrorMsgCursorA, 'time')
1860
- );
1861
- expect(error.path).to.be.eql('a');
1862
- expect(error.data).to.be.deep.equal(data);
1863
- expect(error.errorData).to.be.eql('ejv');
1864
-
1865
- expect(ejv({
1866
- a : '00:27:35.123'
1867
- }, [{
1868
- key : 'a',
1869
- type : 'string',
1870
- format : 'time'
1871
- }])).to.be.null;
1872
- });
1873
-
1874
- it('multiple format', () => {
1875
- const formatArr : string[] = ['time'];
1876
-
1877
- const data = {
1878
- a : 'ejv'
1879
- };
1880
-
1881
- const error : EjvError = ejv(data, [{
1882
- key : 'a',
1883
- type : 'string',
1884
- format : formatArr
1885
- }]);
1886
-
1887
- expect(error).to.be.instanceof(EjvError);
1888
- expect(error.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1889
- expect(error.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1890
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1891
- );
1892
- expect(error.path).to.be.eql('a');
1893
- expect(error.data).to.be.deep.equal(data);
1894
- expect(error.errorData).to.be.eql('ejv');
1895
-
1896
- expect(ejv({
1897
- a : '00:27:35.123'
1898
- }, [{
1899
- key : 'a',
1900
- type : 'string',
1901
- format : formatArr
1902
- }])).to.be.null;
1903
- });
1904
- });
1905
-
1906
- describe('date-time', () => {
1907
- it('single format', () => {
1908
- const data = {
1909
- a : 'ejv'
1910
- };
1911
-
1912
- const error : EjvError = ejv(data, [{
1913
- key : 'a',
1914
- type : 'string',
1915
- format : 'date-time'
1916
- }]);
1917
-
1918
- expect(error).to.be.instanceof(EjvError);
1919
- expect(error.type).to.be.eql(ErrorType.FORMAT);
1920
- expect(error.message).to.be.eql(ErrorMsg.FORMAT
1921
- .replace(ErrorMsgCursorA, 'date-time')
1922
- );
1923
- expect(error.path).to.be.eql('a');
1924
- expect(error.data).to.be.deep.equal(data);
1925
- expect(error.errorData).to.be.eql('ejv');
1926
-
1927
- expect(ejv({
1928
- a : '2018-12-19T00:27:35.123Z'
1929
- }, [{
1930
- key : 'a',
1931
- type : 'string',
1932
- format : 'date-time'
1933
- }])).to.be.null;
1934
-
1935
- expect(ejv({
1936
- a : '2018-12-19T00:27:35+00:00'
1937
- }, [{
1938
- key : 'a',
1939
- type : 'string',
1940
- format : 'date-time'
1941
- }])).to.be.null;
1942
-
1943
- expect(ejv({
1944
- a : '20181219T002735Z'
1945
- }, [{
1946
- key : 'a',
1947
- type : 'string',
1948
- format : 'date-time'
1949
- }])).to.be.null;
1950
- });
1951
-
1952
- it('multiple format', () => {
1953
- const formatArr : string[] = ['date-time'];
1954
-
1955
- const data = {
1956
- a : 'ejv'
1957
- };
1958
-
1959
- const error : EjvError = ejv(data, [{
1960
- key : 'a',
1961
- type : 'string',
1962
- format : formatArr
1963
- }]);
1964
-
1965
- expect(error).to.be.instanceof(EjvError);
1966
- expect(error.type).to.be.eql(ErrorType.FORMAT_ONE_OF);
1967
- expect(error.message).to.be.eql(ErrorMsg.FORMAT_ONE_OF
1968
- .replace(ErrorMsgCursorA, JSON.stringify(formatArr))
1969
- );
1970
- expect(error.path).to.be.eql('a');
1971
- expect(error.data).to.be.deep.equal(data);
1972
- expect(error.errorData).to.be.eql('ejv');
1973
-
1974
- expect(ejv({
1975
- a : '2018-12-19T00:27:35.123Z'
1976
- }, [{
1977
- key : 'a',
1978
- type : 'string',
1979
- format : formatArr
1980
- }])).to.be.null;
1981
-
1982
- expect(ejv({
1983
- a : '2018-12-19T00:27:35+00:00'
1984
- }, [{
1985
- key : 'a',
1986
- type : 'string',
1987
- format : formatArr
1988
- }])).to.be.null;
1989
-
1990
- expect(ejv({
1991
- a : '20181219T002735Z'
1992
- }, [{
1993
- key : 'a',
1994
- type : 'string',
1995
- format : formatArr
1996
- }])).to.be.null;
1997
- });
1998
- });
1999
- });
2000
-
2001
- describe('pattern', () => {
2002
- describe('check parameter', () => {
2003
- it('undefined is ok', () => {
2004
- expect(ejv({
2005
- a : 'ejv@ejv.com'
2006
- }, [{
2007
- key : 'a',
2008
- type : 'string',
2009
- pattern : undefined
2010
- }])).to.be.null;
2011
- });
2012
-
2013
- it('null', () => {
2014
- expect(() => ejv({
2015
- a : 'ejv@ejv.com'
2016
- }, [{
2017
- key : 'a',
2018
- type : 'string',
2019
- pattern : null
2020
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2021
- .replace(ErrorMsgCursorA, 'null'));
2022
- });
2023
-
2024
- it('number', () => {
2025
- expect(() => ejv({
2026
- a : 'ejv@ejv.com'
2027
- }, [{
2028
- key : 'a',
2029
- type : 'string',
2030
- pattern : 1 as any
2031
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2032
- .replace(ErrorMsgCursorA, '1'));
2033
- });
2034
-
2035
- it('empty string', () => {
2036
- expect(() => ejv({
2037
- a : 'ejv@ejv.com'
2038
- }, [{
2039
- key : 'a',
2040
- type : 'string',
2041
- pattern : ''
2042
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2043
- .replace(ErrorMsgCursorA, '//'));
2044
- });
2045
-
2046
- it('empty array', () => {
2047
- expect(() => ejv({
2048
- a : 'ejv@ejv.com'
2049
- }, [{
2050
- key : 'a',
2051
- type : 'string',
2052
- pattern : []
2053
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2054
- .replace(ErrorMsgCursorA, '[]'));
2055
- });
2056
-
2057
- it('null array', () => {
2058
- expect(() => ejv({
2059
- a : 'ejv@ejv.com'
2060
- }, [{
2061
- key : 'a',
2062
- type : 'string',
2063
- pattern : [null, /ab/]
2064
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2065
- .replace(ErrorMsgCursorA, '[/null/, /ab/]'));
2066
- });
2067
-
2068
- it('number array', () => {
2069
- expect(() => ejv({
2070
- a : 'ejv@ejv.com'
2071
- }, [{
2072
- key : 'a',
2073
- type : 'string',
2074
- pattern : [1, 3] as any
2075
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2076
- .replace(ErrorMsgCursorA, '[1, 3]'));
2077
- });
2078
-
2079
- it('empty string array', () => {
2080
- expect(() => ejv({
2081
- a : 'ejv@ejv.com'
2082
- }, [{
2083
- key : 'a',
2084
- type : 'string',
2085
- pattern : ['']
2086
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2087
- .replace(ErrorMsgCursorA, '[//]'));
2088
- });
2089
-
2090
- it('empty reg exp', () => {
2091
- expect(() => ejv({
2092
- a : 'ejv@ejv.com'
2093
- }, [{
2094
- key : 'a',
2095
- type : 'string',
2096
- pattern : new RegExp('')
2097
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2098
- .replace(ErrorMsgCursorA, '//'));
2099
- });
2100
-
2101
- it('null reg exp', () => {
2102
- expect(() => ejv({
2103
- a : 'ejv@ejv.com'
2104
- }, [{
2105
- key : 'a',
2106
- type : 'string',
2107
- pattern : new RegExp(null)
2108
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2109
- .replace(ErrorMsgCursorA, '/null/'));
2110
- });
2111
-
2112
- it('empty reg exp array', () => {
2113
- expect(() => ejv({
2114
- a : 'ejv@ejv.com'
2115
- }, [{
2116
- key : 'a',
2117
- type : 'string',
2118
- pattern : [new RegExp('')]
2119
- }])).to.be.throw(ErrorMsg.INVALID_STRING_PATTERN
2120
- .replace(ErrorMsgCursorA, '[//]'));
2121
- });
2122
- });
2123
-
2124
- it('by string', () => {
2125
- expect(ejv({
2126
- a : 'abc'
2127
- }, [{
2128
- key : 'a',
2129
- type : 'string',
2130
- pattern : 'ab+c'
2131
- }])).to.be.null;
2132
-
2133
- const data = {
2134
- a : 'abc'
2135
- };
2136
-
2137
- const error : EjvError = ejv(data, [{
2138
- key : 'a',
2139
- type : 'string',
2140
- pattern : 'ac'
2141
- }]);
2142
-
2143
- expect(error).to.be.instanceof(EjvError);
2144
- expect(error.type).to.be.eql(ErrorType.PATTERN);
2145
- expect(error.message).to.be.eql(ErrorMsg.PATTERN
2146
- .replace(ErrorMsgCursorA, '/ac/'));
2147
- expect(error.path).to.be.eql('a');
2148
- expect(error.data).to.be.deep.equal(data);
2149
- expect(error.errorData).to.be.eql('abc');
2150
- });
2151
-
2152
- it('by string[]', () => {
2153
- expect(ejv({
2154
- a : 'abc'
2155
- }, [{
2156
- key : 'a',
2157
- type : 'string',
2158
- pattern : ['ab+c']
2159
- }])).to.be.null;
2160
-
2161
- expect(ejv({
2162
- a : 'abc'
2163
- }, [{
2164
- key : 'a',
2165
- type : 'string',
2166
- pattern : ['ac', 'ab+c']
2167
- }])).to.be.null;
2168
-
2169
- expect(ejv({
2170
- a : 'abc'
2171
- }, [{
2172
- key : 'a',
2173
- type : 'string',
2174
- pattern : ['ab+c', 'ac']
2175
- }])).to.be.null;
2176
-
2177
- const data = {
2178
- a : 'abc'
2179
- };
2180
-
2181
- const error : EjvError = ejv(data, [{
2182
- key : 'a',
2183
- type : 'string',
2184
- pattern : ['abcc', 'ac']
2185
- }]);
2186
-
2187
- expect(error).to.be.instanceof(EjvError);
2188
- expect(error.type).to.be.eql(ErrorType.PATTERN_ONE_OF);
2189
- expect(error.message).to.be.eql(ErrorMsg.PATTERN_ONE_OF
2190
- .replace(ErrorMsgCursorA, '[/abcc/, /ac/]'));
2191
- expect(error.path).to.be.eql('a');
2192
- expect(error.data).to.be.deep.equal(data);
2193
- expect(error.errorData).to.be.eql('abc');
2194
- });
2195
-
2196
- it('by RegExp', () => {
2197
- expect(ejv({
2198
- a : 'abc'
2199
- }, [{
2200
- key : 'a',
2201
- type : 'string',
2202
- pattern : /ab+c/
2203
- }])).to.be.null;
2204
-
2205
- const data = {
2206
- a : 'abc'
2207
- };
2208
-
2209
- const error : EjvError = ejv(data, [{
2210
- key : 'a',
2211
- type : 'string',
2212
- pattern : /ac/
2213
- }]);
2214
-
2215
- expect(error).to.be.instanceof(EjvError);
2216
- expect(error.type).to.be.eql(ErrorType.PATTERN);
2217
- expect(error.message).to.be.eql(ErrorMsg.PATTERN
2218
- .replace(ErrorMsgCursorA, /ac/.toString()));
2219
- expect(error.path).to.be.eql('a');
2220
- expect(error.data).to.be.deep.equal(data);
2221
- expect(error.errorData).to.be.eql('abc');
2222
- });
2223
-
2224
- it('by RegExp[]', () => {
2225
- expect(ejv({
2226
- a : 'abc'
2227
- }, [{
2228
- key : 'a',
2229
- type : 'string',
2230
- pattern : /ab+c/
2231
- }])).to.be.null;
2232
-
2233
- expect(ejv({
2234
- a : 'abc'
2235
- }, [{
2236
- key : 'a',
2237
- type : 'string',
2238
- pattern : [/ac/, /ab+c/]
2239
- }])).to.be.null;
2240
-
2241
- expect(ejv({
2242
- a : 'abc'
2243
- }, [{
2244
- key : 'a',
2245
- type : 'string',
2246
- pattern : [/ab+c/, /ac/]
2247
- }])).to.be.null;
2248
-
2249
- const data = {
2250
- a : 'abc'
2251
- };
2252
-
2253
- const error : EjvError = ejv(data, [{
2254
- key : 'a',
2255
- type : 'string',
2256
- pattern : [/abcc/, /ac/]
2257
- }]);
2258
-
2259
- expect(error).to.be.instanceof(EjvError);
2260
- expect(error.type).to.be.eql(ErrorType.PATTERN_ONE_OF);
2261
- expect(error.message).to.be.eql(ErrorMsg.PATTERN_ONE_OF
2262
- .replace(ErrorMsgCursorA, '[/abcc/, /ac/]'));
2263
- expect(error.path).to.be.eql('a');
2264
- expect(error.data).to.be.deep.equal(data);
2265
- expect(error.errorData).to.be.eql('abc');
2266
- });
2267
-
2268
- describe('special case', () => {
2269
- it('array of object has string', () => {
2270
- const error : EjvError = ejv({
2271
- a : [{
2272
- b : 'ejv'
2273
- }]
2274
- }, [{
2275
- key : 'a',
2276
- type : 'array',
2277
- items : [{
2278
- type : 'object',
2279
- properties : [{
2280
- key : 'b',
2281
- type : 'string',
2282
- pattern : /ejv/
2283
- }]
2284
- }]
2285
- }]);
2286
-
2287
- expect(error).to.be.null;
2288
- });
2289
- });
2290
- });
2291
- });
2292
-
2293
- describe('object', () => {
2294
- describe('type', () => {
2295
- describe('mismatch', () => {
2296
- typeTester.filter(obj => {
2297
- return !['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
2298
- })
2299
- .forEach((obj) => {
2300
- const data = {
2301
- a : obj.value
2302
- };
2303
-
2304
- it(obj.type, () => {
2305
- const error : EjvError = ejv(data, [{
2306
- key : 'a',
2307
- type : 'object'
2308
- }]);
2309
-
2310
- expect(error).to.be.instanceof(EjvError);
2311
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
2312
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
2313
- .replace(ErrorMsgCursorA, 'object')
2314
- );
2315
- expect(error.path).to.be.eql('a');
2316
- expect(error.data).to.be.deep.equal(data);
2317
- expect(error.errorData).to.be.eql(obj.value);
2318
- });
2319
- });
2320
-
2321
- it('multiple types', () => {
2322
- const value = {};
2323
- const typeArr : string[] = ['boolean', 'number'];
2324
-
2325
- const data = {
2326
- a : value
2327
- };
2328
-
2329
- const error : EjvError = ejv(data, [{
2330
- key : 'a',
2331
- type : typeArr
2332
- }]);
2333
-
2334
- expect(error).to.be.instanceof(EjvError);
2335
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
2336
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
2337
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
2338
- expect(error.path).to.be.eql('a');
2339
- expect(error.data).to.be.deep.equal(data);
2340
- expect(error.errorData).to.be.eql(value);
2341
- });
2342
- });
2343
-
2344
- describe('match', () => {
2345
- it('optional', () => {
2346
- expect(ejv({
2347
- a : undefined
2348
- }, [{
2349
- key : 'a',
2350
- type : 'object',
2351
- optional : true
2352
- }])).to.be.null;
2353
- });
2354
-
2355
- typeTester.filter(obj => {
2356
- return ['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
2357
- })
2358
- .forEach((obj) => {
2359
- it(obj.type, () => {
2360
- expect(ejv({
2361
- a : obj.value
2362
- }, [{
2363
- key : 'a',
2364
- type : 'object'
2365
- }])).to.be.null;
2366
- });
2367
- });
2368
-
2369
- it('single type', () => {
2370
- expect(ejv({
2371
- a : {
2372
- b : 1
2373
- }
2374
- }, [{
2375
- key : 'a',
2376
- type : 'object'
2377
- }])).to.be.null;
2378
- });
2379
-
2380
- it('multiple types', () => {
2381
- expect(ejv({
2382
- a : {
2383
- b : 1
2384
- }
2385
- }, [{
2386
- key : 'a',
2387
- type : ['object', 'number']
2388
- }])).to.be.null;
2389
- });
2390
-
2391
- it('multiple types', () => {
2392
- expect(ejv({
2393
- a : {
2394
- b : 1
2395
- }
2396
- }, [{
2397
- key : 'a',
2398
- type : ['number', 'object']
2399
- }])).to.be.null;
2400
- });
2401
- });
2402
- });
2403
-
2404
- describe('properties', () => {
2405
- describe('check parameter', () => {
2406
- it('undefined is ok', () => {
2407
- expect(ejv({
2408
- a : {
2409
- b : 1
2410
- }
2411
- }, [{
2412
- key : 'a',
2413
- type : 'object',
2414
- properties : undefined
2415
- }])).to.be.null;
2416
- });
2417
-
2418
- it('null', () => {
2419
- expect(() => ejv({
2420
- a : {
2421
- b : 1
2422
- }
2423
- }, [{
2424
- key : 'a',
2425
- type : 'object',
2426
- properties : null
2427
- }])).to.throw(ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY);
2428
- });
2429
-
2430
- it('not array', () => {
2431
- expect(() => ejv({
2432
- a : {
2433
- b : 1
2434
- }
2435
- }, [{
2436
- key : 'a',
2437
- type : 'object',
2438
- properties : 'b' as any
2439
- }])).to.throw(ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY);
2440
- });
2441
-
2442
- it('empty array', () => {
2443
- expect(() => ejv({
2444
- a : {
2445
- b : 1
2446
- }
2447
- }, [{
2448
- key : 'a',
2449
- type : 'object',
2450
- properties : []
2451
- }])).to.throw(ErrorMsg.PROPERTIES_SHOULD_HAVE_ITEMS);
2452
- });
2453
-
2454
- it('not object array', () => {
2455
- expect(() => ejv({
2456
- a : {
2457
- b : 1
2458
- }
2459
- }, [{
2460
- key : 'a',
2461
- type : 'object',
2462
- properties : ['b'] as any
2463
- }])).to.throw(ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT);
2464
- });
2465
- });
2466
-
2467
- it('with single type', () => {
2468
- const undefinedError : EjvError = ejv({
2469
- a : {
2470
- b : undefined
2471
- }
2472
- }, [{
2473
- key : 'a',
2474
- type : 'object',
2475
- properties : [{
2476
- key : 'b',
2477
- type : 'string'
2478
- }]
2479
- }]);
2480
-
2481
- expect(undefinedError).to.be.instanceof(EjvError);
2482
- expect(undefinedError.type).to.be.eql(ErrorType.REQUIRED);
2483
- expect(undefinedError.message).to.be.eql(ErrorMsg.REQUIRED);
2484
- expect(undefinedError.path).to.be.eql('a/b');
2485
-
2486
- const nullError : EjvError = ejv({
2487
- a : null
2488
- }, [{
2489
- key : 'a',
2490
- type : 'object',
2491
- properties : [{
2492
- key : 'b',
2493
- type : 'string'
2494
- }]
2495
- }]);
2496
-
2497
- expect(nullError).to.be.instanceof(EjvError);
2498
- expect(nullError.type).to.be.eql(ErrorType.REQUIRED);
2499
- expect(nullError.message).to.be.eql(ErrorMsg.REQUIRED);
2500
- expect(nullError.path).to.be.eql('a');
2501
-
2502
- const data = {
2503
- a : {
2504
- b : 1
2505
- }
2506
- };
2507
-
2508
- const error : EjvError = ejv(data, [{
2509
- key : 'a',
2510
- type : 'object',
2511
- properties : [{
2512
- key : 'b',
2513
- type : 'string'
2514
- }]
2515
- }]);
2516
-
2517
- expect(error).to.be.instanceof(EjvError);
2518
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
2519
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
2520
- .replace(ErrorMsgCursorA, 'string'));
2521
- expect(error.path).to.be.eql('a/b');
2522
- expect(error.data).to.be.deep.equal(data);
2523
- expect(error.errorData).to.be.eql(1);
2524
-
2525
- expect(ejv({
2526
- a : {
2527
- b : 1
2528
- }
2529
- }, [{
2530
- key : 'a',
2531
- type : 'object',
2532
- properties : [{
2533
- key : 'b',
2534
- type : 'number'
2535
- }]
2536
- }])).to.be.null;
2537
- });
2538
-
2539
- it('with multiple types', () => {
2540
- const typeArr : string[] = ['string', 'boolean'];
2541
-
2542
- const undefinedError : EjvError = ejv({
2543
- a : {
2544
- b : undefined
2545
- }
2546
- }, [{
2547
- key : 'a',
2548
- type : 'object',
2549
- properties : [{
2550
- key : 'b',
2551
- type : typeArr
2552
- }]
2553
- }]);
2554
-
2555
- expect(undefinedError).to.be.instanceof(EjvError);
2556
- expect(undefinedError.type).to.be.eql(ErrorType.REQUIRED);
2557
- expect(undefinedError.message).to.be.eql(ErrorMsg.REQUIRED);
2558
- expect(undefinedError.path).to.be.eql('a/b');
2559
-
2560
- const data = {
2561
- a : {
2562
- b : 1
2563
- }
2564
- };
2565
-
2566
- const error : EjvError = ejv(data, [{
2567
- key : 'a',
2568
- type : 'object',
2569
- properties : [{
2570
- key : 'b',
2571
- type : typeArr
2572
- }]
2573
- }]);
2574
-
2575
- expect(error).to.be.instanceof(EjvError);
2576
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
2577
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
2578
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
2579
- expect(error.path).to.be.eql('a/b');
2580
- expect(error.data).to.be.deep.equal(data);
2581
- expect(error.errorData).to.be.eql(1);
2582
-
2583
- expect(ejv({
2584
- a : {
2585
- b : 1
2586
- }
2587
- }, [{
2588
- key : 'a',
2589
- type : 'object',
2590
- properties : [{
2591
- key : 'b',
2592
- type : ['number', 'string']
2593
- }]
2594
- }])).to.be.null;
2595
-
2596
- expect(ejv({
2597
- a : {
2598
- b : 1
2599
- }
2600
- }, [{
2601
- key : 'a',
2602
- type : 'object',
2603
- properties : [{
2604
- key : 'b',
2605
- type : ['string', 'number']
2606
- }]
2607
- }])).to.be.null;
2608
- });
2609
- });
2610
-
2611
- describe('allowNoProperty', () => {
2612
- describe('check parameter', () => {
2613
- it('undefined is ok', () => {
2614
- expect(ejv({
2615
- a : {
2616
- b : 1
2617
- }
2618
- }, [{
2619
- key : 'a',
2620
- type : 'object',
2621
- allowNoProperty : undefined
2622
- }])).to.be.null;
2623
- });
2624
-
2625
- it('null', () => {
2626
- expect(() => ejv({
2627
- a : {
2628
- b : 1
2629
- }
2630
- }, [{
2631
- key : 'a',
2632
- type : 'object',
2633
- allowNoProperty : null
2634
- }])).to.throw(ErrorMsg.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN);
2635
- });
2636
- });
2637
-
2638
- describe('default', () => {
2639
- it('empty object', () => {
2640
- const error : EjvError = ejv({
2641
- a : {}
2642
- }, [{
2643
- key : 'a',
2644
- type : 'object'
2645
- }]);
2646
-
2647
- expect(error).to.be.null;
2648
- });
2649
-
2650
- it('object has property', () => {
2651
- const error : EjvError = ejv({
2652
- a : {
2653
- b : 1
2654
- }
2655
- }, [{
2656
- key : 'a',
2657
- type : 'object'
2658
- }]);
2659
-
2660
- expect(error).to.be.null;
2661
- });
2662
- });
2663
-
2664
- describe('allowNoProperty === false', () => {
2665
- it('empty object', () => {
2666
- const error : EjvError = ejv({
2667
- a : {}
2668
- }, [{
2669
- key : 'a',
2670
- type : 'object',
2671
- allowNoProperty : false
2672
- }]);
2673
-
2674
- expect(error).to.be.instanceof(EjvError);
2675
- expect(error.type).to.be.eql(ErrorType.NO_PROPERTY);
2676
- expect(error.message).to.be.eql(ErrorMsg.NO_PROPERTY);
2677
- expect(error.path).to.be.eql('a');
2678
- expect(error.data).to.be.a('object');
2679
- });
2680
-
2681
- it('object has property', () => {
2682
- const error : EjvError = ejv({
2683
- a : {
2684
- b : 1
2685
- }
2686
- }, [{
2687
- key : 'a',
2688
- type : 'object',
2689
- allowNoProperty : false
2690
- }]);
2691
-
2692
- expect(error).to.be.null;
2693
- });
2694
- });
2695
-
2696
- describe('allowNoProperty === true', () => {
2697
- it('empty object', () => {
2698
- const error : EjvError = ejv({
2699
- a : {}
2700
- }, [{
2701
- key : 'a',
2702
- type : 'object',
2703
- allowNoProperty : true
2704
- }]);
2705
-
2706
- expect(error).to.be.null;
2707
- });
2708
-
2709
- it('object has property', () => {
2710
- const error : EjvError = ejv({
2711
- a : {
2712
- b : 1
2713
- }
2714
- }, [{
2715
- key : 'a',
2716
- type : 'object',
2717
- allowNoProperty : true
2718
- }]);
2719
-
2720
- expect(error).to.be.null;
2721
- });
2722
- });
2723
- });
2724
- });
2725
-
2726
- describe('date', () => {
2727
- const now : Date = new Date();
2728
-
2729
- const year : number = now.getFullYear();
2730
- const month : number = now.getMonth();
2731
- const date : number = now.getDate();
2732
-
2733
- const hours : number = now.getHours();
2734
- const minutes : number = now.getMinutes();
2735
- const seconds : number = now.getSeconds();
2736
- const ms : number = now.getMilliseconds();
2737
-
2738
- const nowOnlyDate : Date = new Date();
2739
- nowOnlyDate.setHours(0, 0, 0, 0);
2740
-
2741
- const dateTestData = {
2742
- date : nowOnlyDate
2743
- };
2744
-
2745
- const dateTimeTestData = {
2746
- date : now
2747
- };
2748
-
2749
- function padZero(value : number, digit : number = 2) : string {
2750
- return ('' + value).padStart(digit, '0');
2751
- }
2752
-
2753
- function getDateStr(year : number, month : number, date : number,
2754
- hours? : number, minutes? : number, seconds? : number, ms? : number) : string {
2755
- const tempDate : Date = hours !== undefined ?
2756
- new Date(year, month, date, hours, minutes, seconds, ms) :
2757
- new Date(year, month, date);
2758
-
2759
- const dateStr : string = [
2760
- tempDate.getFullYear(),
2761
- padZero(tempDate.getMonth() + 1),
2762
- padZero(tempDate.getDate())
2763
- ].join('-');
2764
-
2765
- const hoursStr : string = hours !== undefined ?
2766
- [
2767
- 'T',
2768
- [
2769
- padZero(hours),
2770
- padZero(minutes),
2771
- padZero(seconds)
2772
- ].join(':'),
2773
- '.',
2774
- padZero(ms, 3),
2775
- 'Z'
2776
- ].join('') :
2777
- '';
2778
-
2779
- return dateStr + hoursStr;
2780
- }
2781
-
2782
- describe('type', () => {
2783
- describe('mismatch', () => {
2784
- typeTester.filter(obj => obj.type !== 'date')
2785
- .forEach((obj) => {
2786
- const data = {
2787
- a : obj.value
2788
- };
2789
-
2790
- it(obj.type, () => {
2791
- const error : EjvError = ejv(data, [{
2792
- key : 'a',
2793
- type : 'date'
2794
- }]);
2795
-
2796
- expect(error).to.be.instanceof(EjvError);
2797
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
2798
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
2799
- .replace(ErrorMsgCursorA, 'date')
2800
- );
2801
- expect(error.path).to.be.eql('a');
2802
- expect(error.data).to.be.deep.equal(data);
2803
- expect(error.errorData).to.be.eql(obj.value);
2804
- });
2805
- });
2806
-
2807
- it('multiple types', () => {
2808
- const value = 'ejv';
2809
- const typeArr : string[] = ['boolean', 'date'];
2810
-
2811
- const data = {
2812
- a : value
2813
- };
2814
-
2815
- const error : EjvError = ejv(data, [{
2816
- key : 'a',
2817
- type : typeArr
2818
- }]);
2819
-
2820
- expect(error).to.be.instanceof(EjvError);
2821
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
2822
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
2823
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
2824
- expect(error.path).to.be.eql('a');
2825
- expect(error.data).to.be.deep.equal(data);
2826
- expect(error.errorData).to.be.eql(value);
2827
- });
2828
- });
2829
-
2830
- describe('match', () => {
2831
- it('optional', () => {
2832
- expect(ejv({
2833
- a : undefined
2834
- }, [{
2835
- key : 'a',
2836
- type : 'date',
2837
- optional : true
2838
- }])).to.be.null;
2839
- });
2840
-
2841
- it('single type', () => {
2842
- expect(ejv({
2843
- a : new Date
2844
- }, [{
2845
- key : 'a',
2846
- type : 'date'
2847
- }])).to.be.null;
2848
- });
2849
-
2850
- it('multiple types', () => {
2851
- expect(ejv({
2852
- a : new Date
2853
- }, [{
2854
- key : 'a',
2855
- type : ['date', 'number']
2856
- }])).to.be.null;
2857
- });
2858
-
2859
- it('multiple types', () => {
2860
- expect(ejv({
2861
- a : new Date
2862
- }, [{
2863
- key : 'a',
2864
- type : ['number', 'date']
2865
- }])).to.be.null;
2866
- });
2867
- });
2868
- });
2869
-
2870
- describe('min & exclusiveMin', () => {
2871
- describe('check parameter', () => {
2872
- it('min === null', () => {
2873
- expect(() => ejv({
2874
- date : new Date
2875
- }, [{
2876
- key : 'date',
2877
- type : 'date',
2878
- min : null
2879
- }])).to.throw(ErrorMsg.MIN_DATE_SHOULD_BE_DATE_OR_STRING);
2880
- });
2881
-
2882
- it('exclusiveMin === null', () => {
2883
- expect(() => ejv({
2884
- date : new Date
2885
- }, [{
2886
- key : 'date',
2887
- type : 'date',
2888
- min : new Date,
2889
- exclusiveMin : null
2890
- }])).to.throw(ErrorMsg.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN);
2891
- });
2892
- });
2893
-
2894
- describe('by date', () => {
2895
-
2896
- it('without exclusiveMin', () => {
2897
- expect(ejv(dateTestData, [{
2898
- key : 'date',
2899
- type : 'date',
2900
- min : new Date(year, month, date - 1)
2901
- }])).to.be.null;
2902
-
2903
- expect(ejv(dateTestData, [{
2904
- key : 'date',
2905
- type : 'date',
2906
- min : new Date(year, month, date)
2907
- }])).to.be.null;
2908
-
2909
- const error1 : EjvError = ejv(dateTestData, [{
2910
- key : 'date',
2911
- type : 'date',
2912
- min : new Date(year, month, date + 1)
2913
- }]);
2914
-
2915
- expect(error1).to.be.instanceof(EjvError);
2916
- expect(error1.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
2917
- expect(error1.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
2918
- .replace(ErrorMsgCursorA, ''));
2919
- expect(error1.path).to.be.eql('date');
2920
- expect(error1.data).to.be.deep.equal(dateTestData);
2921
- expect(error1.errorData).to.be.instanceof(Date);
2922
-
2923
- // with time
2924
- expect(ejv(dateTimeTestData, [{
2925
- key : 'date',
2926
- type : 'date',
2927
- min : new Date(year, month, date, hours, minutes, seconds, ms - 1)
2928
- }])).to.be.null;
2929
-
2930
- expect(ejv(dateTimeTestData, [{
2931
- key : 'date',
2932
- type : 'date',
2933
- min : new Date(year, month, date, hours, minutes, seconds, ms)
2934
- }])).to.be.null;
2935
-
2936
- const error2 : EjvError = ejv(dateTimeTestData, [{
2937
- key : 'date',
2938
- type : 'date',
2939
- min : new Date(year, month, date, hours, minutes, seconds, ms + 1)
2940
- }]);
2941
-
2942
- expect(error2).to.be.instanceof(EjvError);
2943
- expect(error2.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
2944
- expect(error2.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
2945
- .replace(ErrorMsgCursorA, ''));
2946
- expect(error2.path).to.be.eql('date');
2947
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
2948
- expect(error2.errorData).to.be.instanceof(Date);
2949
- });
2950
-
2951
- it('exclusiveMin === false', () => {
2952
- expect(ejv(dateTestData, [{
2953
- key : 'date',
2954
- type : 'date',
2955
- min : new Date(year, month, date - 1),
2956
- exclusiveMin : false
2957
- }])).to.be.null;
2958
-
2959
- expect(ejv(dateTestData, [{
2960
- key : 'date',
2961
- type : 'date',
2962
- min : new Date(year, month, date),
2963
- exclusiveMin : false
2964
- }])).to.be.null;
2965
-
2966
- const error1 : EjvError = ejv(dateTestData, [{
2967
- key : 'date',
2968
- type : 'date',
2969
- min : new Date(year, month, date + 1),
2970
- exclusiveMin : false
2971
- }]);
2972
-
2973
- expect(error1).to.be.instanceof(EjvError);
2974
- expect(error1.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
2975
- expect(error1.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
2976
- .replace(ErrorMsgCursorA, ''));
2977
- expect(error1.path).to.be.eql('date');
2978
- expect(error1.data).to.be.deep.equal(dateTestData);
2979
- expect(error1.errorData).to.be.instanceof(Date);
2980
-
2981
- // with time
2982
- expect(ejv(dateTimeTestData, [{
2983
- key : 'date',
2984
- type : 'date',
2985
- min : new Date(year, month, date, hours, minutes, seconds, ms - 1),
2986
- exclusiveMin : false
2987
- }])).to.be.null;
2988
-
2989
- expect(ejv(dateTimeTestData, [{
2990
- key : 'date',
2991
- type : 'date',
2992
- min : new Date(year, month, date, hours, minutes, seconds, ms),
2993
- exclusiveMin : false
2994
- }])).to.be.null;
2995
-
2996
- const error2 : EjvError = ejv(dateTimeTestData, [{
2997
- key : 'date',
2998
- type : 'date',
2999
- min : new Date(year, month, date, hours, minutes, seconds, ms + 1),
3000
- exclusiveMin : false
3001
- }]);
3002
-
3003
- expect(error2).to.be.instanceof(EjvError);
3004
- expect(error2.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
3005
- expect(error2.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
3006
- .replace(ErrorMsgCursorA, ''));
3007
- expect(error2.path).to.be.eql('date');
3008
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3009
- expect(error2.errorData).to.be.instanceof(Date);
3010
- });
3011
-
3012
- it('exclusiveMin === true', () => {
3013
- expect(ejv(dateTestData, [{
3014
- key : 'date',
3015
- type : 'date',
3016
- min : new Date(year, month, date - 1),
3017
- exclusiveMin : true
3018
- }])).to.be.null;
3019
-
3020
- const error1 : EjvError = ejv(dateTestData, [{
3021
- key : 'date',
3022
- type : 'date',
3023
- min : new Date(year, month, date),
3024
- exclusiveMin : true
3025
- }]);
3026
-
3027
- expect(error1).to.be.instanceof(EjvError);
3028
- expect(error1.type).to.be.eql(ErrorType.AFTER_DATE);
3029
- expect(error1.message).to.include(ErrorMsg.AFTER_DATE
3030
- .replace(ErrorMsgCursorA, ''));
3031
- expect(error1.path).to.be.eql('date');
3032
- expect(error1.data).to.be.deep.equal(dateTestData);
3033
- expect(error1.errorData).to.be.instanceof(Date);
3034
-
3035
- const error2 : EjvError = ejv(dateTestData, [{
3036
- key : 'date',
3037
- type : 'date',
3038
- min : new Date(year, month, date + 1),
3039
- exclusiveMin : true
3040
- }]);
3041
-
3042
- expect(error2).to.be.instanceof(EjvError);
3043
- expect(error2.type).to.be.eql(ErrorType.AFTER_DATE);
3044
- expect(error2.message).to.include(ErrorMsg.AFTER_DATE
3045
- .replace(ErrorMsgCursorA, ''));
3046
- expect(error2.path).to.be.eql('date');
3047
- expect(error2.data).to.be.deep.equal(dateTestData);
3048
- expect(error2.errorData).to.be.instanceof(Date);
3049
-
3050
- // with time
3051
- expect(ejv(dateTimeTestData, [{
3052
- key : 'date',
3053
- type : 'date',
3054
- min : new Date(year, month, date, hours, minutes, seconds, ms - 1),
3055
- exclusiveMin : true
3056
- }])).to.be.null;
3057
-
3058
- const error3 : EjvError = ejv(dateTimeTestData, [{
3059
- key : 'date',
3060
- type : 'date',
3061
- min : new Date(year, month, date, hours, minutes, seconds, ms),
3062
- exclusiveMin : true
3063
- }]);
3064
-
3065
- expect(error3).to.be.instanceof(EjvError);
3066
- expect(error3.type).to.be.eql(ErrorType.AFTER_DATE);
3067
- expect(error3.message).to.include(ErrorMsg.AFTER_DATE
3068
- .replace(ErrorMsgCursorA, ''));
3069
- expect(error3.path).to.be.eql('date');
3070
- expect(error3.data).to.be.deep.equal(dateTimeTestData);
3071
- expect(error3.errorData).to.be.instanceof(Date);
3072
-
3073
- const error4 : EjvError = ejv(dateTimeTestData, [{
3074
- key : 'date',
3075
- type : 'date',
3076
- min : new Date(year, month, date, hours, minutes, seconds, ms + 1),
3077
- exclusiveMin : true
3078
- }]);
3079
-
3080
- expect(error4).to.be.instanceof(EjvError);
3081
- expect(error4.type).to.be.eql(ErrorType.AFTER_DATE);
3082
- expect(error4.message).to.include(ErrorMsg.AFTER_DATE
3083
- .replace(ErrorMsgCursorA, ''));
3084
- expect(error4.path).to.be.eql('date');
3085
- expect(error4.data).to.be.deep.equal(dateTimeTestData);
3086
- expect(error4.errorData).to.be.instanceof(Date);
3087
- });
3088
- });
3089
-
3090
- describe('by date string', () => {
3091
- it('without exclusiveMin', () => {
3092
- expect(ejv(dateTestData, [{
3093
- key : 'date',
3094
- type : 'date',
3095
- min : getDateStr(year, month, date - 1)
3096
- }])).to.be.null;
3097
-
3098
- expect(ejv(dateTestData, [{
3099
- key : 'date',
3100
- type : 'date',
3101
- min : getDateStr(year, month, date)
3102
- }])).to.be.null;
3103
-
3104
- const error1 : EjvError = ejv(dateTestData, [{
3105
- key : 'date',
3106
- type : 'date',
3107
- min : getDateStr(year, month, date + 1)
3108
- }]);
3109
-
3110
- expect(error1).to.be.instanceof(EjvError);
3111
- expect(error1.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
3112
- expect(error1.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
3113
- .replace(ErrorMsgCursorA, ''));
3114
- expect(error1.path).to.be.eql('date');
3115
- expect(error1.data).to.be.deep.equal(dateTestData);
3116
- expect(error1.errorData).to.be.instanceof(Date);
3117
-
3118
- // with time
3119
- expect(ejv(dateTimeTestData, [{
3120
- key : 'date',
3121
- type : 'date',
3122
- min : getDateStr(year, month, date, hours, minutes, seconds, ms - 1)
3123
- }])).to.be.null;
3124
-
3125
- expect(ejv(dateTimeTestData, [{
3126
- key : 'date',
3127
- type : 'date',
3128
- min : getDateStr(year, month, date, hours, minutes, seconds, ms)
3129
- }])).to.be.null;
3130
-
3131
- const error2 : EjvError = ejv(dateTimeTestData, [{
3132
- key : 'date',
3133
- type : 'date',
3134
- min : getDateStr(year, month, date, hours, minutes, seconds, ms + 1)
3135
- }]);
3136
-
3137
- expect(error2).to.be.instanceof(EjvError);
3138
- expect(error2.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
3139
- expect(error2.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
3140
- .replace(ErrorMsgCursorA, ''));
3141
- expect(error2.path).to.be.eql('date');
3142
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3143
- expect(error2.errorData).to.be.instanceof(Date);
3144
- });
3145
-
3146
- it('exclusiveMin === false', () => {
3147
- expect(ejv(dateTestData, [{
3148
- key : 'date',
3149
- type : 'date',
3150
- min : getDateStr(year, month, date - 1),
3151
- exclusiveMin : false
3152
- }])).to.be.null;
3153
-
3154
- expect(ejv(dateTestData, [{
3155
- key : 'date',
3156
- type : 'date',
3157
- min : getDateStr(year, month, date),
3158
- exclusiveMin : false
3159
- }])).to.be.null;
3160
-
3161
- const error1 : EjvError = ejv(dateTestData, [{
3162
- key : 'date',
3163
- type : 'date',
3164
- min : getDateStr(year, month, date + 1),
3165
- exclusiveMin : false
3166
- }]);
3167
-
3168
- expect(error1).to.be.instanceof(EjvError);
3169
- expect(error1.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
3170
- expect(error1.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
3171
- .replace(ErrorMsgCursorA, ''));
3172
- expect(error1.path).to.be.eql('date');
3173
- expect(error1.data).to.be.deep.equal(dateTestData);
3174
- expect(error1.errorData).to.be.instanceof(Date);
3175
-
3176
- // with time
3177
- expect(ejv(dateTimeTestData, [{
3178
- key : 'date',
3179
- type : 'date',
3180
- min : getDateStr(year, month, date, hours, minutes, seconds, ms - 1),
3181
- exclusiveMin : false
3182
- }])).to.be.null;
3183
-
3184
- expect(ejv(dateTimeTestData, [{
3185
- key : 'date',
3186
- type : 'date',
3187
- min : getDateStr(year, month, date, hours, minutes, seconds, ms),
3188
- exclusiveMin : false
3189
- }])).to.be.null;
3190
-
3191
- const error2 : EjvError = ejv(dateTimeTestData, [{
3192
- key : 'date',
3193
- type : 'date',
3194
- min : getDateStr(year, month, date, hours, minutes, seconds, ms + 1),
3195
- exclusiveMin : false
3196
- }]);
3197
-
3198
- expect(error2).to.be.instanceof(EjvError);
3199
- expect(error2.type).to.be.eql(ErrorType.AFTER_OR_SAME_DATE);
3200
- expect(error2.message).to.include(ErrorMsg.AFTER_OR_SAME_DATE
3201
- .replace(ErrorMsgCursorA, ''));
3202
- expect(error2.path).to.be.eql('date');
3203
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3204
- expect(error2.errorData).to.be.instanceof(Date);
3205
- });
3206
-
3207
- it('exclusiveMin === true', () => {
3208
- expect(ejv(dateTestData, [{
3209
- key : 'date',
3210
- type : 'date',
3211
- min : getDateStr(year, month, date - 1),
3212
- exclusiveMin : true
3213
- }])).to.be.null;
3214
-
3215
- const error1 : EjvError = ejv(dateTestData, [{
3216
- key : 'date',
3217
- type : 'date',
3218
- min : getDateStr(year, month, date),
3219
- exclusiveMin : true
3220
- }]);
3221
-
3222
- expect(error1).to.be.instanceof(EjvError);
3223
- expect(error1.type).to.be.eql(ErrorType.AFTER_DATE);
3224
- expect(error1.message).to.include(ErrorMsg.AFTER_DATE
3225
- .replace(ErrorMsgCursorA, ''));
3226
- expect(error1.path).to.be.eql('date');
3227
- expect(error1.data).to.be.deep.equal(dateTestData);
3228
- expect(error1.errorData).to.be.instanceof(Date);
3229
-
3230
- const error2 : EjvError = ejv(dateTestData, [{
3231
- key : 'date',
3232
- type : 'date',
3233
- min : getDateStr(year, month, date + 1),
3234
- exclusiveMin : true
3235
- }]);
3236
-
3237
- expect(error2).to.be.instanceof(EjvError);
3238
- expect(error2.type).to.be.eql(ErrorType.AFTER_DATE);
3239
- expect(error2.message).to.include(ErrorMsg.AFTER_DATE
3240
- .replace(ErrorMsgCursorA, ''));
3241
- expect(error2.path).to.be.eql('date');
3242
- expect(error2.data).to.be.deep.equal(dateTestData);
3243
- expect(error2.errorData).to.be.instanceof(Date);
3244
-
3245
- // with time
3246
- expect(ejv(dateTimeTestData, [{
3247
- key : 'date',
3248
- type : 'date',
3249
- min : getDateStr(year, month, date, hours, minutes, seconds, ms - 1),
3250
- exclusiveMin : true
3251
- }])).to.be.null;
3252
-
3253
- const error3 : EjvError = ejv(dateTimeTestData, [{
3254
- key : 'date',
3255
- type : 'date',
3256
- min : getDateStr(year, month, date, hours, minutes, seconds, ms),
3257
- exclusiveMin : true
3258
- }]);
3259
-
3260
- expect(error3).to.be.instanceof(EjvError);
3261
- expect(error3.type).to.be.eql(ErrorType.AFTER_DATE);
3262
- expect(error3.message).to.include(ErrorMsg.AFTER_DATE
3263
- .replace(ErrorMsgCursorA, ''));
3264
- expect(error3.path).to.be.eql('date');
3265
- expect(error3.data).to.be.deep.equal(dateTimeTestData);
3266
- expect(error3.errorData).to.be.instanceof(Date);
3267
-
3268
- const error4 : EjvError = ejv(dateTimeTestData, [{
3269
- key : 'date',
3270
- type : 'date',
3271
- min : getDateStr(year, month, date, hours, minutes, seconds, ms + 1),
3272
- exclusiveMin : true
3273
- }]);
3274
-
3275
- expect(error4).to.be.instanceof(EjvError);
3276
- expect(error4.type).to.be.eql(ErrorType.AFTER_DATE);
3277
- expect(error4.message).to.include(ErrorMsg.AFTER_DATE
3278
- .replace(ErrorMsgCursorA, ''));
3279
- expect(error4.path).to.be.eql('date');
3280
- expect(error4.data).to.be.deep.equal(dateTimeTestData);
3281
- expect(error4.errorData).to.be.instanceof(Date);
3282
- });
3283
- });
3284
- });
3285
-
3286
- describe('max & exclusiveMax', () => {
3287
- describe('check parameter', () => {
3288
- it('max === null', () => {
3289
- expect(() => ejv(dateTestData, [{
3290
- key : 'date',
3291
- type : 'date',
3292
- max : null
3293
- }])).to.throw(ErrorMsg.MAX_DATE_SHOULD_BE_DATE_OR_STRING);
3294
- });
3295
-
3296
- it('exclusiveMax === null', () => {
3297
- expect(() => ejv(dateTestData, [{
3298
- key : 'date',
3299
- type : 'date',
3300
- max : new Date,
3301
- exclusiveMax : null
3302
- }])).to.throw(ErrorMsg.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN);
3303
- });
3304
- });
3305
-
3306
- describe('by date', () => {
3307
- it('without exclusiveMax', () => {
3308
- const error1 : EjvError = ejv(dateTestData, [{
3309
- key : 'date',
3310
- type : 'date',
3311
- max : new Date(year, month, date - 1)
3312
- }]);
3313
-
3314
- expect(error1).to.be.instanceof(EjvError);
3315
- expect(error1.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3316
- expect(error1.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3317
- .replace(ErrorMsgCursorA, ''));
3318
- expect(error1.path).to.be.eql('date');
3319
- expect(error1.data).to.be.deep.equal(dateTestData);
3320
- expect(error1.errorData).to.be.instanceof(Date);
3321
-
3322
- expect(ejv(dateTestData, [{
3323
- key : 'date',
3324
- type : 'date',
3325
- max : new Date(year, month, date)
3326
- }])).to.be.null;
3327
-
3328
- expect(ejv(dateTestData, [{
3329
- key : 'date',
3330
- type : 'date',
3331
- max : new Date(year, month, date + 1)
3332
- }])).to.be.null;
3333
-
3334
- // with time
3335
- const error2 : EjvError = ejv(dateTimeTestData, [{
3336
- key : 'date',
3337
- type : 'date',
3338
- max : new Date(year, month, date, hours, minutes, seconds, ms - 1)
3339
- }]);
3340
-
3341
- expect(error2).to.be.instanceof(EjvError);
3342
- expect(error2.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3343
- expect(error2.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3344
- .replace(ErrorMsgCursorA, ''));
3345
- expect(error2.path).to.be.eql('date');
3346
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3347
- expect(error2.errorData).to.be.instanceof(Date);
3348
-
3349
- expect(ejv(dateTimeTestData, [{
3350
- key : 'date',
3351
- type : 'date',
3352
- max : new Date(year, month, date, hours, minutes, seconds, ms)
3353
- }])).to.be.null;
3354
-
3355
- expect(ejv(dateTimeTestData, [{
3356
- key : 'date',
3357
- type : 'date',
3358
- max : new Date(year, month, date, hours, minutes, seconds, ms + 1)
3359
- }])).to.be.null;
3360
- });
3361
-
3362
- it('exclusiveMax === false', () => {
3363
- const error1 : EjvError = ejv(dateTestData, [{
3364
- key : 'date',
3365
- type : 'date',
3366
- max : new Date(year, month, date - 1),
3367
- exclusiveMax : false
3368
- }]);
3369
-
3370
- expect(error1).to.be.instanceof(EjvError);
3371
- expect(error1.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3372
- expect(error1.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3373
- .replace(ErrorMsgCursorA, ''));
3374
- expect(error1.path).to.be.eql('date');
3375
- expect(error1.data).to.be.deep.equal(dateTestData);
3376
- expect(error1.errorData).to.be.instanceof(Date);
3377
-
3378
- expect(ejv(dateTestData, [{
3379
- key : 'date',
3380
- type : 'date',
3381
- max : new Date(year, month, date),
3382
- exclusiveMax : false
3383
- }])).to.be.null;
3384
-
3385
- expect(ejv(dateTestData, [{
3386
- key : 'date',
3387
- type : 'date',
3388
- max : new Date(year, month, date + 1),
3389
- exclusiveMax : false
3390
- }])).to.be.null;
3391
-
3392
- // with time
3393
- const error2 : EjvError = ejv(dateTimeTestData, [{
3394
- key : 'date',
3395
- type : 'date',
3396
- max : new Date(year, month, date, hours, minutes, seconds, ms - 1),
3397
- exclusiveMax : false
3398
- }]);
3399
-
3400
- expect(error2).to.be.instanceof(EjvError);
3401
- expect(error2.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3402
- expect(error2.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3403
- .replace(ErrorMsgCursorA, ''));
3404
- expect(error2.path).to.be.eql('date');
3405
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3406
- expect(error2.errorData).to.be.instanceof(Date);
3407
-
3408
- expect(ejv(dateTimeTestData, [{
3409
- key : 'date',
3410
- type : 'date',
3411
- max : new Date(year, month, date, hours, minutes, seconds, ms),
3412
- exclusiveMax : false
3413
- }])).to.be.null;
3414
-
3415
- expect(ejv(dateTimeTestData, [{
3416
- key : 'date',
3417
- type : 'date',
3418
- max : new Date(year, month, date, hours, minutes, seconds, ms + 1),
3419
- exclusiveMax : false
3420
- }])).to.be.null;
3421
- });
3422
-
3423
- it('exclusiveMax === true', () => {
3424
- const error1 : EjvError = ejv(dateTestData, [{
3425
- key : 'date',
3426
- type : 'date',
3427
- max : new Date(year, month, date - 1),
3428
- exclusiveMax : true
3429
- }]);
3430
-
3431
- expect(error1).to.be.instanceof(EjvError);
3432
- expect(error1.type).to.be.eql(ErrorType.BEFORE_DATE);
3433
- expect(error1.message).to.include(ErrorMsg.BEFORE_DATE
3434
- .replace(ErrorMsgCursorA, ''));
3435
- expect(error1.path).to.be.eql('date');
3436
- expect(error1.data).to.be.deep.equal(dateTestData);
3437
- expect(error1.errorData).to.be.instanceof(Date);
3438
-
3439
- const error2 : EjvError = ejv(dateTestData, [{
3440
- key : 'date',
3441
- type : 'date',
3442
- max : new Date(year, month, date),
3443
- exclusiveMax : true
3444
- }]);
3445
-
3446
- expect(error2).to.be.instanceof(EjvError);
3447
- expect(error2.type).to.be.eql(ErrorType.BEFORE_DATE);
3448
- expect(error2.message).to.include(ErrorMsg.BEFORE_DATE
3449
- .replace(ErrorMsgCursorA, ''));
3450
- expect(error2.path).to.be.eql('date');
3451
- expect(error2.data).to.be.deep.equal(dateTestData);
3452
- expect(error2.errorData).to.be.instanceof(Date);
3453
-
3454
- expect(ejv(dateTestData, [{
3455
- key : 'date',
3456
- type : 'date',
3457
- max : new Date(year, month, date + 1),
3458
- exclusiveMax : true
3459
- }])).to.be.null;
3460
-
3461
- // with time
3462
- const error3 : EjvError = ejv(dateTimeTestData, [{
3463
- key : 'date',
3464
- type : 'date',
3465
- max : new Date(year, month, date, hours, minutes, seconds, ms - 1),
3466
- exclusiveMax : true
3467
- }]);
3468
-
3469
- expect(error3).to.be.instanceof(EjvError);
3470
- expect(error3.type).to.be.eql(ErrorType.BEFORE_DATE);
3471
- expect(error3.message).to.include(ErrorMsg.BEFORE_DATE
3472
- .replace(ErrorMsgCursorA, ''));
3473
- expect(error3.path).to.be.eql('date');
3474
- expect(error3.data).to.be.deep.equal(dateTimeTestData);
3475
- expect(error3.errorData).to.be.instanceof(Date);
3476
-
3477
- const error4 : EjvError = ejv(dateTimeTestData, [{
3478
- key : 'date',
3479
- type : 'date',
3480
- max : new Date(year, month, date, hours, minutes, seconds, ms),
3481
- exclusiveMax : true
3482
- }]);
3483
-
3484
- expect(error4).to.be.instanceof(EjvError);
3485
- expect(error4.type).to.be.eql(ErrorType.BEFORE_DATE);
3486
- expect(error4.message).to.include(ErrorMsg.BEFORE_DATE
3487
- .replace(ErrorMsgCursorA, ''));
3488
- expect(error4.path).to.be.eql('date');
3489
- expect(error4.data).to.be.deep.equal(dateTimeTestData);
3490
- expect(error4.errorData).to.be.instanceof(Date);
3491
-
3492
- expect(ejv(dateTimeTestData, [{
3493
- key : 'date',
3494
- type : 'date',
3495
- max : new Date(year, month, date, hours, minutes, seconds, ms + 1),
3496
- exclusiveMax : true
3497
- }])).to.be.null;
3498
- });
3499
- });
3500
-
3501
- describe('by date string', () => {
3502
- it('without exclusiveMax', () => {
3503
- const error1 : EjvError = ejv(dateTestData, [{
3504
- key : 'date',
3505
- type : 'date',
3506
- max : getDateStr(year, month, date - 1)
3507
- }]);
3508
-
3509
- expect(error1).to.be.instanceof(EjvError);
3510
- expect(error1.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3511
- expect(error1.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3512
- .replace(ErrorMsgCursorA, ''));
3513
- expect(error1.path).to.be.eql('date');
3514
- expect(error1.data).to.be.deep.equal(dateTestData);
3515
- expect(error1.errorData).to.be.instanceof(Date);
3516
-
3517
- expect(ejv(dateTestData, [{
3518
- key : 'date',
3519
- type : 'date',
3520
- max : getDateStr(year, month, date)
3521
- }])).to.be.null;
3522
-
3523
- expect(ejv(dateTestData, [{
3524
- key : 'date',
3525
- type : 'date',
3526
- max : getDateStr(year, month, date + 1)
3527
- }])).to.be.null;
3528
-
3529
- // with time
3530
- const error2 : EjvError = ejv(dateTimeTestData, [{
3531
- key : 'date',
3532
- type : 'date',
3533
- max : getDateStr(year, month, date, hours, minutes, seconds, ms - 1)
3534
- }]);
3535
-
3536
- expect(error2).to.be.instanceof(EjvError);
3537
- expect(error2.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3538
- expect(error2.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3539
- .replace(ErrorMsgCursorA, ''));
3540
- expect(error2.path).to.be.eql('date');
3541
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3542
- expect(error2.errorData).to.be.instanceof(Date);
3543
-
3544
- expect(ejv(dateTimeTestData, [{
3545
- key : 'date',
3546
- type : 'date',
3547
- max : getDateStr(year, month, date, hours, minutes, seconds, ms)
3548
- }])).to.be.null;
3549
-
3550
- expect(ejv(dateTimeTestData, [{
3551
- key : 'date',
3552
- type : 'date',
3553
- max : getDateStr(year, month, date, hours, minutes, seconds, ms + 1)
3554
- }])).to.be.null;
3555
- });
3556
-
3557
- it('exclusiveMax === false', () => {
3558
- const error1 : EjvError = ejv(dateTestData, [{
3559
- key : 'date',
3560
- type : 'date',
3561
- max : getDateStr(year, month, date - 1),
3562
- exclusiveMax : false
3563
- }]);
3564
-
3565
- expect(error1).to.be.instanceof(EjvError);
3566
- expect(error1.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3567
- expect(error1.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3568
- .replace(ErrorMsgCursorA, ''));
3569
- expect(error1.path).to.be.eql('date');
3570
- expect(error1.data).to.be.deep.equal(dateTestData);
3571
- expect(error1.errorData).to.be.instanceof(Date);
3572
-
3573
- expect(ejv(dateTestData, [{
3574
- key : 'date',
3575
- type : 'date',
3576
- max : getDateStr(year, month, date),
3577
- exclusiveMax : false
3578
- }])).to.be.null;
3579
-
3580
- expect(ejv(dateTestData, [{
3581
- key : 'date',
3582
- type : 'date',
3583
- max : getDateStr(year, month, date + 1),
3584
- exclusiveMax : false
3585
- }])).to.be.null;
3586
-
3587
- // with time
3588
- const error2 : EjvError = ejv(dateTimeTestData, [{
3589
- key : 'date',
3590
- type : 'date',
3591
- max : getDateStr(year, month, date, hours, minutes, seconds, ms - 1),
3592
- exclusiveMax : false
3593
- }]);
3594
-
3595
- expect(error2).to.be.instanceof(EjvError);
3596
- expect(error2.type).to.be.eql(ErrorType.BEFORE_OR_SAME_DATE);
3597
- expect(error2.message).to.include(ErrorMsg.BEFORE_OR_SAME_DATE
3598
- .replace(ErrorMsgCursorA, ''));
3599
- expect(error2.path).to.be.eql('date');
3600
- expect(error2.data).to.be.deep.equal(dateTimeTestData);
3601
- expect(error2.errorData).to.be.instanceof(Date);
3602
-
3603
- expect(ejv(dateTimeTestData, [{
3604
- key : 'date',
3605
- type : 'date',
3606
- max : getDateStr(year, month, date, hours, minutes, seconds, ms),
3607
- exclusiveMax : false
3608
- }])).to.be.null;
3609
-
3610
- expect(ejv(dateTimeTestData, [{
3611
- key : 'date',
3612
- type : 'date',
3613
- max : getDateStr(year, month, date, hours, minutes, seconds, ms + 1),
3614
- exclusiveMax : false
3615
- }])).to.be.null;
3616
- });
3617
-
3618
- it('exclusiveMax === true', () => {
3619
- const error1 : EjvError = ejv(dateTestData, [{
3620
- key : 'date',
3621
- type : 'date',
3622
- max : getDateStr(year, month, date - 1),
3623
- exclusiveMax : true
3624
- }]);
3625
-
3626
- expect(error1).to.be.instanceof(EjvError);
3627
- expect(error1.type).to.be.eql(ErrorType.BEFORE_DATE);
3628
- expect(error1.message).to.include(ErrorMsg.BEFORE_DATE
3629
- .replace(ErrorMsgCursorA, ''));
3630
- expect(error1.path).to.be.eql('date');
3631
- expect(error1.data).to.be.deep.equal(dateTestData);
3632
- expect(error1.errorData).to.be.instanceof(Date);
3633
-
3634
- const error2 : EjvError = ejv(dateTestData, [{
3635
- key : 'date',
3636
- type : 'date',
3637
- max : getDateStr(year, month, date),
3638
- exclusiveMax : true
3639
- }]);
3640
-
3641
- expect(error2).to.be.instanceof(EjvError);
3642
- expect(error2.message).to.include(ErrorMsg.BEFORE_DATE
3643
- .replace(ErrorMsgCursorA, ''));
3644
- expect(error2.path).to.be.eql('date');
3645
- expect(error2.data).to.be.deep.equal(dateTestData);
3646
- expect(error2.errorData).to.be.instanceof(Date);
3647
-
3648
- expect(ejv(dateTestData, [{
3649
- key : 'date',
3650
- type : 'date',
3651
- max : getDateStr(year, month, date + 1),
3652
- exclusiveMax : true
3653
- }])).to.be.null;
3654
-
3655
- // with time
3656
- const error3 : EjvError = ejv(dateTimeTestData, [{
3657
- key : 'date',
3658
- type : 'date',
3659
- max : getDateStr(year, month, date, hours, minutes, seconds, ms - 1),
3660
- exclusiveMax : true
3661
- }]);
3662
-
3663
- expect(error3).to.be.instanceof(EjvError);
3664
- expect(error3.type).to.be.eql(ErrorType.BEFORE_DATE);
3665
- expect(error3.message).to.include(ErrorMsg.BEFORE_DATE
3666
- .replace(ErrorMsgCursorA, ''));
3667
- expect(error3.path).to.be.eql('date');
3668
- expect(error3.data).to.be.deep.equal(dateTimeTestData);
3669
- expect(error3.errorData).to.be.instanceof(Date);
3670
-
3671
- const error4 : EjvError = ejv(dateTimeTestData, [{
3672
- key : 'date',
3673
- type : 'date',
3674
- max : getDateStr(year, month, date, hours, minutes, seconds, ms),
3675
- exclusiveMax : true
3676
- }]);
3677
-
3678
- expect(error4).to.be.instanceof(EjvError);
3679
- expect(error4.type).to.be.eql(ErrorType.BEFORE_DATE);
3680
- expect(error4.message).to.include(ErrorMsg.BEFORE_DATE
3681
- .replace(ErrorMsgCursorA, ''));
3682
- expect(error4.path).to.be.eql('date');
3683
- expect(error4.data).to.be.deep.equal(dateTimeTestData);
3684
- expect(error4.errorData).to.be.instanceof(Date);
3685
-
3686
- expect(ejv(dateTimeTestData, [{
3687
- key : 'date',
3688
- type : 'date',
3689
- max : getDateStr(year, month, date, hours, minutes, seconds, ms + 1),
3690
- exclusiveMax : true
3691
- }])).to.be.null;
3692
- });
3693
- });
3694
- });
3695
- });
3696
-
3697
- describe('regexp', () => {
3698
- describe('type', () => {
3699
- describe('mismatch', () => {
3700
- typeTester.filter(obj => obj.type !== 'regexp')
3701
- .forEach((obj) => {
3702
- it(obj.type, () => {
3703
- const testData = {
3704
- a : obj.value
3705
- };
3706
-
3707
- const error : EjvError = ejv(testData, [{
3708
- key : 'a',
3709
- type : 'regexp'
3710
- }]);
3711
-
3712
- expect(error).to.be.instanceof(EjvError);
3713
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
3714
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
3715
- .replace(ErrorMsgCursorA, 'regexp')
3716
- );
3717
- expect(error.path).to.be.eql('a');
3718
- expect(error.data).to.be.deep.equal(testData);
3719
- expect(error.errorData).to.be.eql(obj.value);
3720
- });
3721
- });
3722
-
3723
- it('multiple types', () => {
3724
- const value = 'ejv';
3725
- const typeArr : string[] = ['boolean', 'regexp'];
3726
-
3727
- const testData = {
3728
- a : value
3729
- };
3730
-
3731
- const error : EjvError = ejv(testData, [{
3732
- key : 'a',
3733
- type : typeArr
3734
- }]);
3735
-
3736
- expect(error).to.be.instanceof(EjvError);
3737
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
3738
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
3739
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
3740
- expect(error.path).to.be.eql('a');
3741
- expect(error.data).to.be.deep.equal(testData);
3742
- expect(error.errorData).to.be.eql(value);
3743
- });
3744
- });
3745
-
3746
- describe('match', () => {
3747
- it('optional', () => {
3748
- expect(ejv({
3749
- a : undefined
3750
- }, [{
3751
- key : 'a',
3752
- type : 'regexp',
3753
- optional : true
3754
- }])).to.be.null;
3755
- });
3756
-
3757
- it('single type', () => {
3758
- expect(ejv({
3759
- a : /./
3760
- }, [{
3761
- key : 'a',
3762
- type : 'regexp'
3763
- }])).to.be.null;
3764
- });
3765
-
3766
- it('multiple types', () => {
3767
- expect(ejv({
3768
- a : /./
3769
- }, [{
3770
- key : 'a',
3771
- type : ['regexp', 'number']
3772
- }])).to.be.null;
3773
- });
3774
-
3775
- it('multiple types', () => {
3776
- expect(ejv({
3777
- a : /./
3778
- }, [{
3779
- key : 'a',
3780
- type : ['number', 'regexp']
3781
- }])).to.be.null;
3782
- });
3783
- });
3784
- });
3785
- });
3786
-
3787
- describe('array', () => {
3788
- describe('type', () => {
3789
- describe('mismatch', () => {
3790
- typeTester.filter(obj => obj.type !== 'array')
3791
- .forEach((obj) => {
3792
- it(obj.type, () => {
3793
- const testObj = {
3794
- a : obj.value
3795
- };
3796
-
3797
- const error : EjvError = ejv(testObj, [{
3798
- key : 'a',
3799
- type : 'array'
3800
- }]);
3801
-
3802
- expect(error).to.be.instanceof(EjvError);
3803
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
3804
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
3805
- .replace(ErrorMsgCursorA, 'array')
3806
- );
3807
- expect(error.path).to.be.eql('a');
3808
- expect(error.data).to.be.eql(testObj);
3809
- expect(error.errorData).to.be.eql(obj.value);
3810
- });
3811
- });
3812
-
3813
- it('multiple types', () => {
3814
- const value = 'ejv';
3815
- const typeArr : string[] = ['boolean', 'array'];
3816
-
3817
- const testObj = {
3818
- a : value
3819
- };
3820
-
3821
- const error : EjvError = ejv(testObj, [{
3822
- key : 'a',
3823
- type : typeArr
3824
- }]);
3825
-
3826
- expect(error).to.be.instanceof(EjvError);
3827
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
3828
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH_ONE_OF
3829
- .replace(ErrorMsgCursorA, JSON.stringify(typeArr)));
3830
- expect(error.path).to.be.eql('a');
3831
- expect(error.data).to.be.eql(testObj);
3832
- expect(error.errorData).to.be.eql(value);
3833
- });
3834
- });
3835
-
3836
- describe('match', () => {
3837
- it('optional', () => {
3838
- expect(ejv({
3839
- a : undefined
3840
- }, [{
3841
- key : 'a',
3842
- type : 'array',
3843
- optional : true
3844
- }])).to.be.null;
3845
- });
3846
-
3847
- it('single type', () => {
3848
- expect(ejv({
3849
- a : [1, 2, 3]
3850
- }, [{
3851
- key : 'a',
3852
- type : 'array'
3853
- }])).to.be.null;
3854
- });
3855
-
3856
- it('multiple types', () => {
3857
- expect(ejv({
3858
- a : [1, 2, 3]
3859
- }, [{
3860
- key : 'a',
3861
- type : ['array', 'number']
3862
- }])).to.be.null;
3863
- });
3864
-
3865
- it('multiple types', () => {
3866
- expect(ejv({
3867
- a : [1, 2, 3]
3868
- }, [{
3869
- key : 'a',
3870
- type : ['number', 'array']
3871
- }])).to.be.null;
3872
- });
3873
- });
3874
-
3875
- describe('has invalid type', () => {
3876
- it('has undefined', () => {
3877
- const value = ['a', undefined];
3878
- const testObj = {
3879
- a : value
3880
- };
3881
-
3882
- const error : EjvError = ejv(testObj, [
3883
- { key : 'a', type : 'array', items : 'string' }
3884
- ]);
3885
-
3886
- expect(error).to.be.instanceof(EjvError);
3887
- expect(error.type).to.be.eql(ErrorType.ITEMS_TYPE);
3888
- expect(error.message).to.be.eql(ErrorMsg.ITEMS_TYPE
3889
- .replace(ErrorMsgCursorA, 'string'));
3890
- expect(error.path).to.be.eql('a/1');
3891
- expect(error.data).to.be.eql(testObj);
3892
- expect(error.errorData).to.be.eql(undefined);
3893
- });
3894
-
3895
- it('has undefined, but optional', () => {
3896
- const value = ['a', undefined];
3897
- const testObj = {
3898
- a : value
3899
- };
3900
-
3901
- expect(ejv(testObj, [
3902
- {
3903
- key : 'a', type : 'array', items : [
3904
- { type : 'string', optional : true }
3905
- ]
3906
- }
3907
- ])).to.be.null;
3908
- });
3909
-
3910
- it('has null', () => {
3911
- const value = ['a', null];
3912
- const testObj = {
3913
- a : value
3914
- };
3915
-
3916
- const error : EjvError = ejv(testObj, [
3917
- { key : 'a', type : 'array', items : 'string' }
3918
- ]);
3919
-
3920
- expect(error).to.be.instanceof(EjvError);
3921
- expect(error.type).to.be.eql(ErrorType.ITEMS_TYPE);
3922
- expect(error.message).to.be.eql(ErrorMsg.ITEMS_TYPE
3923
- .replace(ErrorMsgCursorA, 'string'));
3924
- expect(error.path).to.be.eql('a/1');
3925
- expect(error.data).to.be.eql(testObj);
3926
- expect(error.errorData).to.be.eql(null);
3927
- });
3928
-
3929
- it('has null, but nullable', () => {
3930
- const value = ['a', null];
3931
-
3932
- expect(ejv({
3933
- a : value
3934
- }, [
3935
- {
3936
- key : 'a', type : 'array', items : [
3937
- { type : 'string', nullable : true }
3938
- ]
3939
- }
3940
- ])).to.be.null;
3941
- });
3942
- });
3943
- });
3944
-
3945
- describe('minLength', () => {
3946
- describe('check parameter', () => {
3947
- it('undefined is ok', () => {
3948
- expect(ejv({
3949
- a : [1, 2, 3]
3950
- }, [{
3951
- key : 'a',
3952
- type : 'array',
3953
- minLength : undefined
3954
- }])).to.be.null;
3955
- });
3956
-
3957
- it('null', () => {
3958
- expect(() => ejv({
3959
- a : [1, 2, 3]
3960
- }, [{
3961
- key : 'a',
3962
- type : 'array',
3963
- minLength : null
3964
- }])).to.throw(ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
3965
- });
3966
-
3967
- it('float number', () => {
3968
- expect(() => ejv({
3969
- a : 'a'
3970
- }, [{
3971
- key : 'a',
3972
- type : 'string',
3973
- minLength : 1.5
3974
- }])).to.throw(ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
3975
- });
3976
-
3977
- it('string', () => {
3978
- expect(() => ejv({
3979
- a : [1, 2, 3]
3980
- }, [{
3981
- key : 'a',
3982
- type : 'array',
3983
- minLength : '1' as any
3984
- }])).to.throw(ErrorMsg.MIN_LENGTH_SHOULD_BE_INTEGER);
3985
- });
3986
- });
3987
-
3988
- it('fail', () => {
3989
- const value = [1, 2, 3];
3990
- const testData = {
3991
- a : value
3992
- };
3993
-
3994
- const error : EjvError = ejv(testData, [{
3995
- key : 'a',
3996
- type : 'array',
3997
- minLength : 4
3998
- }]);
3999
-
4000
- expect(error).to.be.instanceof(EjvError);
4001
- expect(error.type).to.be.eql(ErrorType.MIN_LENGTH);
4002
- expect(error.message).to.be.eql(ErrorMsg.MIN_LENGTH
4003
- .replace(ErrorMsgCursorA, '4'));
4004
- expect(error.path).to.be.eql('a');
4005
- expect(error.data).to.be.deep.equal(testData);
4006
- expect(error.errorData).to.be.ordered.members(value);
4007
- });
4008
-
4009
- it('ok', () => {
4010
- expect(ejv({
4011
- a : [1, 2, 3]
4012
- }, [{
4013
- key : 'a',
4014
- type : 'array',
4015
- minLength : 2
4016
- }])).to.be.null;
4017
-
4018
- expect(ejv({
4019
- a : [1, 2, 3]
4020
- }, [{
4021
- key : 'a',
4022
- type : 'array',
4023
- minLength : 3
4024
- }])).to.be.null;
4025
- });
4026
- });
4027
-
4028
- describe('maxLength', () => {
4029
- describe('check parameter', () => {
4030
- it('undefined is ok', () => {
4031
- expect(ejv({
4032
- a : [1, 2, 3]
4033
- }, [{
4034
- key : 'a',
4035
- type : 'array',
4036
- maxLength : undefined
4037
- }])).to.be.null;
4038
- });
4039
-
4040
- it('null', () => {
4041
- expect(() => ejv({
4042
- a : [1, 2, 3]
4043
- }, [{
4044
- key : 'a',
4045
- type : 'array',
4046
- maxLength : null
4047
- }])).to.throw(ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
4048
- });
4049
-
4050
- it('float number', () => {
4051
- expect(() => ejv({
4052
- a : 'a'
4053
- }, [{
4054
- key : 'a',
4055
- type : 'string',
4056
- maxLength : 1.5
4057
- }])).to.throw(ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
4058
- });
4059
-
4060
- it('string', () => {
4061
- expect(() => ejv({
4062
- a : [1, 2, 3]
4063
- }, [{
4064
- key : 'a',
4065
- type : 'array',
4066
- maxLength : '1' as any
4067
- }])).to.throw(ErrorMsg.MAX_LENGTH_SHOULD_BE_INTEGER);
4068
- });
4069
- });
4070
-
4071
- it('fail', () => {
4072
- const value = [1, 2, 3];
4073
- const testData = {
4074
- a : value
4075
- };
4076
-
4077
- const error : EjvError = ejv(testData, [{
4078
- key : 'a',
4079
- type : 'array',
4080
- maxLength : 2
4081
- }]);
4082
-
4083
- expect(error).to.be.instanceof(EjvError);
4084
- expect(error.type).to.be.eql(ErrorType.MAX_LENGTH);
4085
- expect(error.message).to.be.eql(ErrorMsg.MAX_LENGTH
4086
- .replace(ErrorMsgCursorA, '2'));
4087
- expect(error.path).to.be.eql('a');
4088
- expect(error.data).to.be.deep.equal(testData);
4089
- expect(error.errorData).to.be.ordered.members(value);
4090
- });
4091
-
4092
- it('ok', () => {
4093
- expect(ejv({
4094
- a : [1, 2, 3]
4095
- }, [{
4096
- key : 'a',
4097
- type : 'array',
4098
- maxLength : 3
4099
- }])).to.be.null;
4100
-
4101
- expect(ejv({
4102
- a : [1, 2, 3]
4103
- }, [{
4104
- key : 'a',
4105
- type : 'array',
4106
- maxLength : 4
4107
- }])).to.be.null;
4108
- });
4109
- });
4110
-
4111
- describe('unique', () => {
4112
- describe('check parameter', () => {
4113
- it('undefined is ok', () => {
4114
- expect(ejv({
4115
- a : [1, 2, 3]
4116
- }, [{
4117
- key : 'a',
4118
- type : 'array',
4119
- unique : undefined
4120
- }])).to.be.null;
4121
- });
4122
-
4123
- it('null', () => {
4124
- expect(() => ejv({
4125
- a : [1, 2, 3]
4126
- }, [{
4127
- key : 'a',
4128
- type : 'array',
4129
- unique : null
4130
- }])).to.throw(ErrorMsg.UNIQUE_SHOULD_BE_BOOLEAN);
4131
- });
4132
-
4133
- it('not boolean', () => {
4134
- expect(() => ejv({
4135
- a : [1, 2, 3]
4136
- }, [{
4137
- key : 'a',
4138
- type : 'array',
4139
- unique : 'hello' as any
4140
- }])).to.throw(ErrorMsg.UNIQUE_SHOULD_BE_BOOLEAN);
4141
- });
4142
- });
4143
-
4144
- it('default', () => {
4145
- const error : EjvError = ejv({
4146
- a : [1, 2, 3],
4147
- b : [1, 1, 1],
4148
- c : ['a', 'a', 'a']
4149
- }, [{
4150
- key : 'a',
4151
- type : 'array'
4152
- }, {
4153
- key : 'b',
4154
- type : 'array'
4155
- }, {
4156
- key : 'c',
4157
- type : 'array',
4158
- items : {
4159
- type : 'string',
4160
- minLength : 1
4161
- }
4162
- }]);
4163
-
4164
- expect(error).to.be.null;
4165
- });
4166
-
4167
- it('false', () => {
4168
- const error : EjvError = ejv({
4169
- a : [1, 2, 3],
4170
- b : [1, 1, 1],
4171
- c : ['a', 'a', 'a']
4172
- }, [{
4173
- key : 'a',
4174
- type : 'array',
4175
- unique : false
4176
- }, {
4177
- key : 'b',
4178
- type : 'array',
4179
- unique : false
4180
- }, {
4181
- key : 'c',
4182
- type : 'array',
4183
- unique : false,
4184
- items : {
4185
- type : 'string',
4186
- minLength : 1
4187
- }
4188
- }]);
4189
-
4190
- expect(error).to.be.null;
4191
- });
4192
-
4193
- it('true', () => {
4194
- const numberValue = [1, 1, 1];
4195
- const numberTestObj = {
4196
- a : [1, 2, 3],
4197
- b : numberValue
4198
- };
4199
-
4200
- const error : EjvError = ejv(numberTestObj, [{
4201
- key : 'a',
4202
- type : 'array',
4203
- unique : true
4204
- }, {
4205
- key : 'b',
4206
- type : 'array',
4207
- unique : true
4208
- }]);
4209
-
4210
- expect(error).to.be.instanceof(EjvError);
4211
- expect(error.type).to.be.eql(ErrorType.UNIQUE_ITEMS);
4212
- expect(error.message).to.be.eql(ErrorMsg.UNIQUE_ITEMS);
4213
- expect(error.path).to.be.eql('b');
4214
- expect(error.data).to.be.deep.equal(numberTestObj);
4215
- expect(error.errorData).to.be.ordered.members(numberValue);
4216
-
4217
- const stringValue = ['a', 'a', 'a'];
4218
- const stringTestObj = {
4219
- a : stringValue
4220
- };
4221
-
4222
- const stringsError : EjvError = ejv(stringTestObj, [{
4223
- key : 'a',
4224
- type : 'array',
4225
- unique : true,
4226
- items : {
4227
- type : 'string',
4228
- minLength : 1
4229
- }
4230
- }]);
4231
-
4232
- expect(stringsError).to.be.instanceof(EjvError);
4233
- expect(stringsError.type).to.be.eql(ErrorType.UNIQUE_ITEMS);
4234
- expect(stringsError.message).to.be.eql(ErrorMsg.UNIQUE_ITEMS);
4235
- expect(stringsError.path).to.be.eql('a');
4236
- expect(stringsError.data).to.be.deep.equal(stringTestObj);
4237
- expect(stringsError.errorData).to.be.ordered.members(stringValue);
4238
- });
4239
- });
4240
-
4241
- describe('items', () => {
4242
- describe('check parameter', () => {
4243
- it('undefined is ok', () => {
4244
- expect(ejv({
4245
- a : [1, 2, 3]
4246
- }, [{
4247
- key : 'a',
4248
- type : 'array',
4249
- items : undefined
4250
- }])).to.be.null;
4251
- });
4252
-
4253
- it('null', () => {
4254
- expect(() => ejv({
4255
- a : [1, 2, 3]
4256
- }, [{
4257
- key : 'a',
4258
- type : 'array',
4259
- items : null
4260
- }])).to.throw(ErrorMsg.INVALID_ITEMS_SCHEME
4261
- .replace(ErrorMsgCursorA, 'null'));
4262
- });
4263
- });
4264
-
4265
- describe('single data type', () => {
4266
- describe('check parameter', () => {
4267
- it('invalid data type', () => {
4268
- expect(() => ejv({
4269
- a : [1, 2, 3]
4270
- }, [{
4271
- key : 'a',
4272
- type : 'array',
4273
- items : 'invalidDataType'
4274
- }])).to.throw(); // error message by partial scheme
4275
- });
4276
- });
4277
-
4278
- it('fail', () => {
4279
- const value = [1, 2, 3];
4280
- const testObj = {
4281
- a : value
4282
- };
4283
-
4284
- const error : EjvError = ejv(testObj, [{
4285
- key : 'a',
4286
- type : 'array',
4287
- items : 'string'
4288
- }]);
4289
-
4290
- expect(error).to.be.instanceof(EjvError);
4291
- expect(error.type).to.be.eql(ErrorType.ITEMS_TYPE);
4292
- expect(error.message).to.be.eql(ErrorMsg.ITEMS_TYPE
4293
- .replace(ErrorMsgCursorA, 'string'));
4294
- expect(error.path).to.be.eql('a/0');
4295
- expect(error.data).to.be.deep.equal(testObj);
4296
- expect(error.errorData).to.be.eql(value[0]);
4297
- });
4298
-
4299
- it('ok', () => {
4300
- expect(ejv({
4301
- a : [1, 2, 3]
4302
- }, [{
4303
- key : 'a',
4304
- type : 'array',
4305
- items : 'number'
4306
- }])).to.be.null;
4307
- });
4308
-
4309
- it('ok - empty array', () => {
4310
- const error : EjvError = ejv({
4311
- a : []
4312
- }, [{
4313
- key : 'a',
4314
- type : 'array',
4315
- items : 'object'
4316
- }]);
4317
-
4318
- expect(error).to.be.null;
4319
- });
4320
- });
4321
-
4322
- describe('multiple data type', () => {
4323
- describe('check parameter', () => {
4324
- it('invalid data type', () => {
4325
- expect(() => ejv({
4326
- a : [1, 2, 3]
4327
- }, [{
4328
- key : 'a',
4329
- type : 'array',
4330
- items : ['number', 'invalidDataType']
4331
- }])).to.throw(); // error message by partial scheme
4332
- });
4333
- });
4334
-
4335
- it('fail', () => {
4336
- const enumArr : string[] = ['boolean', 'string'];
4337
-
4338
- const value = [1, 2, 2];
4339
- const testObj = {
4340
- a : [1, 2, 3],
4341
- b : [1, 2, 2]
4342
- };
4343
-
4344
- const error : EjvError = ejv(testObj, [{
4345
- key : 'a',
4346
- type : 'array',
4347
- items : enumArr
4348
- }]);
4349
-
4350
- expect(error).to.be.instanceof(EjvError);
4351
- expect(error.type).to.be.eql(ErrorType.ITEMS_TYPE);
4352
- expect(error.message).to.be.eql(ErrorMsg.ITEMS_TYPE
4353
- .replace(ErrorMsgCursorA, JSON.stringify(enumArr)));
4354
- expect(error.path).to.be.eql('a/0');
4355
- expect(error.data).to.be.deep.equal(testObj);
4356
- expect(error.errorData).to.be.eql(value[0]);
4357
- });
4358
-
4359
- it('ok', () => {
4360
- expect(ejv({
4361
- a : [1, 2, 3]
4362
- }, [{
4363
- key : 'a',
4364
- type : 'array',
4365
- items : ['string', 'number']
4366
- }])).to.be.null;
4367
-
4368
- expect(ejv({
4369
- a : [1, 2, 3]
4370
- }, [{
4371
- key : 'a',
4372
- type : 'array',
4373
- items : ['number', 'string']
4374
- }])).to.be.null;
4375
- });
4376
- });
4377
-
4378
- describe('single scheme', () => {
4379
- // single scheme has it's original error type & message
4380
- describe('check parameter', () => {
4381
- it('invalid data type', () => {
4382
- const scheme : object = {
4383
- type : 'invalidDataType'
4384
- };
4385
-
4386
- expect(() => ejv({
4387
- a : [1, 2, 3]
4388
- }, [{
4389
- key : 'a',
4390
- type : 'array',
4391
- items : scheme as any
4392
- }])).to.throw(); // error message by partial scheme
4393
- });
4394
- });
4395
-
4396
- it('fail', () => {
4397
- const itemScheme : Scheme = {
4398
- type : 'number',
4399
- min : 2
4400
- };
4401
-
4402
- const value = [1, 2, 3];
4403
- const testObj = {
4404
- a : value
4405
- };
4406
-
4407
- const error : EjvError = ejv(testObj, [{
4408
- key : 'a',
4409
- type : 'array',
4410
- items : itemScheme
4411
- }]);
4412
-
4413
- expect(error).to.be.instanceof(EjvError);
4414
- expect(error.type).to.be.eql(ErrorType.GREATER_THAN_OR_EQUAL);
4415
- expect(error.message).to.be.eql(ErrorMsg.GREATER_THAN_OR_EQUAL
4416
- .replace(ErrorMsgCursorA, '' + 2));
4417
- expect(error.path).to.be.eql('a/0');
4418
- expect(error.data).to.be.deep.equal(testObj);
4419
- expect(error.errorData).to.be.eql(value[0]);
4420
- });
4421
-
4422
- it('ok', () => {
4423
- expect(ejv({
4424
- a : [1, 2, 3]
4425
- }, [{
4426
- key : 'a',
4427
- type : 'array',
4428
- items : {
4429
- type : 'number',
4430
- min : 1,
4431
- max : 3
4432
- }
4433
- }])).to.be.null;
4434
- });
4435
-
4436
- it('fail - array of object', () => {
4437
- const data = {
4438
- a : [
4439
- { number : 2 },
4440
- { number : 3 },
4441
- { number : 4 },
4442
- { number : 5 }
4443
- ]
4444
- };
4445
-
4446
- const error : EjvError = ejv(data, [
4447
- {
4448
- key : 'a', type : 'array', items : [{
4449
- type : 'object', properties : [
4450
- { key : 'number', type : 'number', min : 4 }
4451
- ]
4452
- }]
4453
- }
4454
- ]);
4455
-
4456
- expect(error).to.be.instanceof(EjvError);
4457
- expect(error.type).to.be.eql(ErrorType.GREATER_THAN_OR_EQUAL);
4458
- expect(error.message).to.be.eql(ErrorMsg.GREATER_THAN_OR_EQUAL
4459
- .replace(ErrorMsgCursorA, '' + 4));
4460
- expect(error.path).to.be.eql('a/0/number');
4461
- expect(error.data).to.be.deep.equal(data);
4462
- expect(error.errorData).to.be.eql(2);
4463
- });
4464
-
4465
- it('fail - array of deep object', () => {
4466
- const data = {
4467
- a : [
4468
- { b : { c : { d : { number : 2 } } } },
4469
- { b : { c : { d : { number : 3 } } } },
4470
- { b : { c : { d : { number : 4 } } } },
4471
- { b : { c : { d : { number : 5 } } } }
4472
- ]
4473
- };
4474
-
4475
- const error : EjvError = ejv(data, [{
4476
- key : 'a', type : 'array', items : [{
4477
- type : 'object', properties : [{
4478
- key : 'b', type : 'object', properties : [{
4479
- key : 'c', type : 'object', properties : [{
4480
- key : 'd', type : 'object', properties : [
4481
- { key : 'number', type : 'number', min : 4 }
4482
- ]
4483
- }]
4484
- }]
4485
- }]
4486
- }]
4487
- }]);
4488
-
4489
- expect(error).to.be.instanceof(EjvError);
4490
- expect(error.type).to.be.eql(ErrorType.GREATER_THAN_OR_EQUAL);
4491
- expect(error.message).to.be.eql(ErrorMsg.GREATER_THAN_OR_EQUAL
4492
- .replace(ErrorMsgCursorA, '' + 4));
4493
- expect(error.path).to.be.eql('a/0/b/c/d/number');
4494
- expect(error.data).to.be.deep.equal(data);
4495
- expect(error.errorData).to.be.eql(2);
4496
- });
4497
- });
4498
-
4499
- describe('multiple schemes', () => {
4500
- describe('check parameter', () => {
4501
- it('invalid data type', () => {
4502
- const scheme : object = {
4503
- type : 'invalidDataType'
4504
- };
4505
-
4506
- expect(() => ejv({
4507
- a : [1, 2, 3]
4508
- }, [{
4509
- key : 'a',
4510
- type : 'array',
4511
- items : [scheme] as any
4512
- }])).to.throw(); // error message by partial scheme
4513
- });
4514
- });
4515
-
4516
- it('fail', () => {
4517
- const itemScheme1 : Scheme = {
4518
- type : 'number',
4519
- min : 2
4520
- };
4521
-
4522
- const itemScheme2 : Scheme = {
4523
- type : 'number',
4524
- min : 3
4525
- };
4526
-
4527
- const allSchemes : Scheme[] = [itemScheme1, itemScheme2];
4528
-
4529
- const data = {
4530
- a : [1, 2, 3]
4531
- };
4532
-
4533
- const error : EjvError = ejv(data, [{
4534
- key : 'a',
4535
- type : 'array',
4536
- items : allSchemes
4537
- }]);
4538
-
4539
- expect(error).to.be.instanceof(EjvError);
4540
- expect(error.type).to.be.eql(ErrorType.ITEMS_SCHEMES);
4541
- expect(error.message).to.be.eql(ErrorMsg.ITEMS_SCHEMES
4542
- .replace(ErrorMsgCursorA, JSON.stringify(allSchemes)));
4543
- expect(error.path).to.be.eql('a/0');
4544
- expect(error.data).to.be.deep.equal(data);
4545
- expect(error.errorData).to.be.eql(1);
4546
- });
4547
-
4548
- it('ok', () => {
4549
- expect(ejv({
4550
- a : [1, 2, 3]
4551
- }, [{
4552
- key : 'a',
4553
- type : 'array',
4554
- items : [{
4555
- type : 'number',
4556
- min : 1,
4557
- max : 3
4558
- }]
4559
- }])).to.be.null;
4560
-
4561
- // multiple schemes
4562
- expect(ejv({
4563
- a : [1]
4564
- }, [{
4565
- key : 'a',
4566
- type : 'array',
4567
- items : [{
4568
- type : 'number',
4569
- min : 1
4570
- }, {
4571
- type : 'string'
4572
- }]
4573
- }])).to.be.null;
4574
- });
4575
-
4576
- it('nested array - single scheme', () => {
4577
- const arr : string[] = ['ok', null];
4578
- const testObj = {
4579
- a : [{
4580
- b : arr
4581
- }]
4582
- };
4583
-
4584
- const itemScheme : Scheme = { type : 'string', minLength : 1 };
4585
-
4586
- const error : EjvError = ejv(testObj, [{
4587
- key : 'a', type : 'array', items : {
4588
- type : 'object', properties : [{
4589
- key : 'b', type : 'array', unique : true, minLength : 1, optional : true,
4590
- items : itemScheme
4591
- }]
4592
- }
4593
- }]);
4594
-
4595
- expect(error).to.be.instanceof(EjvError);
4596
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
4597
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
4598
- .replace(ErrorMsgCursorA, JSON.stringify(itemScheme)));
4599
- expect(error.path).to.be.eql('a/0/b/1');
4600
- expect(error.data).to.be.eql(testObj);
4601
- expect(error.errorData).to.be.eql(null);
4602
- });
4603
-
4604
- it('nested array - multiple chemes', () => {
4605
- const arr : string[] = ['ok', null];
4606
- const testObj = {
4607
- a : [{
4608
- b : arr
4609
- }]
4610
- };
4611
-
4612
- const itemScheme : Scheme[] = [{ type : 'string', minLength : 1 }];
4613
-
4614
- const error : EjvError = ejv(testObj, [{
4615
- key : 'a', type : 'array', items : {
4616
- type : 'object', properties : [{
4617
- key : 'b', type : 'array', unique : true, minLength : 1, optional : true,
4618
- items : itemScheme
4619
- }]
4620
- }
4621
- }]);
4622
-
4623
- expect(error).to.be.instanceof(EjvError);
4624
- expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
4625
- expect(error.message).to.be.eql(ErrorMsg.TYPE_MISMATCH
4626
- .replace(ErrorMsgCursorA, JSON.stringify(itemScheme)));
4627
- expect(error.path).to.be.eql('a/0/b/1');
4628
- expect(error.data).to.be.eql(testObj);
4629
- expect(error.errorData).to.be.eql(null);
4630
- });
4631
- });
4632
- });
4633
- });
4634
- });
1
+ import { describe, it } from 'mocha';
2
+ import { expect } from 'chai';
3
+
4
+ import { ejv } from '../src/ejv';
5
+ import { ErrorMsg, ErrorType } from '../src/constants';
6
+ import { AnyObject, EjvError, Scheme } from '../src/interfaces';
7
+ import { createErrorMsg } from '../src/util';
8
+ import { checkSchemeError } from './common-test-util';
9
+
10
+
11
+ describe('ejv()', () => {
12
+ describe('ejv() itself', () => {
13
+ describe('data', () => {
14
+ it('no data', () => {
15
+ const error: EjvError | null = ejv(
16
+ undefined as unknown as AnyObject,
17
+ undefined as unknown as Scheme[]
18
+ );
19
+
20
+ expect(error).to.be.instanceof(EjvError);
21
+ expect(error).to.have.property('type', ErrorType.NO_DATA);
22
+ expect(error).to.have.property('message', ErrorMsg.NO_DATA);
23
+ expect(error).to.have.property('data', undefined);
24
+ expect(error).to.not.have.property('path');
25
+ expect(error).to.not.have.property('errorScheme');
26
+ expect(error).to.have.property('errorData', undefined);
27
+ });
28
+
29
+ it('null data', () => {
30
+ const error: EjvError | null = ejv(
31
+ null as unknown as AnyObject,
32
+ undefined as unknown as Scheme[]
33
+ );
34
+
35
+ expect(error).to.be.instanceof(EjvError);
36
+ expect(error).to.have.property('type', ErrorType.NO_DATA);
37
+ expect(error).to.have.property('message', ErrorMsg.NO_DATA);
38
+ expect(error).to.have.property('data', null);
39
+ expect(error).to.not.have.property('path');
40
+ expect(error).to.not.have.property('errorScheme');
41
+ expect(error).to.have.property('errorData', null);
42
+ });
43
+ });
44
+
45
+ describe('scheme', () => {
46
+ const data = {
47
+ a: 'hello'
48
+ };
49
+
50
+ // can't use checkSchemeError()
51
+ it('no scheme', () => {
52
+ const error: EjvError | null = ejv(data, undefined as unknown as Scheme[]);
53
+
54
+ expect(error).to.be.instanceof(EjvError);
55
+ expect(error).to.have.property('type', ErrorType.NO_SCHEME);
56
+ expect(error).to.have.property('message', ErrorMsg.NO_SCHEME);
57
+ expect(error).to.have.property('data', data);
58
+ expect(error).to.not.have.property('path');
59
+ expect(error).to.have.property('errorScheme', undefined);
60
+ expect(error).to.not.have.property('errorData');
61
+ expect(error).to.have.property('isSchemeError', true);
62
+ expect(error).to.have.property('isDataError', false);
63
+ });
64
+
65
+ // can't use checkSchemeError()
66
+ it('null scheme', () => {
67
+ const error: EjvError | null = ejv(data, null as unknown as Scheme[]);
68
+
69
+ expect(error).to.be.instanceof(EjvError);
70
+ expect(error).to.have.property('type', ErrorType.NO_SCHEME);
71
+ expect(error).to.have.property('message', ErrorMsg.NO_SCHEME);
72
+ expect(error).to.have.property('data', data);
73
+ expect(error).to.not.have.property('path');
74
+ expect(error).to.have.property('errorScheme', null);
75
+ expect(error).to.not.have.property('errorData');
76
+ expect(error).to.have.property('isSchemeError', true);
77
+ expect(error).to.have.property('isDataError', false);
78
+ });
79
+
80
+ // can't use checkSchemeError()
81
+ it('empty scheme array', () => {
82
+ const errorScheme: Scheme[] = [];
83
+
84
+ const error: EjvError | null = ejv(data, errorScheme);
85
+
86
+ expect(error).to.be.instanceof(EjvError);
87
+ expect(error).to.have.property('type', ErrorType.INVALID_SCHEMES);
88
+ expect(error).to.have.property('message', ErrorMsg.EMPTY_SCHEME);
89
+ expect(error).to.have.property('data', data);
90
+ expect(error).to.not.have.property('path');
91
+ expect(error).to.have.property('errorScheme', errorScheme);
92
+ expect(error).to.not.have.property('errorData');
93
+ expect(error).to.have.property('isSchemeError', true);
94
+ expect(error).to.have.property('isDataError', false);
95
+ });
96
+
97
+ // can't use checkSchemeError()
98
+ it('invalid scheme object', () => {
99
+ const errorScheme: Scheme[] = ['string'];
100
+
101
+ const error: EjvError | null = ejv(data, errorScheme);
102
+
103
+ expect(error).to.be.instanceof(EjvError);
104
+ expect(error).to.have.property('type', ErrorType.INVALID_SCHEMES);
105
+ expect(error).to.have.property('message', ErrorMsg.NO_OBJECT_ARRAY_SCHEME);
106
+ expect(error).to.have.property('data', data);
107
+ expect(error).to.not.have.property('path');
108
+ expect(error).to.have.property('errorScheme', errorScheme);
109
+ expect(error).to.not.have.property('errorData');
110
+ expect(error).to.have.property('isSchemeError', true);
111
+ expect(error).to.have.property('isDataError', false);
112
+ });
113
+
114
+ it('no type', () => {
115
+ const errorScheme: Scheme = {
116
+ key: 'a'
117
+ } as unknown as Scheme;
118
+
119
+ checkSchemeError({
120
+ data,
121
+ errorScheme,
122
+ message: ErrorMsg.SCHEMES_SHOULD_HAVE_TYPE
123
+ });
124
+ });
125
+
126
+ it('invalid type', () => {
127
+ const errorScheme: Scheme = {
128
+ key: 'a',
129
+ type: 'invalidType'
130
+ };
131
+
132
+ checkSchemeError({
133
+ data,
134
+ errorScheme,
135
+ message: createErrorMsg(ErrorMsg.SCHEMES_HAS_INVALID_TYPE, {
136
+ placeholders: ['invalidType']
137
+ })
138
+ });
139
+ });
140
+
141
+ it('duplicated type', () => {
142
+ const errorScheme: Scheme = {
143
+ key: 'a',
144
+ type: ['string', 'string']
145
+ };
146
+
147
+ checkSchemeError({
148
+ data,
149
+ errorScheme,
150
+ message: createErrorMsg(ErrorMsg.SCHEMES_HAS_DUPLICATED_TYPE, {
151
+ placeholders: ['string']
152
+ })
153
+ });
154
+ });
155
+ });
156
+
157
+ describe('options', () => {
158
+ describe('customErrorMsg', () => {
159
+ it('override required error', () => {
160
+ const customErrorMsg = 'property \'a\' required';
161
+
162
+ const error: EjvError | null = ejv({
163
+ // empty
164
+ }, [{
165
+ key: 'a',
166
+ type: 'number'
167
+ }], {
168
+ customErrorMsg: {
169
+ [ErrorType.REQUIRED]: customErrorMsg
170
+ }
171
+ });
172
+
173
+ expect(error).to.be.instanceof(EjvError);
174
+
175
+ if (!error) {
176
+ throw new Error('spec failed');
177
+ }
178
+
179
+ expect(error.type).to.be.eql(ErrorType.REQUIRED);
180
+ expect(error.message).to.be.eql(customErrorMsg);
181
+ });
182
+
183
+ it('override type matching error', () => {
184
+ const customErrorMsg = 'property \'a\' should be a number';
185
+
186
+ const error: EjvError | null = ejv({
187
+ a: 'a'
188
+ }, [{
189
+ key: 'a',
190
+ type: 'number'
191
+ }], {
192
+ customErrorMsg: {
193
+ [ErrorType.TYPE_MISMATCH]: customErrorMsg
194
+ }
195
+ });
196
+
197
+ expect(error).to.be.instanceof(EjvError);
198
+
199
+ if (!error) {
200
+ throw new Error('spec failed');
201
+ }
202
+
203
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
204
+ expect(error.message).to.be.eql(customErrorMsg);
205
+ });
206
+ });
207
+ });
208
+ });
209
+ });