ejv 2.1.1 → 2.1.3

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 (45) hide show
  1. package/.mocharc.json +8 -8
  2. package/CHANGELOG.md +134 -135
  3. package/LICENSE +21 -21
  4. package/README-KR.md +604 -597
  5. package/README.md +608 -603
  6. package/build/cjs/constants.js +1 -0
  7. package/build/cjs/constants.js.map +1 -1
  8. package/build/cjs/ejv.js +3 -0
  9. package/build/cjs/ejv.js.map +1 -1
  10. package/build/cjs/package.json +1 -1
  11. package/build/cjs/tester.js +11 -1
  12. package/build/cjs/tester.js.map +1 -1
  13. package/build/constants.d.ts +1 -0
  14. package/build/esm/constants.js +1 -0
  15. package/build/esm/constants.js.map +1 -1
  16. package/build/esm/ejv.js +4 -1
  17. package/build/esm/ejv.js.map +1 -1
  18. package/build/esm/package.json +1 -1
  19. package/build/esm/tester.js +9 -0
  20. package/build/esm/tester.js.map +1 -1
  21. package/build/tester.d.ts +1 -0
  22. package/eslint.config.mjs +66 -59
  23. package/package.json +56 -54
  24. package/scripts/add-js-extensions.ts +59 -59
  25. package/spec/ArrayScheme.ts +1021 -1021
  26. package/spec/CommonScheme.ts +251 -251
  27. package/spec/DateScheme.ts +472 -472
  28. package/spec/NumberScheme.ts +1160 -1160
  29. package/spec/ObjectScheme.ts +499 -499
  30. package/spec/RegExpScheme.ts +112 -112
  31. package/spec/StringScheme.ts +1407 -1336
  32. package/spec/common-test-util.ts +63 -63
  33. package/spec/ejv.spec.ts +235 -235
  34. package/spec/testers.spec.ts +833 -833
  35. package/src/constants.ts +164 -162
  36. package/src/ejv.ts +1751 -1746
  37. package/src/index.ts +14 -14
  38. package/src/interfaces.ts +144 -144
  39. package/src/tester.ts +323 -312
  40. package/src/util.ts +124 -124
  41. package/tsconfig.cjs.json +8 -8
  42. package/tsconfig.esm.json +7 -7
  43. package/tsconfig.json +19 -19
  44. package/tsconfig.scripts.json +14 -14
  45. package/tsconfig.types.json +9 -9
@@ -1,1160 +1,1160 @@
1
- import { describe, it } from 'mocha';
2
- import { expect } from 'chai';
3
-
4
- import { ejv } from '../src/ejv';
5
-
6
- import { EjvError, Scheme } from '../src/interfaces';
7
- import { ERROR_MESSAGE, ERROR_TYPE } from '../src/constants';
8
- import { createErrorMsg } from '../src/util';
9
- import { checkSchemeError, TypeTester, typeTesterArr } from './common-test-util';
10
-
11
-
12
- describe('NumberScheme', () => {
13
- describe('type', () => {
14
- describe('mismatch', () => {
15
- typeTesterArr
16
- .filter((obj: TypeTester): boolean => obj.type !== 'number')
17
- .forEach((obj: TypeTester): void => {
18
- const data = {
19
- a: obj.value
20
- };
21
-
22
- it(obj.type, () => {
23
- const error: EjvError | null = ejv(data, [{
24
- key: 'a',
25
- type: 'number'
26
- }]);
27
-
28
- expect(error).to.be.instanceof(EjvError);
29
-
30
- if (!error) {
31
- throw new Error('spec failed');
32
- }
33
-
34
- expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH);
35
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
36
- placeholders: ['number']
37
- }));
38
- expect(error.path).to.be.eql('a');
39
- expect(error.data).to.be.deep.equal(data);
40
- expect(error.errorData).to.be.eql(obj.value);
41
- });
42
- });
43
-
44
- it('multiple types', () => {
45
- const value = 123;
46
- const typeArr: string[] = ['boolean', 'string'];
47
-
48
- const data = {
49
- a: value
50
- };
51
-
52
- const error: EjvError | null = ejv(data, [{
53
- key: 'a',
54
- type: typeArr
55
- }]);
56
-
57
- expect(error).to.be.instanceof(EjvError);
58
-
59
- if (!error) {
60
- throw new Error('spec failed');
61
- }
62
-
63
- expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH_ONE_OF);
64
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
65
- placeholders: [JSON.stringify(typeArr)]
66
- }));
67
- expect(error.path).to.be.eql('a');
68
- expect(error.data).to.be.deep.equal(data);
69
- expect(error.errorData).to.be.eql(value);
70
- });
71
- });
72
-
73
- describe('match', () => {
74
- it('optional', () => {
75
- expect(ejv({
76
- a: undefined
77
- }, [{
78
- key: 'a',
79
- type: 'number',
80
- optional: true
81
- }])).to.be.null;
82
- });
83
-
84
- it('single type', () => {
85
- expect(ejv({
86
- a: 123
87
- }, [{
88
- key: 'a',
89
- type: 'number'
90
- }])).to.be.null;
91
- });
92
-
93
- it('multiple types', () => {
94
- expect(ejv({
95
- a: 123
96
- }, [{
97
- key: 'a',
98
- type: ['number', 'string']
99
- }])).to.be.null;
100
- });
101
-
102
- it('multiple types', () => {
103
- expect(ejv({
104
- a: 123
105
- }, [{
106
- key: 'a',
107
- type: ['string', 'number']
108
- }])).to.be.null;
109
- });
110
- });
111
- });
112
-
113
- describe('enum & notEnum', () => {
114
- describe('enum', () => {
115
- describe('check parameter', () => {
116
- it('undefined is ok', () => {
117
- expect(ejv({
118
- a: 1
119
- }, [{
120
- key: 'a',
121
- type: 'number',
122
- enum: undefined
123
- }])).to.be.null;
124
- });
125
-
126
- it('null', () => {
127
- const data = {
128
- a: 1
129
- };
130
-
131
- const errorScheme: Scheme = {
132
- key: 'a',
133
- type: 'number',
134
- enum: null as unknown as number[]
135
- };
136
-
137
- checkSchemeError({
138
- data,
139
- errorScheme,
140
- message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
141
- });
142
- });
143
-
144
- it('not array', () => {
145
- const data = {
146
- a: 10
147
- };
148
-
149
- const errorScheme: Scheme = {
150
- key: 'a',
151
- type: 'number',
152
- enum: 1 as unknown as number[]
153
- };
154
-
155
- checkSchemeError({
156
- data,
157
- errorScheme,
158
- message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
159
- });
160
- });
161
-
162
- it('not number', () => {
163
- const data = {
164
- a: 10
165
- };
166
-
167
- const errorScheme: Scheme = {
168
- key: 'a',
169
- type: 'number',
170
- enum: ['10']
171
- };
172
-
173
- checkSchemeError({
174
- data,
175
- errorScheme,
176
- message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_NUMBERS)
177
- });
178
- });
179
- });
180
-
181
- it('fail', () => {
182
- const enumArr: number[] = [9, 11];
183
-
184
- const data = {
185
- a: 10
186
- };
187
-
188
- const error: EjvError | null = ejv(data, [{
189
- key: 'a',
190
- type: 'number',
191
- enum: enumArr
192
- }]);
193
-
194
- expect(error).to.be.instanceof(EjvError);
195
-
196
- if (!error) {
197
- throw new Error('spec failed');
198
- }
199
-
200
- expect(error.type).to.be.eql(ERROR_TYPE.ONE_VALUE_OF);
201
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.ONE_VALUE_OF, {
202
- placeholders: [JSON.stringify(enumArr)]
203
- }));
204
- expect(error.path).to.be.eql('a');
205
- expect(error.data).to.be.deep.equal(data);
206
- expect(error.errorData).to.be.eql(10);
207
- });
208
-
209
- it('ok', () => {
210
- expect(ejv({
211
- a: 10
212
- }, [{
213
- key: 'a',
214
- type: 'number',
215
- enum: [9, 10, 11]
216
- }])).to.be.null;
217
- });
218
- });
219
-
220
- describe('notEnum', () => {
221
- describe('check parameter', () => {
222
- it('undefined is ok', () => {
223
- expect(ejv({
224
- a: 1
225
- }, [{
226
- key: 'a',
227
- type: 'number',
228
- notEnum: undefined
229
- }])).to.be.null;
230
- });
231
-
232
- it('null', () => {
233
- const data = {
234
- a: 1
235
- };
236
-
237
- const errorScheme: Scheme = {
238
- key: 'a',
239
- type: 'number',
240
- notEnum: null as unknown as number[]
241
- };
242
-
243
- checkSchemeError({
244
- data,
245
- errorScheme,
246
- message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
247
- });
248
- });
249
-
250
- it('not array', () => {
251
- const data = {
252
- a: 10
253
- };
254
-
255
- const errorScheme: Scheme = {
256
- key: 'a',
257
- type: 'number',
258
- notEnum: 1 as unknown as number[]
259
- };
260
-
261
- checkSchemeError({
262
- data,
263
- errorScheme,
264
- message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
265
- });
266
- });
267
-
268
- it('not number', () => {
269
- const data = {
270
- a: 10
271
- };
272
-
273
- const errorScheme: Scheme = {
274
- key: 'a',
275
- type: 'number',
276
- notEnum: ['10']
277
- };
278
-
279
- checkSchemeError({
280
- data,
281
- errorScheme,
282
- message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_NUMBERS)
283
- });
284
- });
285
- });
286
-
287
- it('fail', () => {
288
- const enumArr: number[] = [9, 10, 11];
289
-
290
- const data = {
291
- a: 9
292
- };
293
-
294
- const error: EjvError | null = ejv(data, [{
295
- key: 'a',
296
- type: 'number',
297
- notEnum: enumArr
298
- }]);
299
-
300
- expect(error).to.be.instanceof(EjvError);
301
-
302
- if (!error) {
303
- throw new Error('spec failed');
304
- }
305
-
306
- expect(error.type).to.be.eql(ERROR_TYPE.NOT_ONE_VALUE_OF);
307
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
308
- placeholders: [JSON.stringify(enumArr)]
309
- }));
310
- expect(error.path).to.be.eql('a');
311
- expect(error.data).to.be.deep.equal(data);
312
- expect(error.errorData).to.be.eql(9);
313
- });
314
-
315
- it('ok', () => {
316
- expect(ejv({
317
- a: 8
318
- }, [{
319
- key: 'a',
320
- type: 'number',
321
- notEnum: [9, 10, 11]
322
- }])).to.be.null;
323
- });
324
- });
325
- });
326
-
327
- describe('min & exclusiveMin', () => {
328
- describe('min only', () => {
329
- describe('check parameter', () => {
330
- it('undefined is ok', () => {
331
- expect(ejv({
332
- a: 1
333
- }, [{
334
- key: 'a',
335
- type: 'number',
336
- min: undefined
337
- }])).to.be.null;
338
- });
339
-
340
- it('null', () => {
341
- const data = {
342
- a: 3
343
- };
344
-
345
- const errorScheme: Scheme = {
346
- key: 'a',
347
- type: 'number',
348
- min: null as unknown as number
349
- };
350
-
351
- checkSchemeError({
352
- data,
353
- errorScheme,
354
- message: createErrorMsg(ERROR_MESSAGE.MIN_SHOULD_BE_NUMBER)
355
- });
356
- });
357
-
358
- it('min type', () => {
359
- const data = {
360
- a: 3
361
- };
362
-
363
- const errorScheme: Scheme = {
364
- key: 'a',
365
- type: 'number',
366
- min: '3'
367
- };
368
-
369
- checkSchemeError({
370
- data,
371
- errorScheme,
372
- message: createErrorMsg(ERROR_MESSAGE.MIN_SHOULD_BE_NUMBER)
373
- });
374
- });
375
-
376
- it('0 is ok', () => {
377
- expect(ejv({
378
- a: 1
379
- }, [{
380
- key: 'a',
381
- type: 'number',
382
- min: 0
383
- }])).to.be.null;
384
- });
385
- });
386
-
387
- it('ok', () => {
388
- const error1: EjvError | null = ejv({
389
- a: 9
390
- }, [{
391
- key: 'a',
392
- type: 'number',
393
- min: 10
394
- }]);
395
-
396
- expect(error1).to.be.instanceof(EjvError);
397
-
398
- if (!error1) {
399
- throw new Error('spec failed');
400
- }
401
-
402
- expect(error1.type).to.be.eql(ERROR_TYPE.BIGGER_THAN_OR_EQUAL);
403
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN_OR_EQUAL, {
404
- placeholders: [10]
405
- }));
406
-
407
- expect(ejv({
408
- a: 10
409
- }, [{
410
- key: 'a',
411
- type: 'number',
412
- min: 10
413
- }])).to.be.null;
414
-
415
- expect(ejv({
416
- a: 11
417
- }, [{
418
- key: 'a',
419
- type: 'number',
420
- min: 10
421
- }])).to.be.null;
422
- });
423
- });
424
-
425
- describe('exclusiveMin', () => {
426
- describe('check parameter', () => {
427
- it('exclusiveMin type', () => {
428
- const data = {
429
- a: 3
430
- };
431
-
432
- const errorScheme: Scheme = {
433
- key: 'a',
434
- type: 'number',
435
- min: 3,
436
- exclusiveMin: '3' as unknown as boolean
437
- };
438
-
439
- checkSchemeError({
440
- data,
441
- errorScheme,
442
- message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN)
443
- });
444
- });
445
- });
446
-
447
- it('exclusiveMin === true', () => {
448
- const error1: EjvError | null = ejv({
449
- a: 9
450
- }, [{
451
- key: 'a',
452
- type: 'number',
453
- min: 10,
454
- exclusiveMin: true
455
- }]);
456
-
457
- expect(error1).to.be.instanceof(EjvError);
458
-
459
- if (!error1) {
460
- throw new Error('spec failed');
461
- }
462
-
463
- expect(error1.type).to.be.eql(ERROR_TYPE.BIGGER_THAN);
464
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN, {
465
- placeholders: [10]
466
- }));
467
-
468
- const error2: EjvError | null = ejv({
469
- a: 10
470
- }, [{
471
- key: 'a',
472
- type: 'number',
473
- min: 10,
474
- exclusiveMin: true
475
- }]);
476
-
477
- expect(error2).to.be.instanceof(EjvError);
478
-
479
- if (!error2) {
480
- throw new Error('spec failed');
481
- }
482
-
483
- expect(error2.type).to.be.eql(ERROR_TYPE.BIGGER_THAN);
484
- expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN, {
485
- placeholders: [10]
486
- }));
487
-
488
- expect(ejv({
489
- a: 11
490
- }, [{
491
- key: 'a',
492
- type: 'number',
493
- min: 10,
494
- exclusiveMin: true
495
- }])).to.be.null;
496
- });
497
-
498
- it('exclusiveMin === false', () => {
499
- const error1: EjvError | null = ejv({
500
- a: 9
501
- }, [{
502
- key: 'a',
503
- type: 'number',
504
- min: 10,
505
- exclusiveMin: false
506
- }]);
507
-
508
- expect(error1).to.be.instanceof(EjvError);
509
-
510
- if (!error1) {
511
- throw new Error('spec failed');
512
- }
513
-
514
- expect(error1.type).to.be.eql(ERROR_TYPE.BIGGER_THAN_OR_EQUAL);
515
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN_OR_EQUAL, {
516
- placeholders: [10]
517
- }));
518
-
519
- expect(ejv({
520
- a: 10
521
- }, [{
522
- key: 'a',
523
- type: 'number',
524
- min: 10,
525
- exclusiveMin: false
526
- }])).to.be.null;
527
-
528
- expect(ejv({
529
- a: 11
530
- }, [{
531
- key: 'a',
532
- type: 'number',
533
- min: 10,
534
- exclusiveMin: false
535
- }])).to.be.null;
536
- });
537
- });
538
- });
539
-
540
- describe('max & exclusiveMax', () => {
541
- describe('max only', () => {
542
- describe('check parameter', () => {
543
- it('undefined is ok', () => {
544
- expect(ejv({
545
- a: 1
546
- }, [{
547
- key: 'a',
548
- type: 'number',
549
- max: undefined
550
- }])).to.be.null;
551
- });
552
-
553
- it('null', () => {
554
- const data = {
555
- a: 3
556
- };
557
-
558
- const errorScheme: Scheme = {
559
- key: 'a',
560
- type: 'number',
561
- max: null as unknown as number
562
- };
563
-
564
- checkSchemeError({
565
- data,
566
- errorScheme,
567
- message: createErrorMsg(ERROR_MESSAGE.MAX_SHOULD_BE_NUMBER)
568
- });
569
- });
570
-
571
- it('max type', () => {
572
- const data = {
573
- a: 3
574
- };
575
-
576
- const errorScheme: Scheme = {
577
- key: 'a',
578
- type: 'number',
579
- max: '3'
580
- };
581
-
582
- checkSchemeError({
583
- data,
584
- errorScheme,
585
- message: createErrorMsg(ERROR_MESSAGE.MAX_SHOULD_BE_NUMBER)
586
- });
587
- });
588
-
589
- it('0 is ok', () => {
590
- expect(ejv({
591
- a: -1
592
- }, [{
593
- key: 'a',
594
- type: 'number',
595
- max: 0
596
- }])).to.be.null;
597
- });
598
- });
599
-
600
- it('ok', () => {
601
- expect(ejv({
602
- a: 9
603
- }, [{
604
- key: 'a',
605
- type: 'number',
606
- max: 10
607
- }])).to.be.null;
608
-
609
- expect(ejv({
610
- a: 10
611
- }, [{
612
- key: 'a',
613
- type: 'number',
614
- max: 10
615
- }])).to.be.null;
616
-
617
- const error1: EjvError | null = ejv({
618
- a: 11
619
- }, [{
620
- key: 'a',
621
- type: 'number',
622
- max: 10
623
- }]);
624
-
625
- expect(error1).to.be.instanceof(EjvError);
626
-
627
- if (!error1) {
628
- throw new Error('spec failed');
629
- }
630
-
631
- expect(error1.type).to.be.eql(ERROR_TYPE.SMALLER_THAN_OR_EQUAL);
632
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN_OR_EQUAL, {
633
- placeholders: [10]
634
- }));
635
- });
636
- });
637
-
638
- describe('exclusiveMax', () => {
639
- describe('check parameter', () => {
640
- it('exclusiveMax type', () => {
641
- const data = {
642
- a: 3
643
- };
644
-
645
- const errorScheme: Scheme = {
646
- key: 'a',
647
- type: 'number',
648
- max: 3,
649
- exclusiveMax: '3' as unknown as boolean
650
- };
651
-
652
- checkSchemeError({
653
- data,
654
- errorScheme,
655
- message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN)
656
- });
657
- });
658
- });
659
-
660
- it('exclusiveMax === true', () => {
661
- expect(ejv({
662
- a: 9
663
- }, [{
664
- key: 'a',
665
- type: 'number',
666
- max: 10,
667
- exclusiveMax: true
668
- }])).to.be.null;
669
-
670
- const error1: EjvError | null = ejv({
671
- a: 10
672
- }, [{
673
- key: 'a',
674
- type: 'number',
675
- max: 10,
676
- exclusiveMax: true
677
- }]);
678
-
679
- expect(error1).to.be.instanceof(EjvError);
680
-
681
- if (!error1) {
682
- throw new Error('spec failed');
683
- }
684
-
685
- expect(error1.type).to.be.eql(ERROR_TYPE.SMALLER_THAN);
686
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN, {
687
- placeholders: [10]
688
- }));
689
-
690
- const error2: EjvError | null = ejv({
691
- a: 11
692
- }, [{
693
- key: 'a',
694
- type: 'number',
695
- max: 10,
696
- exclusiveMax: true
697
- }]);
698
-
699
- expect(error2).to.be.instanceof(EjvError);
700
-
701
- if (!error2) {
702
- throw new Error('spec failed');
703
- }
704
-
705
- expect(error2.type).to.be.eql(ERROR_TYPE.SMALLER_THAN);
706
- expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN, {
707
- placeholders: [10]
708
- }));
709
- });
710
-
711
- it('exclusiveMax === false', () => {
712
- expect(ejv({
713
- a: 9
714
- }, [{
715
- key: 'a',
716
- type: 'number',
717
- max: 10,
718
- exclusiveMax: false
719
- }])).to.be.null;
720
-
721
- expect(ejv({
722
- a: 10
723
- }, [{
724
- key: 'a',
725
- type: 'number',
726
- max: 10,
727
- exclusiveMax: false
728
- }])).to.be.null;
729
-
730
- const error1: EjvError | null = ejv({
731
- a: 11
732
- }, [{
733
- key: 'a',
734
- type: 'number',
735
- max: 10,
736
- exclusiveMax: false
737
- }]);
738
-
739
- expect(error1).to.be.instanceof(EjvError);
740
-
741
- if (!error1) {
742
- throw new Error('spec failed');
743
- }
744
-
745
- expect(error1.type).to.be.eql(ERROR_TYPE.SMALLER_THAN_OR_EQUAL);
746
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN_OR_EQUAL, {
747
- placeholders: [10]
748
- }));
749
- });
750
- });
751
- });
752
-
753
- describe('format', () => {
754
- describe('check parameter', () => {
755
- it('undefined is ok', () => {
756
- expect(ejv({
757
- a: 123.5
758
- }, [{
759
- key: 'a',
760
- type: 'number',
761
- format: undefined
762
- }])).to.be.null;
763
- });
764
-
765
- it('null', () => {
766
- const data = {
767
- a: 123.5
768
- };
769
-
770
- const errorScheme: Scheme = {
771
- key: 'a',
772
- type: 'number',
773
- format: null as unknown as string
774
- };
775
-
776
- checkSchemeError({
777
- data,
778
- errorScheme,
779
- message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
780
- placeholders: ['null']
781
- })
782
- });
783
- });
784
-
785
- describe('invalid number format', () => {
786
- const data = {
787
- a: 1
788
- };
789
-
790
- it('single', () => {
791
- const errorScheme: Scheme = {
792
- key: 'a',
793
- type: 'number',
794
- format: 'invalidNumberFormat'
795
- };
796
-
797
- checkSchemeError({
798
- data,
799
- errorScheme,
800
- message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
801
- placeholders: ['invalidNumberFormat']
802
- })
803
- });
804
- });
805
-
806
- it('multiple', () => {
807
- const errorScheme: Scheme = {
808
- key: 'a',
809
- type: 'number',
810
- format: ['index', 'invalidNumberFormat']
811
- };
812
-
813
- checkSchemeError({
814
- data,
815
- errorScheme,
816
- message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
817
- placeholders: ['invalidNumberFormat']
818
- })
819
- });
820
- });
821
- });
822
- });
823
-
824
- describe('integer', () => {
825
- describe('single format', () => {
826
- it('fail', () => {
827
- const error: EjvError | null = ejv({
828
- a: 123.5
829
- }, [{
830
- key: 'a',
831
- type: 'number',
832
- format: 'integer'
833
- }]);
834
-
835
- expect(error).to.be.instanceof(EjvError);
836
-
837
- if (!error) {
838
- throw new Error('spec failed');
839
- }
840
-
841
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
842
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
843
- placeholders: ['integer']
844
- }));
845
- });
846
-
847
- it('ok', () => {
848
- expect(ejv({
849
- a: 123
850
- }, [{
851
- key: 'a',
852
- type: 'number',
853
- format: 'integer'
854
- }])).to.be.null;
855
- });
856
- });
857
-
858
- describe('multiple formats', () => {
859
- it('fail', () => {
860
- const formatArr: string[] = ['integer'];
861
-
862
- const error: EjvError | null = ejv({
863
- a: 123.5
864
- }, [{
865
- key: 'a',
866
- type: 'number',
867
- format: formatArr
868
- }]);
869
-
870
- expect(error).to.be.instanceof(EjvError);
871
-
872
- if (!error) {
873
- throw new Error('spec failed');
874
- }
875
-
876
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
877
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
878
- placeholders: [JSON.stringify(formatArr)]
879
- }));
880
- });
881
-
882
- it('ok', () => {
883
- expect(ejv({
884
- a: -7
885
- }, [{
886
- key: 'a',
887
- type: 'number',
888
- format: ['integer']
889
- }])).to.be.null;
890
-
891
- expect(ejv({
892
- a: 0
893
- }, [{
894
- key: 'a',
895
- type: 'number',
896
- format: ['integer']
897
- }])).to.be.null;
898
-
899
- expect(ejv({
900
- a: 123
901
- }, [{
902
- key: 'a',
903
- type: 'number',
904
- format: ['integer']
905
- }])).to.be.null;
906
- });
907
-
908
- it('ok - with others', () => {
909
- expect(ejv({
910
- a: -7
911
- }, [{
912
- key: 'a',
913
- type: 'number',
914
- format: ['integer', 'index']
915
- }])).to.be.null;
916
-
917
- expect(ejv({
918
- a: 0
919
- }, [{
920
- key: 'a',
921
- type: 'number',
922
- format: ['integer', 'index']
923
- }])).to.be.null;
924
-
925
- expect(ejv({
926
- a: 123
927
- }, [{
928
- key: 'a',
929
- type: 'number',
930
- format: ['integer', 'index']
931
- }])).to.be.null;
932
- });
933
-
934
- it('ok - with others', () => {
935
- expect(ejv({
936
- a: -7
937
- }, [{
938
- key: 'a',
939
- type: 'number',
940
- format: ['index', 'integer']
941
- }])).to.be.null;
942
-
943
- expect(ejv({
944
- a: 0
945
- }, [{
946
- key: 'a',
947
- type: 'number',
948
- format: ['index', 'integer']
949
- }])).to.be.null;
950
-
951
- expect(ejv({
952
- a: 123
953
- }, [{
954
- key: 'a',
955
- type: 'number',
956
- format: ['index', 'integer']
957
- }])).to.be.null;
958
- });
959
- });
960
- });
961
-
962
- describe('index', () => {
963
- describe('single format', () => {
964
- it('fail', () => {
965
- const error1: EjvError | null = ejv({
966
- a: 1.5
967
- }, [{
968
- key: 'a',
969
- type: 'number',
970
- format: 'index'
971
- }]);
972
-
973
- expect(error1).to.be.instanceof(EjvError);
974
-
975
- if (!error1) {
976
- throw new Error('spec failed');
977
- }
978
-
979
- expect(error1.type).to.be.eql(ERROR_TYPE.FORMAT);
980
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
981
- placeholders: ['index']
982
- }));
983
-
984
- const error2: EjvError | null = ejv({
985
- a: -1
986
- }, [{
987
- key: 'a',
988
- type: 'number',
989
- format: 'index'
990
- }]);
991
-
992
- expect(error2).to.be.instanceof(EjvError);
993
-
994
- if (!error2) {
995
- throw new Error('spec failed');
996
- }
997
-
998
- expect(error2.type).to.be.eql(ERROR_TYPE.FORMAT);
999
- expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
1000
- placeholders: ['index']
1001
- }));
1002
-
1003
- const error3: EjvError | null = ejv({
1004
- a: -1.6
1005
- }, [{
1006
- key: 'a',
1007
- type: 'number',
1008
- format: 'index'
1009
- }]);
1010
-
1011
- expect(error3).to.be.instanceof(EjvError);
1012
-
1013
- if (!error3) {
1014
- throw new Error('spec failed');
1015
- }
1016
-
1017
- expect(error3.type).to.be.eql(ERROR_TYPE.FORMAT);
1018
- expect(error3.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
1019
- placeholders: ['index']
1020
- }));
1021
- });
1022
-
1023
- it('ok', () => {
1024
- expect(ejv({
1025
- a: 0
1026
- }, [{
1027
- key: 'a',
1028
- type: 'number',
1029
- format: 'index'
1030
- }])).to.be.null;
1031
-
1032
- expect(ejv({
1033
- a: 6
1034
- }, [{
1035
- key: 'a',
1036
- type: 'number',
1037
- format: 'index'
1038
- }])).to.be.null;
1039
- });
1040
- });
1041
-
1042
- describe('multiple formats', () => {
1043
- it('fail', () => {
1044
- const formatArr: string[] = ['index'];
1045
-
1046
- const error1: EjvError | null = ejv({
1047
- a: 1.5
1048
- }, [{
1049
- key: 'a',
1050
- type: 'number',
1051
- format: formatArr
1052
- }]);
1053
-
1054
- expect(error1).to.be.instanceof(EjvError);
1055
-
1056
- if (!error1) {
1057
- throw new Error('spec failed');
1058
- }
1059
-
1060
- expect(error1.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1061
- expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1062
- placeholders: [JSON.stringify(formatArr)]
1063
- }));
1064
-
1065
- const error2: EjvError | null = ejv({
1066
- a: -1
1067
- }, [{
1068
- key: 'a',
1069
- type: 'number',
1070
- format: formatArr
1071
- }]);
1072
-
1073
- expect(error2).to.be.instanceof(EjvError);
1074
-
1075
- if (!error2) {
1076
- throw new Error('spec failed');
1077
- }
1078
-
1079
- expect(error2.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1080
- expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1081
- placeholders: [JSON.stringify(formatArr)]
1082
- }));
1083
-
1084
- const error3: EjvError | null = ejv({
1085
- a: -1.6
1086
- }, [{
1087
- key: 'a',
1088
- type: 'number',
1089
- format: formatArr
1090
- }]);
1091
-
1092
- expect(error3).to.be.instanceof(EjvError);
1093
-
1094
- if (!error3) {
1095
- throw new Error('spec failed');
1096
- }
1097
-
1098
- expect(error3.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1099
- expect(error3.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1100
- placeholders: [JSON.stringify(formatArr)]
1101
- }));
1102
- });
1103
-
1104
- it('ok', () => {
1105
- expect(ejv({
1106
- a: 0
1107
- }, [{
1108
- key: 'a',
1109
- type: 'number',
1110
- format: ['index']
1111
- }])).to.be.null;
1112
-
1113
- expect(ejv({
1114
- a: 6
1115
- }, [{
1116
- key: 'a',
1117
- type: 'number',
1118
- format: ['index']
1119
- }])).to.be.null;
1120
- });
1121
-
1122
- it('ok - with others', () => {
1123
- expect(ejv({
1124
- a: 0
1125
- }, [{
1126
- key: 'a',
1127
- type: 'number',
1128
- format: ['index', 'integer']
1129
- }])).to.be.null;
1130
-
1131
- expect(ejv({
1132
- a: 6
1133
- }, [{
1134
- key: 'a',
1135
- type: 'number',
1136
- format: ['index', 'integer']
1137
- }])).to.be.null;
1138
- });
1139
-
1140
- it('ok - with others', () => {
1141
- expect(ejv({
1142
- a: 0
1143
- }, [{
1144
- key: 'a',
1145
- type: 'number',
1146
- format: ['integer', 'index']
1147
- }])).to.be.null;
1148
-
1149
- expect(ejv({
1150
- a: 6
1151
- }, [{
1152
- key: 'a',
1153
- type: 'number',
1154
- format: ['integer', 'index']
1155
- }])).to.be.null;
1156
- });
1157
- });
1158
- });
1159
- });
1160
- });
1
+ import { describe, it } from 'mocha';
2
+ import { expect } from 'chai';
3
+
4
+ import { ejv } from '../src/ejv';
5
+
6
+ import { EjvError, Scheme } from '../src/interfaces';
7
+ import { ERROR_MESSAGE, ERROR_TYPE } from '../src/constants';
8
+ import { createErrorMsg } from '../src/util';
9
+ import { checkSchemeError, TypeTester, typeTesterArr } from './common-test-util';
10
+
11
+
12
+ describe('NumberScheme', () => {
13
+ describe('type', () => {
14
+ describe('mismatch', () => {
15
+ typeTesterArr
16
+ .filter((obj: TypeTester): boolean => obj.type !== 'number')
17
+ .forEach((obj: TypeTester): void => {
18
+ const data = {
19
+ a: obj.value
20
+ };
21
+
22
+ it(obj.type, () => {
23
+ const error: EjvError | null = ejv(data, [{
24
+ key: 'a',
25
+ type: 'number'
26
+ }]);
27
+
28
+ expect(error).to.be.instanceof(EjvError);
29
+
30
+ if (!error) {
31
+ throw new Error('spec failed');
32
+ }
33
+
34
+ expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH);
35
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH, {
36
+ placeholders: ['number']
37
+ }));
38
+ expect(error.path).to.be.eql('a');
39
+ expect(error.data).to.be.deep.equal(data);
40
+ expect(error.errorData).to.be.eql(obj.value);
41
+ });
42
+ });
43
+
44
+ it('multiple types', () => {
45
+ const value = 123;
46
+ const typeArr: string[] = ['boolean', 'string'];
47
+
48
+ const data = {
49
+ a: value
50
+ };
51
+
52
+ const error: EjvError | null = ejv(data, [{
53
+ key: 'a',
54
+ type: typeArr
55
+ }]);
56
+
57
+ expect(error).to.be.instanceof(EjvError);
58
+
59
+ if (!error) {
60
+ throw new Error('spec failed');
61
+ }
62
+
63
+ expect(error.type).to.be.eql(ERROR_TYPE.TYPE_MISMATCH_ONE_OF);
64
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.TYPE_MISMATCH_ONE_OF, {
65
+ placeholders: [JSON.stringify(typeArr)]
66
+ }));
67
+ expect(error.path).to.be.eql('a');
68
+ expect(error.data).to.be.deep.equal(data);
69
+ expect(error.errorData).to.be.eql(value);
70
+ });
71
+ });
72
+
73
+ describe('match', () => {
74
+ it('optional', () => {
75
+ expect(ejv({
76
+ a: undefined
77
+ }, [{
78
+ key: 'a',
79
+ type: 'number',
80
+ optional: true
81
+ }])).to.be.null;
82
+ });
83
+
84
+ it('single type', () => {
85
+ expect(ejv({
86
+ a: 123
87
+ }, [{
88
+ key: 'a',
89
+ type: 'number'
90
+ }])).to.be.null;
91
+ });
92
+
93
+ it('multiple types', () => {
94
+ expect(ejv({
95
+ a: 123
96
+ }, [{
97
+ key: 'a',
98
+ type: ['number', 'string']
99
+ }])).to.be.null;
100
+ });
101
+
102
+ it('multiple types', () => {
103
+ expect(ejv({
104
+ a: 123
105
+ }, [{
106
+ key: 'a',
107
+ type: ['string', 'number']
108
+ }])).to.be.null;
109
+ });
110
+ });
111
+ });
112
+
113
+ describe('enum & notEnum', () => {
114
+ describe('enum', () => {
115
+ describe('check parameter', () => {
116
+ it('undefined is ok', () => {
117
+ expect(ejv({
118
+ a: 1
119
+ }, [{
120
+ key: 'a',
121
+ type: 'number',
122
+ enum: undefined
123
+ }])).to.be.null;
124
+ });
125
+
126
+ it('null', () => {
127
+ const data = {
128
+ a: 1
129
+ };
130
+
131
+ const errorScheme: Scheme = {
132
+ key: 'a',
133
+ type: 'number',
134
+ enum: null as unknown as number[]
135
+ };
136
+
137
+ checkSchemeError({
138
+ data,
139
+ errorScheme,
140
+ message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
141
+ });
142
+ });
143
+
144
+ it('not array', () => {
145
+ const data = {
146
+ a: 10
147
+ };
148
+
149
+ const errorScheme: Scheme = {
150
+ key: 'a',
151
+ type: 'number',
152
+ enum: 1 as unknown as number[]
153
+ };
154
+
155
+ checkSchemeError({
156
+ data,
157
+ errorScheme,
158
+ message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
159
+ });
160
+ });
161
+
162
+ it('not number', () => {
163
+ const data = {
164
+ a: 10
165
+ };
166
+
167
+ const errorScheme: Scheme = {
168
+ key: 'a',
169
+ type: 'number',
170
+ enum: ['10']
171
+ };
172
+
173
+ checkSchemeError({
174
+ data,
175
+ errorScheme,
176
+ message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_NUMBERS)
177
+ });
178
+ });
179
+ });
180
+
181
+ it('fail', () => {
182
+ const enumArr: number[] = [9, 11];
183
+
184
+ const data = {
185
+ a: 10
186
+ };
187
+
188
+ const error: EjvError | null = ejv(data, [{
189
+ key: 'a',
190
+ type: 'number',
191
+ enum: enumArr
192
+ }]);
193
+
194
+ expect(error).to.be.instanceof(EjvError);
195
+
196
+ if (!error) {
197
+ throw new Error('spec failed');
198
+ }
199
+
200
+ expect(error.type).to.be.eql(ERROR_TYPE.ONE_VALUE_OF);
201
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.ONE_VALUE_OF, {
202
+ placeholders: [JSON.stringify(enumArr)]
203
+ }));
204
+ expect(error.path).to.be.eql('a');
205
+ expect(error.data).to.be.deep.equal(data);
206
+ expect(error.errorData).to.be.eql(10);
207
+ });
208
+
209
+ it('ok', () => {
210
+ expect(ejv({
211
+ a: 10
212
+ }, [{
213
+ key: 'a',
214
+ type: 'number',
215
+ enum: [9, 10, 11]
216
+ }])).to.be.null;
217
+ });
218
+ });
219
+
220
+ describe('notEnum', () => {
221
+ describe('check parameter', () => {
222
+ it('undefined is ok', () => {
223
+ expect(ejv({
224
+ a: 1
225
+ }, [{
226
+ key: 'a',
227
+ type: 'number',
228
+ notEnum: undefined
229
+ }])).to.be.null;
230
+ });
231
+
232
+ it('null', () => {
233
+ const data = {
234
+ a: 1
235
+ };
236
+
237
+ const errorScheme: Scheme = {
238
+ key: 'a',
239
+ type: 'number',
240
+ notEnum: null as unknown as number[]
241
+ };
242
+
243
+ checkSchemeError({
244
+ data,
245
+ errorScheme,
246
+ message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
247
+ });
248
+ });
249
+
250
+ it('not array', () => {
251
+ const data = {
252
+ a: 10
253
+ };
254
+
255
+ const errorScheme: Scheme = {
256
+ key: 'a',
257
+ type: 'number',
258
+ notEnum: 1 as unknown as number[]
259
+ };
260
+
261
+ checkSchemeError({
262
+ data,
263
+ errorScheme,
264
+ message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
265
+ });
266
+ });
267
+
268
+ it('not number', () => {
269
+ const data = {
270
+ a: 10
271
+ };
272
+
273
+ const errorScheme: Scheme = {
274
+ key: 'a',
275
+ type: 'number',
276
+ notEnum: ['10']
277
+ };
278
+
279
+ checkSchemeError({
280
+ data,
281
+ errorScheme,
282
+ message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_NUMBERS)
283
+ });
284
+ });
285
+ });
286
+
287
+ it('fail', () => {
288
+ const enumArr: number[] = [9, 10, 11];
289
+
290
+ const data = {
291
+ a: 9
292
+ };
293
+
294
+ const error: EjvError | null = ejv(data, [{
295
+ key: 'a',
296
+ type: 'number',
297
+ notEnum: enumArr
298
+ }]);
299
+
300
+ expect(error).to.be.instanceof(EjvError);
301
+
302
+ if (!error) {
303
+ throw new Error('spec failed');
304
+ }
305
+
306
+ expect(error.type).to.be.eql(ERROR_TYPE.NOT_ONE_VALUE_OF);
307
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
308
+ placeholders: [JSON.stringify(enumArr)]
309
+ }));
310
+ expect(error.path).to.be.eql('a');
311
+ expect(error.data).to.be.deep.equal(data);
312
+ expect(error.errorData).to.be.eql(9);
313
+ });
314
+
315
+ it('ok', () => {
316
+ expect(ejv({
317
+ a: 8
318
+ }, [{
319
+ key: 'a',
320
+ type: 'number',
321
+ notEnum: [9, 10, 11]
322
+ }])).to.be.null;
323
+ });
324
+ });
325
+ });
326
+
327
+ describe('min & exclusiveMin', () => {
328
+ describe('min only', () => {
329
+ describe('check parameter', () => {
330
+ it('undefined is ok', () => {
331
+ expect(ejv({
332
+ a: 1
333
+ }, [{
334
+ key: 'a',
335
+ type: 'number',
336
+ min: undefined
337
+ }])).to.be.null;
338
+ });
339
+
340
+ it('null', () => {
341
+ const data = {
342
+ a: 3
343
+ };
344
+
345
+ const errorScheme: Scheme = {
346
+ key: 'a',
347
+ type: 'number',
348
+ min: null as unknown as number
349
+ };
350
+
351
+ checkSchemeError({
352
+ data,
353
+ errorScheme,
354
+ message: createErrorMsg(ERROR_MESSAGE.MIN_SHOULD_BE_NUMBER)
355
+ });
356
+ });
357
+
358
+ it('min type', () => {
359
+ const data = {
360
+ a: 3
361
+ };
362
+
363
+ const errorScheme: Scheme = {
364
+ key: 'a',
365
+ type: 'number',
366
+ min: '3'
367
+ };
368
+
369
+ checkSchemeError({
370
+ data,
371
+ errorScheme,
372
+ message: createErrorMsg(ERROR_MESSAGE.MIN_SHOULD_BE_NUMBER)
373
+ });
374
+ });
375
+
376
+ it('0 is ok', () => {
377
+ expect(ejv({
378
+ a: 1
379
+ }, [{
380
+ key: 'a',
381
+ type: 'number',
382
+ min: 0
383
+ }])).to.be.null;
384
+ });
385
+ });
386
+
387
+ it('ok', () => {
388
+ const error1: EjvError | null = ejv({
389
+ a: 9
390
+ }, [{
391
+ key: 'a',
392
+ type: 'number',
393
+ min: 10
394
+ }]);
395
+
396
+ expect(error1).to.be.instanceof(EjvError);
397
+
398
+ if (!error1) {
399
+ throw new Error('spec failed');
400
+ }
401
+
402
+ expect(error1.type).to.be.eql(ERROR_TYPE.BIGGER_THAN_OR_EQUAL);
403
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN_OR_EQUAL, {
404
+ placeholders: [10]
405
+ }));
406
+
407
+ expect(ejv({
408
+ a: 10
409
+ }, [{
410
+ key: 'a',
411
+ type: 'number',
412
+ min: 10
413
+ }])).to.be.null;
414
+
415
+ expect(ejv({
416
+ a: 11
417
+ }, [{
418
+ key: 'a',
419
+ type: 'number',
420
+ min: 10
421
+ }])).to.be.null;
422
+ });
423
+ });
424
+
425
+ describe('exclusiveMin', () => {
426
+ describe('check parameter', () => {
427
+ it('exclusiveMin type', () => {
428
+ const data = {
429
+ a: 3
430
+ };
431
+
432
+ const errorScheme: Scheme = {
433
+ key: 'a',
434
+ type: 'number',
435
+ min: 3,
436
+ exclusiveMin: '3' as unknown as boolean
437
+ };
438
+
439
+ checkSchemeError({
440
+ data,
441
+ errorScheme,
442
+ message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MIN_SHOULD_BE_BOOLEAN)
443
+ });
444
+ });
445
+ });
446
+
447
+ it('exclusiveMin === true', () => {
448
+ const error1: EjvError | null = ejv({
449
+ a: 9
450
+ }, [{
451
+ key: 'a',
452
+ type: 'number',
453
+ min: 10,
454
+ exclusiveMin: true
455
+ }]);
456
+
457
+ expect(error1).to.be.instanceof(EjvError);
458
+
459
+ if (!error1) {
460
+ throw new Error('spec failed');
461
+ }
462
+
463
+ expect(error1.type).to.be.eql(ERROR_TYPE.BIGGER_THAN);
464
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN, {
465
+ placeholders: [10]
466
+ }));
467
+
468
+ const error2: EjvError | null = ejv({
469
+ a: 10
470
+ }, [{
471
+ key: 'a',
472
+ type: 'number',
473
+ min: 10,
474
+ exclusiveMin: true
475
+ }]);
476
+
477
+ expect(error2).to.be.instanceof(EjvError);
478
+
479
+ if (!error2) {
480
+ throw new Error('spec failed');
481
+ }
482
+
483
+ expect(error2.type).to.be.eql(ERROR_TYPE.BIGGER_THAN);
484
+ expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN, {
485
+ placeholders: [10]
486
+ }));
487
+
488
+ expect(ejv({
489
+ a: 11
490
+ }, [{
491
+ key: 'a',
492
+ type: 'number',
493
+ min: 10,
494
+ exclusiveMin: true
495
+ }])).to.be.null;
496
+ });
497
+
498
+ it('exclusiveMin === false', () => {
499
+ const error1: EjvError | null = ejv({
500
+ a: 9
501
+ }, [{
502
+ key: 'a',
503
+ type: 'number',
504
+ min: 10,
505
+ exclusiveMin: false
506
+ }]);
507
+
508
+ expect(error1).to.be.instanceof(EjvError);
509
+
510
+ if (!error1) {
511
+ throw new Error('spec failed');
512
+ }
513
+
514
+ expect(error1.type).to.be.eql(ERROR_TYPE.BIGGER_THAN_OR_EQUAL);
515
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.BIGGER_THAN_OR_EQUAL, {
516
+ placeholders: [10]
517
+ }));
518
+
519
+ expect(ejv({
520
+ a: 10
521
+ }, [{
522
+ key: 'a',
523
+ type: 'number',
524
+ min: 10,
525
+ exclusiveMin: false
526
+ }])).to.be.null;
527
+
528
+ expect(ejv({
529
+ a: 11
530
+ }, [{
531
+ key: 'a',
532
+ type: 'number',
533
+ min: 10,
534
+ exclusiveMin: false
535
+ }])).to.be.null;
536
+ });
537
+ });
538
+ });
539
+
540
+ describe('max & exclusiveMax', () => {
541
+ describe('max only', () => {
542
+ describe('check parameter', () => {
543
+ it('undefined is ok', () => {
544
+ expect(ejv({
545
+ a: 1
546
+ }, [{
547
+ key: 'a',
548
+ type: 'number',
549
+ max: undefined
550
+ }])).to.be.null;
551
+ });
552
+
553
+ it('null', () => {
554
+ const data = {
555
+ a: 3
556
+ };
557
+
558
+ const errorScheme: Scheme = {
559
+ key: 'a',
560
+ type: 'number',
561
+ max: null as unknown as number
562
+ };
563
+
564
+ checkSchemeError({
565
+ data,
566
+ errorScheme,
567
+ message: createErrorMsg(ERROR_MESSAGE.MAX_SHOULD_BE_NUMBER)
568
+ });
569
+ });
570
+
571
+ it('max type', () => {
572
+ const data = {
573
+ a: 3
574
+ };
575
+
576
+ const errorScheme: Scheme = {
577
+ key: 'a',
578
+ type: 'number',
579
+ max: '3'
580
+ };
581
+
582
+ checkSchemeError({
583
+ data,
584
+ errorScheme,
585
+ message: createErrorMsg(ERROR_MESSAGE.MAX_SHOULD_BE_NUMBER)
586
+ });
587
+ });
588
+
589
+ it('0 is ok', () => {
590
+ expect(ejv({
591
+ a: -1
592
+ }, [{
593
+ key: 'a',
594
+ type: 'number',
595
+ max: 0
596
+ }])).to.be.null;
597
+ });
598
+ });
599
+
600
+ it('ok', () => {
601
+ expect(ejv({
602
+ a: 9
603
+ }, [{
604
+ key: 'a',
605
+ type: 'number',
606
+ max: 10
607
+ }])).to.be.null;
608
+
609
+ expect(ejv({
610
+ a: 10
611
+ }, [{
612
+ key: 'a',
613
+ type: 'number',
614
+ max: 10
615
+ }])).to.be.null;
616
+
617
+ const error1: EjvError | null = ejv({
618
+ a: 11
619
+ }, [{
620
+ key: 'a',
621
+ type: 'number',
622
+ max: 10
623
+ }]);
624
+
625
+ expect(error1).to.be.instanceof(EjvError);
626
+
627
+ if (!error1) {
628
+ throw new Error('spec failed');
629
+ }
630
+
631
+ expect(error1.type).to.be.eql(ERROR_TYPE.SMALLER_THAN_OR_EQUAL);
632
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN_OR_EQUAL, {
633
+ placeholders: [10]
634
+ }));
635
+ });
636
+ });
637
+
638
+ describe('exclusiveMax', () => {
639
+ describe('check parameter', () => {
640
+ it('exclusiveMax type', () => {
641
+ const data = {
642
+ a: 3
643
+ };
644
+
645
+ const errorScheme: Scheme = {
646
+ key: 'a',
647
+ type: 'number',
648
+ max: 3,
649
+ exclusiveMax: '3' as unknown as boolean
650
+ };
651
+
652
+ checkSchemeError({
653
+ data,
654
+ errorScheme,
655
+ message: createErrorMsg(ERROR_MESSAGE.EXCLUSIVE_MAX_SHOULD_BE_BOOLEAN)
656
+ });
657
+ });
658
+ });
659
+
660
+ it('exclusiveMax === true', () => {
661
+ expect(ejv({
662
+ a: 9
663
+ }, [{
664
+ key: 'a',
665
+ type: 'number',
666
+ max: 10,
667
+ exclusiveMax: true
668
+ }])).to.be.null;
669
+
670
+ const error1: EjvError | null = ejv({
671
+ a: 10
672
+ }, [{
673
+ key: 'a',
674
+ type: 'number',
675
+ max: 10,
676
+ exclusiveMax: true
677
+ }]);
678
+
679
+ expect(error1).to.be.instanceof(EjvError);
680
+
681
+ if (!error1) {
682
+ throw new Error('spec failed');
683
+ }
684
+
685
+ expect(error1.type).to.be.eql(ERROR_TYPE.SMALLER_THAN);
686
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN, {
687
+ placeholders: [10]
688
+ }));
689
+
690
+ const error2: EjvError | null = ejv({
691
+ a: 11
692
+ }, [{
693
+ key: 'a',
694
+ type: 'number',
695
+ max: 10,
696
+ exclusiveMax: true
697
+ }]);
698
+
699
+ expect(error2).to.be.instanceof(EjvError);
700
+
701
+ if (!error2) {
702
+ throw new Error('spec failed');
703
+ }
704
+
705
+ expect(error2.type).to.be.eql(ERROR_TYPE.SMALLER_THAN);
706
+ expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN, {
707
+ placeholders: [10]
708
+ }));
709
+ });
710
+
711
+ it('exclusiveMax === false', () => {
712
+ expect(ejv({
713
+ a: 9
714
+ }, [{
715
+ key: 'a',
716
+ type: 'number',
717
+ max: 10,
718
+ exclusiveMax: false
719
+ }])).to.be.null;
720
+
721
+ expect(ejv({
722
+ a: 10
723
+ }, [{
724
+ key: 'a',
725
+ type: 'number',
726
+ max: 10,
727
+ exclusiveMax: false
728
+ }])).to.be.null;
729
+
730
+ const error1: EjvError | null = ejv({
731
+ a: 11
732
+ }, [{
733
+ key: 'a',
734
+ type: 'number',
735
+ max: 10,
736
+ exclusiveMax: false
737
+ }]);
738
+
739
+ expect(error1).to.be.instanceof(EjvError);
740
+
741
+ if (!error1) {
742
+ throw new Error('spec failed');
743
+ }
744
+
745
+ expect(error1.type).to.be.eql(ERROR_TYPE.SMALLER_THAN_OR_EQUAL);
746
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.SMALLER_THAN_OR_EQUAL, {
747
+ placeholders: [10]
748
+ }));
749
+ });
750
+ });
751
+ });
752
+
753
+ describe('format', () => {
754
+ describe('check parameter', () => {
755
+ it('undefined is ok', () => {
756
+ expect(ejv({
757
+ a: 123.5
758
+ }, [{
759
+ key: 'a',
760
+ type: 'number',
761
+ format: undefined
762
+ }])).to.be.null;
763
+ });
764
+
765
+ it('null', () => {
766
+ const data = {
767
+ a: 123.5
768
+ };
769
+
770
+ const errorScheme: Scheme = {
771
+ key: 'a',
772
+ type: 'number',
773
+ format: null as unknown as string
774
+ };
775
+
776
+ checkSchemeError({
777
+ data,
778
+ errorScheme,
779
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
780
+ placeholders: ['null']
781
+ })
782
+ });
783
+ });
784
+
785
+ describe('invalid number format', () => {
786
+ const data = {
787
+ a: 1
788
+ };
789
+
790
+ it('single', () => {
791
+ const errorScheme: Scheme = {
792
+ key: 'a',
793
+ type: 'number',
794
+ format: 'invalidNumberFormat'
795
+ };
796
+
797
+ checkSchemeError({
798
+ data,
799
+ errorScheme,
800
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
801
+ placeholders: ['invalidNumberFormat']
802
+ })
803
+ });
804
+ });
805
+
806
+ it('multiple', () => {
807
+ const errorScheme: Scheme = {
808
+ key: 'a',
809
+ type: 'number',
810
+ format: ['index', 'invalidNumberFormat']
811
+ };
812
+
813
+ checkSchemeError({
814
+ data,
815
+ errorScheme,
816
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_NUMBER_FORMAT, {
817
+ placeholders: ['invalidNumberFormat']
818
+ })
819
+ });
820
+ });
821
+ });
822
+ });
823
+
824
+ describe('integer', () => {
825
+ describe('single format', () => {
826
+ it('fail', () => {
827
+ const error: EjvError | null = ejv({
828
+ a: 123.5
829
+ }, [{
830
+ key: 'a',
831
+ type: 'number',
832
+ format: 'integer'
833
+ }]);
834
+
835
+ expect(error).to.be.instanceof(EjvError);
836
+
837
+ if (!error) {
838
+ throw new Error('spec failed');
839
+ }
840
+
841
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
842
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
843
+ placeholders: ['integer']
844
+ }));
845
+ });
846
+
847
+ it('ok', () => {
848
+ expect(ejv({
849
+ a: 123
850
+ }, [{
851
+ key: 'a',
852
+ type: 'number',
853
+ format: 'integer'
854
+ }])).to.be.null;
855
+ });
856
+ });
857
+
858
+ describe('multiple formats', () => {
859
+ it('fail', () => {
860
+ const formatArr: string[] = ['integer'];
861
+
862
+ const error: EjvError | null = ejv({
863
+ a: 123.5
864
+ }, [{
865
+ key: 'a',
866
+ type: 'number',
867
+ format: formatArr
868
+ }]);
869
+
870
+ expect(error).to.be.instanceof(EjvError);
871
+
872
+ if (!error) {
873
+ throw new Error('spec failed');
874
+ }
875
+
876
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
877
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
878
+ placeholders: [JSON.stringify(formatArr)]
879
+ }));
880
+ });
881
+
882
+ it('ok', () => {
883
+ expect(ejv({
884
+ a: -7
885
+ }, [{
886
+ key: 'a',
887
+ type: 'number',
888
+ format: ['integer']
889
+ }])).to.be.null;
890
+
891
+ expect(ejv({
892
+ a: 0
893
+ }, [{
894
+ key: 'a',
895
+ type: 'number',
896
+ format: ['integer']
897
+ }])).to.be.null;
898
+
899
+ expect(ejv({
900
+ a: 123
901
+ }, [{
902
+ key: 'a',
903
+ type: 'number',
904
+ format: ['integer']
905
+ }])).to.be.null;
906
+ });
907
+
908
+ it('ok - with others', () => {
909
+ expect(ejv({
910
+ a: -7
911
+ }, [{
912
+ key: 'a',
913
+ type: 'number',
914
+ format: ['integer', 'index']
915
+ }])).to.be.null;
916
+
917
+ expect(ejv({
918
+ a: 0
919
+ }, [{
920
+ key: 'a',
921
+ type: 'number',
922
+ format: ['integer', 'index']
923
+ }])).to.be.null;
924
+
925
+ expect(ejv({
926
+ a: 123
927
+ }, [{
928
+ key: 'a',
929
+ type: 'number',
930
+ format: ['integer', 'index']
931
+ }])).to.be.null;
932
+ });
933
+
934
+ it('ok - with others', () => {
935
+ expect(ejv({
936
+ a: -7
937
+ }, [{
938
+ key: 'a',
939
+ type: 'number',
940
+ format: ['index', 'integer']
941
+ }])).to.be.null;
942
+
943
+ expect(ejv({
944
+ a: 0
945
+ }, [{
946
+ key: 'a',
947
+ type: 'number',
948
+ format: ['index', 'integer']
949
+ }])).to.be.null;
950
+
951
+ expect(ejv({
952
+ a: 123
953
+ }, [{
954
+ key: 'a',
955
+ type: 'number',
956
+ format: ['index', 'integer']
957
+ }])).to.be.null;
958
+ });
959
+ });
960
+ });
961
+
962
+ describe('index', () => {
963
+ describe('single format', () => {
964
+ it('fail', () => {
965
+ const error1: EjvError | null = ejv({
966
+ a: 1.5
967
+ }, [{
968
+ key: 'a',
969
+ type: 'number',
970
+ format: 'index'
971
+ }]);
972
+
973
+ expect(error1).to.be.instanceof(EjvError);
974
+
975
+ if (!error1) {
976
+ throw new Error('spec failed');
977
+ }
978
+
979
+ expect(error1.type).to.be.eql(ERROR_TYPE.FORMAT);
980
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
981
+ placeholders: ['index']
982
+ }));
983
+
984
+ const error2: EjvError | null = ejv({
985
+ a: -1
986
+ }, [{
987
+ key: 'a',
988
+ type: 'number',
989
+ format: 'index'
990
+ }]);
991
+
992
+ expect(error2).to.be.instanceof(EjvError);
993
+
994
+ if (!error2) {
995
+ throw new Error('spec failed');
996
+ }
997
+
998
+ expect(error2.type).to.be.eql(ERROR_TYPE.FORMAT);
999
+ expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
1000
+ placeholders: ['index']
1001
+ }));
1002
+
1003
+ const error3: EjvError | null = ejv({
1004
+ a: -1.6
1005
+ }, [{
1006
+ key: 'a',
1007
+ type: 'number',
1008
+ format: 'index'
1009
+ }]);
1010
+
1011
+ expect(error3).to.be.instanceof(EjvError);
1012
+
1013
+ if (!error3) {
1014
+ throw new Error('spec failed');
1015
+ }
1016
+
1017
+ expect(error3.type).to.be.eql(ERROR_TYPE.FORMAT);
1018
+ expect(error3.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
1019
+ placeholders: ['index']
1020
+ }));
1021
+ });
1022
+
1023
+ it('ok', () => {
1024
+ expect(ejv({
1025
+ a: 0
1026
+ }, [{
1027
+ key: 'a',
1028
+ type: 'number',
1029
+ format: 'index'
1030
+ }])).to.be.null;
1031
+
1032
+ expect(ejv({
1033
+ a: 6
1034
+ }, [{
1035
+ key: 'a',
1036
+ type: 'number',
1037
+ format: 'index'
1038
+ }])).to.be.null;
1039
+ });
1040
+ });
1041
+
1042
+ describe('multiple formats', () => {
1043
+ it('fail', () => {
1044
+ const formatArr: string[] = ['index'];
1045
+
1046
+ const error1: EjvError | null = ejv({
1047
+ a: 1.5
1048
+ }, [{
1049
+ key: 'a',
1050
+ type: 'number',
1051
+ format: formatArr
1052
+ }]);
1053
+
1054
+ expect(error1).to.be.instanceof(EjvError);
1055
+
1056
+ if (!error1) {
1057
+ throw new Error('spec failed');
1058
+ }
1059
+
1060
+ expect(error1.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1061
+ expect(error1.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1062
+ placeholders: [JSON.stringify(formatArr)]
1063
+ }));
1064
+
1065
+ const error2: EjvError | null = ejv({
1066
+ a: -1
1067
+ }, [{
1068
+ key: 'a',
1069
+ type: 'number',
1070
+ format: formatArr
1071
+ }]);
1072
+
1073
+ expect(error2).to.be.instanceof(EjvError);
1074
+
1075
+ if (!error2) {
1076
+ throw new Error('spec failed');
1077
+ }
1078
+
1079
+ expect(error2.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1080
+ expect(error2.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1081
+ placeholders: [JSON.stringify(formatArr)]
1082
+ }));
1083
+
1084
+ const error3: EjvError | null = ejv({
1085
+ a: -1.6
1086
+ }, [{
1087
+ key: 'a',
1088
+ type: 'number',
1089
+ format: formatArr
1090
+ }]);
1091
+
1092
+ expect(error3).to.be.instanceof(EjvError);
1093
+
1094
+ if (!error3) {
1095
+ throw new Error('spec failed');
1096
+ }
1097
+
1098
+ expect(error3.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1099
+ expect(error3.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1100
+ placeholders: [JSON.stringify(formatArr)]
1101
+ }));
1102
+ });
1103
+
1104
+ it('ok', () => {
1105
+ expect(ejv({
1106
+ a: 0
1107
+ }, [{
1108
+ key: 'a',
1109
+ type: 'number',
1110
+ format: ['index']
1111
+ }])).to.be.null;
1112
+
1113
+ expect(ejv({
1114
+ a: 6
1115
+ }, [{
1116
+ key: 'a',
1117
+ type: 'number',
1118
+ format: ['index']
1119
+ }])).to.be.null;
1120
+ });
1121
+
1122
+ it('ok - with others', () => {
1123
+ expect(ejv({
1124
+ a: 0
1125
+ }, [{
1126
+ key: 'a',
1127
+ type: 'number',
1128
+ format: ['index', 'integer']
1129
+ }])).to.be.null;
1130
+
1131
+ expect(ejv({
1132
+ a: 6
1133
+ }, [{
1134
+ key: 'a',
1135
+ type: 'number',
1136
+ format: ['index', 'integer']
1137
+ }])).to.be.null;
1138
+ });
1139
+
1140
+ it('ok - with others', () => {
1141
+ expect(ejv({
1142
+ a: 0
1143
+ }, [{
1144
+ key: 'a',
1145
+ type: 'number',
1146
+ format: ['integer', 'index']
1147
+ }])).to.be.null;
1148
+
1149
+ expect(ejv({
1150
+ a: 6
1151
+ }, [{
1152
+ key: 'a',
1153
+ type: 'number',
1154
+ format: ['integer', 'index']
1155
+ }])).to.be.null;
1156
+ });
1157
+ });
1158
+ });
1159
+ });
1160
+ });