ejv 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/.mocharc.json +8 -8
  2. package/CHANGELOG.md +134 -135
  3. package/README-KR.md +597 -597
  4. package/README.md +603 -603
  5. package/build/cjs/constants.js +1 -0
  6. package/build/cjs/constants.js.map +1 -1
  7. package/build/cjs/ejv.js +3 -0
  8. package/build/cjs/ejv.js.map +1 -1
  9. package/build/cjs/tester.js +11 -1
  10. package/build/cjs/tester.js.map +1 -1
  11. package/build/constants.d.ts +1 -0
  12. package/build/esm/constants.js +1 -0
  13. package/build/esm/constants.js.map +1 -1
  14. package/build/esm/ejv.js +4 -1
  15. package/build/esm/ejv.js.map +1 -1
  16. package/build/esm/tester.js +9 -0
  17. package/build/esm/tester.js.map +1 -1
  18. package/build/tester.d.ts +1 -0
  19. package/eslint.config.mjs +66 -59
  20. package/package.json +54 -54
  21. package/scripts/add-js-extensions.ts +59 -59
  22. package/spec/ArrayScheme.ts +1021 -1021
  23. package/spec/CommonScheme.ts +251 -251
  24. package/spec/DateScheme.ts +472 -472
  25. package/spec/NumberScheme.ts +1160 -1160
  26. package/spec/ObjectScheme.ts +499 -499
  27. package/spec/RegExpScheme.ts +112 -112
  28. package/spec/StringScheme.ts +1407 -1336
  29. package/spec/common-test-util.ts +63 -63
  30. package/spec/ejv.spec.ts +235 -235
  31. package/spec/testers.spec.ts +833 -833
  32. package/src/constants.ts +164 -162
  33. package/src/ejv.ts +1751 -1746
  34. package/src/index.ts +14 -14
  35. package/src/interfaces.ts +144 -144
  36. package/src/tester.ts +323 -312
  37. package/src/util.ts +124 -124
  38. package/tsconfig.cjs.json +8 -8
  39. package/tsconfig.esm.json +7 -7
  40. package/tsconfig.json +19 -19
  41. package/tsconfig.scripts.json +14 -14
  42. package/tsconfig.types.json +9 -9
@@ -1,1336 +1,1407 @@
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('StringScheme', () => {
13
- describe('type', () => {
14
- describe('mismatch', () => {
15
- typeTesterArr
16
- .filter((obj: TypeTester): boolean => obj.type !== 'string')
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: 'string'
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: ['string']
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 = 'ejv';
46
- const typeArr: string[] = ['boolean', 'number'];
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: 'string',
80
- optional: true
81
- }])).to.be.null;
82
- });
83
-
84
- it('single type', () => {
85
- expect(ejv({
86
- a: 'ejv'
87
- }, [{
88
- key: 'a',
89
- type: 'string'
90
- }])).to.be.null;
91
- });
92
-
93
- it('multiple types', () => {
94
- expect(ejv({
95
- a: 'ejv'
96
- }, [{
97
- key: 'a',
98
- type: ['string', 'number']
99
- }])).to.be.null;
100
- });
101
-
102
- it('multiple types', () => {
103
- expect(ejv({
104
- a: 'ejv'
105
- }, [{
106
- key: 'a',
107
- type: ['number', 'string']
108
- }])).to.be.null;
109
- });
110
- });
111
- });
112
-
113
- describe('enum & notEnum', () => {
114
- describe('enum', () => {
115
- describe('check parameter', () => {
116
- const data = {
117
- a: 'a'
118
- };
119
-
120
- it('undefined is ok', () => {
121
- expect(ejv(data, [{
122
- key: 'a',
123
- type: 'string',
124
- enum: undefined
125
- }])).to.be.null;
126
- });
127
-
128
- it('null', () => {
129
- const errorScheme: Scheme = {
130
- key: 'a',
131
- type: 'string',
132
- enum: null as unknown as string[]
133
- };
134
-
135
- checkSchemeError({
136
- data,
137
- errorScheme,
138
- message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
139
- });
140
- });
141
-
142
- it('not array', () => {
143
- const errorScheme: Scheme = {
144
- key: 'a',
145
- type: 'string',
146
- enum: 'a' as unknown as string[]
147
- };
148
-
149
- checkSchemeError({
150
- data,
151
- errorScheme,
152
- message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
153
- });
154
- });
155
-
156
- it('not string', () => {
157
- const errorScheme: Scheme = {
158
- key: 'a',
159
- type: 'string',
160
- enum: [10]
161
- };
162
-
163
- checkSchemeError({
164
- data,
165
- errorScheme,
166
- message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_STRINGS)
167
- });
168
- });
169
- });
170
-
171
- it('fail', () => {
172
- const enumArr: string[] = ['b', 'c'];
173
-
174
- const data = {
175
- a: 'a'
176
- };
177
-
178
- const error: EjvError | null = ejv(data, [{
179
- key: 'a',
180
- type: 'string',
181
- enum: enumArr
182
- }]);
183
-
184
- expect(error).to.be.instanceof(EjvError);
185
-
186
- if (!error) {
187
- throw new Error('spec failed');
188
- }
189
-
190
- expect(error.type).to.be.eql(ERROR_TYPE.ONE_VALUE_OF);
191
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.ONE_VALUE_OF, {
192
- placeholders: [JSON.stringify(enumArr)]
193
- }));
194
- expect(error.path).to.be.eql('a');
195
- expect(error.data).to.be.deep.equal(data);
196
- expect(error.errorData).to.be.eql('a');
197
- });
198
-
199
- it('ok', () => {
200
- expect(ejv({
201
- a: 'a'
202
- }, [{
203
- key: 'a',
204
- type: 'string',
205
- enum: ['a', 'b', 'c']
206
- }])).to.be.null;
207
- });
208
- });
209
-
210
- describe('notEnum', () => {
211
- describe('check parameter', () => {
212
- const data = {
213
- a: 'a'
214
- };
215
-
216
- it('undefined is ok', () => {
217
- expect(ejv(data, [{
218
- key: 'a',
219
- type: 'string',
220
- notEnum: undefined
221
- }])).to.be.null;
222
- });
223
-
224
- it('null', () => {
225
- const errorScheme: Scheme = {
226
- key: 'a',
227
- type: 'string',
228
- notEnum: null as unknown as string[]
229
- };
230
-
231
- checkSchemeError({
232
- data,
233
- errorScheme,
234
- message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
235
- });
236
- });
237
-
238
- it('not array', () => {
239
- const errorScheme: Scheme = {
240
- key: 'a',
241
- type: 'string',
242
- notEnum: 'a' as unknown as string[]
243
- };
244
-
245
- checkSchemeError({
246
- data,
247
- errorScheme,
248
- message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
249
- });
250
- });
251
-
252
- it('not string', () => {
253
- const errorScheme: Scheme = {
254
- key: 'a',
255
- type: 'string',
256
- notEnum: [10]
257
- };
258
-
259
- checkSchemeError({
260
- data,
261
- errorScheme,
262
- message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_STRINGS)
263
- });
264
- });
265
- });
266
-
267
- it('fail', () => {
268
- const enumArr: string[] = ['a', 'b', 'c'];
269
-
270
- const data = {
271
- a: 'a'
272
- };
273
-
274
- const error: EjvError | null = ejv(data, [{
275
- key: 'a',
276
- type: 'string',
277
- notEnum: enumArr
278
- }]);
279
-
280
- expect(error).to.be.instanceof(EjvError);
281
-
282
- if (!error) {
283
- throw new Error('spec failed');
284
- }
285
-
286
- expect(error.type).to.be.eql(ERROR_TYPE.NOT_ONE_VALUE_OF);
287
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
288
- placeholders: [JSON.stringify(enumArr)]
289
- }));
290
- expect(error.path).to.be.eql('a');
291
- expect(error.data).to.be.deep.equal(data);
292
- expect(error.errorData).to.be.eql('a');
293
- });
294
-
295
- it('ok', () => {
296
- expect(ejv({
297
- a: 'a'
298
- }, [{
299
- key: 'a',
300
- type: 'string',
301
- notEnum: ['b', 'c']
302
- }])).to.be.null;
303
- });
304
- });
305
- });
306
-
307
- describe('length', () => {
308
- describe('check parameter', () => {
309
- const data = {
310
- a: 'ejv'
311
- };
312
-
313
- it('undefined is ok', () => {
314
- expect(ejv(data, [{
315
- key: 'a',
316
- type: 'string',
317
- length: undefined
318
- }])).to.be.null;
319
- });
320
-
321
- it('null', () => {
322
- const errorScheme: Scheme = {
323
- key: 'a',
324
- type: 'string',
325
- length: null as unknown as number
326
- };
327
-
328
- checkSchemeError({
329
- data,
330
- errorScheme,
331
- message: createErrorMsg(ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER)
332
- });
333
- });
334
-
335
- it('length type', () => {
336
- const errorScheme: Scheme = {
337
- key: 'a',
338
- type: 'string',
339
- length: '3' as unknown as number
340
- };
341
-
342
- checkSchemeError({
343
- data,
344
- errorScheme,
345
- message: createErrorMsg(ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER)
346
- });
347
- });
348
- });
349
-
350
- it('ok', () => {
351
- expect(ejv({
352
- a: 'ejv'
353
- }, [{
354
- key: 'a',
355
- type: 'string',
356
- length: 3
357
- }])).to.be.null;
358
- });
359
-
360
- it('fail', () => {
361
- const str: string = 'ejv';
362
- const data = {
363
- a: str
364
- };
365
-
366
- const error: EjvError | null = ejv(data, [{
367
- key: 'a',
368
- type: 'string',
369
- length: 4
370
- }]);
371
-
372
- expect(error).to.be.instanceof(EjvError);
373
-
374
- if (!error) {
375
- throw new Error('spec failed');
376
- }
377
-
378
- expect(error.type).to.be.eql(ERROR_TYPE.LENGTH);
379
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.LENGTH, {
380
- placeholders: [4]
381
- }));
382
- expect(error.path).to.be.eql('a');
383
- expect(error.data).to.be.deep.equal(data);
384
- expect(error.errorData).to.be.eql(str);
385
- });
386
- });
387
-
388
- describe('minLength', () => {
389
- describe('check parameter', () => {
390
- const data = {
391
- a: 'ejv'
392
- };
393
- it('undefined is ok', () => {
394
- expect(ejv(data, [{
395
- key: 'a',
396
- type: 'string',
397
- minLength: undefined
398
- }])).to.be.null;
399
- });
400
-
401
- it('null', () => {
402
- const errorScheme: Scheme = {
403
- key: 'a',
404
- type: 'string',
405
- minLength: null as unknown as number
406
- };
407
-
408
- checkSchemeError({
409
- data,
410
- errorScheme,
411
- message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER)
412
- });
413
- });
414
-
415
- it('float number', () => {
416
- const errorScheme: Scheme = {
417
- key: 'a',
418
- type: 'string',
419
- minLength: 1.5
420
- };
421
-
422
- checkSchemeError({
423
- data,
424
- errorScheme,
425
- message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER)
426
- });
427
- });
428
-
429
- it('string', () => {
430
- const errorScheme: Scheme = {
431
- key: 'a',
432
- type: 'string',
433
- minLength: '1' as unknown as number
434
- };
435
-
436
- checkSchemeError({
437
- data,
438
- errorScheme,
439
- message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER)
440
- });
441
- });
442
- });
443
-
444
- it('ok', () => {
445
- expect(ejv({
446
- a: 'ejv'
447
- }, [{
448
- key: 'a',
449
- type: 'string',
450
- minLength: 2
451
- }])).to.be.null;
452
-
453
- expect(ejv({
454
- a: 'ejv'
455
- }, [{
456
- key: 'a',
457
- type: 'string',
458
- minLength: 3
459
- }])).to.be.null;
460
- });
461
-
462
- it('fail', () => {
463
- const data = {
464
- a: 'ejv'
465
- };
466
-
467
- const error: EjvError | null = ejv(data, [{
468
- key: 'a',
469
- type: 'string',
470
- minLength: 4
471
- }]);
472
-
473
- expect(error).to.be.instanceof(EjvError);
474
-
475
- if (!error) {
476
- throw new Error('spec failed');
477
- }
478
-
479
- expect(error.type).to.be.eql(ERROR_TYPE.MIN_LENGTH);
480
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.MIN_LENGTH, {
481
- placeholders: [4]
482
- }));
483
- expect(error.path).to.be.eql('a');
484
- expect(error.data).to.be.deep.equal(data);
485
- expect(error.errorData).to.be.eql('ejv');
486
- });
487
- });
488
-
489
- describe('maxLength', () => {
490
- describe('check parameter', () => {
491
- const data = {
492
- a: 'ejv'
493
- };
494
-
495
- it('undefined is ok', () => {
496
- expect(ejv(data, [{
497
- key: 'a',
498
- type: 'string',
499
- maxLength: undefined
500
- }])).to.be.null;
501
- });
502
-
503
- it('null', () => {
504
- const errorScheme: Scheme = {
505
- key: 'a',
506
- type: 'string',
507
- maxLength: null as unknown as number
508
- };
509
-
510
- checkSchemeError({
511
- data,
512
- errorScheme,
513
- message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER)
514
- });
515
- });
516
-
517
- it('float number', () => {
518
- const errorScheme: Scheme = {
519
- key: 'a',
520
- type: 'string',
521
- maxLength: 1.5
522
- };
523
-
524
- checkSchemeError({
525
- data,
526
- errorScheme,
527
- message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER)
528
- });
529
- });
530
-
531
- it('string', () => {
532
- const errorScheme: Scheme = {
533
- key: 'a',
534
- type: 'string',
535
- maxLength: '1' as unknown as number
536
- };
537
-
538
- checkSchemeError({
539
- data,
540
- errorScheme,
541
- message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER)
542
- });
543
- });
544
- });
545
-
546
- it('fail', () => {
547
- const data = {
548
- a: 'ejv'
549
- };
550
-
551
- const error: EjvError | null = ejv(data, [{
552
- key: 'a',
553
- type: 'string',
554
- maxLength: 2
555
- }]);
556
-
557
- expect(error).to.be.instanceof(EjvError);
558
-
559
- if (!error) {
560
- throw new Error('spec failed');
561
- }
562
-
563
- expect(error.type).to.be.eql(ERROR_TYPE.MAX_LENGTH);
564
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.MAX_LENGTH, {
565
- placeholders: [2]
566
- }));
567
- expect(error.path).to.be.eql('a');
568
- expect(error.data).to.be.deep.equal(data);
569
- expect(error.errorData).to.be.eql(data.a);
570
- });
571
-
572
- it('ok', () => {
573
- expect(ejv({
574
- a: 'ejv'
575
- }, [{
576
- key: 'a',
577
- type: 'string',
578
- maxLength: 3
579
- }])).to.be.null;
580
-
581
- expect(ejv({
582
- a: 'ejv'
583
- }, [{
584
- key: 'a',
585
- type: 'string',
586
- maxLength: 4
587
- }])).to.be.null;
588
- });
589
- });
590
-
591
- describe('format', () => {
592
- describe('check parameter', () => {
593
- const data = {
594
- a: 'ejv@ejv.com'
595
- };
596
-
597
- it('undefined is ok', () => {
598
- expect(ejv(data, [{
599
- key: 'a',
600
- type: 'string',
601
- format: undefined
602
- }])).to.be.null;
603
- });
604
-
605
- it('null', () => {
606
- const errorScheme: Scheme = {
607
- key: 'a',
608
- type: 'string',
609
- format: null as unknown as string
610
- };
611
-
612
- checkSchemeError({
613
- data,
614
- errorScheme,
615
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
616
- placeholders: ['null']
617
- })
618
- });
619
- });
620
-
621
- describe('invalid string format', () => {
622
- it('single format', () => {
623
- const errorScheme: Scheme = {
624
- key: 'a',
625
- type: 'string',
626
- format: 'invalidStringFormat'
627
- };
628
-
629
- checkSchemeError({
630
- data,
631
- errorScheme,
632
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
633
- placeholders: ['invalidStringFormat']
634
- })
635
- });
636
- });
637
-
638
- it('multiple format', () => {
639
- const errorScheme: Scheme = {
640
- key: 'a',
641
- type: 'string',
642
- format: ['invalidStringFormat']
643
- };
644
-
645
- checkSchemeError({
646
- data,
647
- errorScheme,
648
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
649
- placeholders: ['invalidStringFormat']
650
- })
651
- });
652
- });
653
- });
654
- });
655
-
656
- describe('email', () => {
657
- it('single format', () => {
658
- const data = {
659
- a: 'ejv'
660
- };
661
-
662
- const error: EjvError | null = ejv(data, [{
663
- key: 'a',
664
- type: 'string',
665
- format: 'email'
666
- }]);
667
-
668
- expect(error).to.be.instanceof(EjvError);
669
-
670
- if (!error) {
671
- throw new Error('spec failed');
672
- }
673
-
674
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
675
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
676
- placeholders: ['email']
677
- }));
678
- expect(error.path).to.be.eql('a');
679
- expect(error.data).to.be.deep.equal(data);
680
- expect(error.errorData).to.be.eql('ejv');
681
-
682
- expect(ejv({
683
- a: 'ejv@ejv.com'
684
- }, [{
685
- key: 'a',
686
- type: 'string',
687
- format: 'email'
688
- }])).to.be.null;
689
- });
690
-
691
- it('multiple format', () => {
692
- const formatArr: string[] = ['email', 'date'];
693
-
694
- const data = {
695
- a: 'ejv'
696
- };
697
-
698
- const error: EjvError | null = ejv(data, [{
699
- key: 'a',
700
- type: 'string',
701
- format: formatArr
702
- }]);
703
-
704
- expect(error).to.be.instanceof(EjvError);
705
-
706
- if (!error) {
707
- throw new Error('spec failed');
708
- }
709
-
710
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
711
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
712
- placeholders: [JSON.stringify(formatArr)]
713
- }));
714
- expect(error.path).to.be.eql('a');
715
- expect(error.data).to.be.deep.equal(data);
716
- expect(error.errorData).to.be.eql('ejv');
717
-
718
- expect(ejv({
719
- a: 'ejv@ejv.com'
720
- }, [{
721
- key: 'a',
722
- type: 'string',
723
- format: formatArr
724
- }])).to.be.null;
725
- });
726
- });
727
-
728
-
729
- describe('date', () => {
730
- it('single format', () => {
731
- const data = {
732
- a: 'ejv'
733
- };
734
-
735
- const error: EjvError | null = ejv(data, [{
736
- key: 'a',
737
- type: 'string',
738
- format: 'date'
739
- }]);
740
-
741
- expect(error).to.be.instanceof(EjvError);
742
-
743
- if (!error) {
744
- throw new Error('spec failed');
745
- }
746
-
747
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
748
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
749
- placeholders: ['date']
750
- }));
751
- expect(error.path).to.be.eql('a');
752
- expect(error.data).to.be.deep.equal(data);
753
- expect(error.errorData).to.be.eql('ejv');
754
-
755
- expect(ejv({
756
- a: '2018-12-19'
757
- }, [{
758
- key: 'a',
759
- type: 'string',
760
- format: 'date'
761
- }])).to.be.null;
762
- });
763
-
764
- it('multiple format', () => {
765
- const formatArr: string[] = ['date'];
766
-
767
- const data = {
768
- a: 'ejv'
769
- };
770
-
771
- const error: EjvError | null = ejv(data, [{
772
- key: 'a',
773
- type: 'string',
774
- format: formatArr
775
- }]);
776
-
777
- expect(error).to.be.instanceof(EjvError);
778
-
779
- if (!error) {
780
- throw new Error('spec failed');
781
- }
782
-
783
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
784
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
785
- placeholders: [JSON.stringify(formatArr)]
786
- }));
787
- expect(error.path).to.be.eql('a');
788
- expect(error.data).to.be.deep.equal(data);
789
- expect(error.errorData).to.be.eql('ejv');
790
-
791
- expect(ejv({
792
- a: '2018-12-19'
793
- }, [{
794
- key: 'a',
795
- type: 'string',
796
- format: formatArr
797
- }])).to.be.null;
798
- });
799
- });
800
-
801
- describe('time', () => {
802
- it('single format', () => {
803
- const data = {
804
- a: 'ejv'
805
- };
806
-
807
- const error: EjvError | null = ejv(data, [{
808
- key: 'a',
809
- type: 'string',
810
- format: 'time'
811
- }]);
812
-
813
- expect(error).to.be.instanceof(EjvError);
814
-
815
- if (!error) {
816
- throw new Error('spec failed');
817
- }
818
-
819
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
820
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
821
- placeholders: ['time']
822
- }));
823
- expect(error.path).to.be.eql('a');
824
- expect(error.data).to.be.deep.equal(data);
825
- expect(error.errorData).to.be.eql('ejv');
826
-
827
- expect(ejv({
828
- a: '00:27:35.123'
829
- }, [{
830
- key: 'a',
831
- type: 'string',
832
- format: 'time'
833
- }])).to.be.null;
834
- });
835
-
836
- it('multiple format', () => {
837
- const formatArr: string[] = ['time'];
838
-
839
- const data = {
840
- a: 'ejv'
841
- };
842
-
843
- const error: EjvError | null = ejv(data, [{
844
- key: 'a',
845
- type: 'string',
846
- format: formatArr
847
- }]);
848
-
849
- expect(error).to.be.instanceof(EjvError);
850
-
851
- if (!error) {
852
- throw new Error('spec failed');
853
- }
854
-
855
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
856
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
857
- placeholders: [JSON.stringify(formatArr)]
858
- }));
859
- expect(error.path).to.be.eql('a');
860
- expect(error.data).to.be.deep.equal(data);
861
- expect(error.errorData).to.be.eql('ejv');
862
-
863
- expect(ejv({
864
- a: '00:27:35.123'
865
- }, [{
866
- key: 'a',
867
- type: 'string',
868
- format: formatArr
869
- }])).to.be.null;
870
- });
871
- });
872
-
873
- describe('date-time', () => {
874
- it('single format', () => {
875
- const data = {
876
- a: 'ejv'
877
- };
878
-
879
- const error: EjvError | null = ejv(data, [{
880
- key: 'a',
881
- type: 'string',
882
- format: 'date-time'
883
- }]);
884
-
885
- expect(error).to.be.instanceof(EjvError);
886
-
887
- if (!error) {
888
- throw new Error('spec failed');
889
- }
890
-
891
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
892
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
893
- placeholders: ['date-time']
894
- }));
895
- expect(error.path).to.be.eql('a');
896
- expect(error.data).to.be.deep.equal(data);
897
- expect(error.errorData).to.be.eql('ejv');
898
-
899
- expect(ejv({
900
- a: '2018-12-19T00:27:35.123Z'
901
- }, [{
902
- key: 'a',
903
- type: 'string',
904
- format: 'date-time'
905
- }])).to.be.null;
906
-
907
- expect(ejv({
908
- a: '2018-12-19T00:27:35+00:00'
909
- }, [{
910
- key: 'a',
911
- type: 'string',
912
- format: 'date-time'
913
- }])).to.be.null;
914
-
915
- expect(ejv({
916
- a: '20181219T002735Z'
917
- }, [{
918
- key: 'a',
919
- type: 'string',
920
- format: 'date-time'
921
- }])).to.be.null;
922
- });
923
-
924
- it('multiple format', () => {
925
- const formatArr: string[] = ['date-time'];
926
-
927
- const data = {
928
- a: 'ejv'
929
- };
930
-
931
- const error: EjvError | null = ejv(data, [{
932
- key: 'a',
933
- type: 'string',
934
- format: formatArr
935
- }]);
936
-
937
- expect(error).to.be.instanceof(EjvError);
938
-
939
- if (!error) {
940
- throw new Error('spec failed');
941
- }
942
-
943
- expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
944
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
945
- placeholders: [JSON.stringify(formatArr)]
946
- }));
947
- expect(error.path).to.be.eql('a');
948
- expect(error.data).to.be.deep.equal(data);
949
- expect(error.errorData).to.be.eql('ejv');
950
-
951
- expect(ejv({
952
- a: '2018-12-19T00:27:35.123Z'
953
- }, [{
954
- key: 'a',
955
- type: 'string',
956
- format: formatArr
957
- }])).to.be.null;
958
-
959
- expect(ejv({
960
- a: '2018-12-19T00:27:35+00:00'
961
- }, [{
962
- key: 'a',
963
- type: 'string',
964
- format: formatArr
965
- }])).to.be.null;
966
-
967
- expect(ejv({
968
- a: '20181219T002735Z'
969
- }, [{
970
- key: 'a',
971
- type: 'string',
972
- format: formatArr
973
- }])).to.be.null;
974
- });
975
- });
976
- });
977
-
978
- describe('pattern', () => {
979
- describe('check parameter', () => {
980
- const data = {
981
- a: 'ejv@ejv.com'
982
- };
983
-
984
- it('undefined is ok', () => {
985
- expect(ejv(data, [{
986
- key: 'a',
987
- type: 'string',
988
- pattern: undefined
989
- }])).to.be.null;
990
- });
991
-
992
- it('null', () => {
993
- const errorScheme: Scheme = {
994
- key: 'a',
995
- type: 'string',
996
- pattern: null as unknown as string
997
- };
998
-
999
- checkSchemeError({
1000
- data,
1001
- errorScheme,
1002
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1003
- placeholders: ['null']
1004
- })
1005
- });
1006
- });
1007
-
1008
- it('number', () => {
1009
- const errorScheme: Scheme = {
1010
- key: 'a',
1011
- type: 'string',
1012
- pattern: 1 as unknown as string
1013
- };
1014
-
1015
- checkSchemeError({
1016
- data,
1017
- errorScheme,
1018
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1019
- placeholders: ['1']
1020
- })
1021
- });
1022
- });
1023
-
1024
- it('empty string', () => {
1025
- const errorScheme: Scheme = {
1026
- key: 'a',
1027
- type: 'string',
1028
- pattern: ''
1029
- };
1030
-
1031
- checkSchemeError({
1032
- data,
1033
- errorScheme,
1034
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1035
- placeholders: ['//']
1036
- })
1037
- });
1038
- });
1039
-
1040
- it('empty array', () => {
1041
- const errorScheme: Scheme = {
1042
- key: 'a',
1043
- type: 'string',
1044
- pattern: []
1045
- };
1046
-
1047
- checkSchemeError({
1048
- data,
1049
- errorScheme,
1050
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1051
- placeholders: ['[]']
1052
- })
1053
- });
1054
- });
1055
-
1056
- it('null array', () => {
1057
- const errorScheme: Scheme = {
1058
- key: 'a',
1059
- type: 'string',
1060
- pattern: [null as unknown as RegExp, /ab/]
1061
- };
1062
-
1063
- checkSchemeError({
1064
- data,
1065
- errorScheme,
1066
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1067
- placeholders: ['[/null/, /ab/]']
1068
- })
1069
- });
1070
- });
1071
-
1072
- it('number array', () => {
1073
- const errorScheme: Scheme = {
1074
- key: 'a',
1075
- type: 'string',
1076
- pattern: [1, 3] as unknown as string[]
1077
- };
1078
-
1079
- checkSchemeError({
1080
- data,
1081
- errorScheme,
1082
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1083
- placeholders: ['[1, 3]']
1084
- })
1085
- });
1086
- });
1087
-
1088
- it('empty string array', () => {
1089
- const errorScheme: Scheme = {
1090
- key: 'a',
1091
- type: 'string',
1092
- pattern: ['']
1093
- };
1094
-
1095
- checkSchemeError({
1096
- data,
1097
- errorScheme,
1098
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1099
- placeholders: ['[//]']
1100
- })
1101
- });
1102
- });
1103
-
1104
- it('empty reg exp', () => {
1105
- const errorScheme: Scheme = {
1106
- key: 'a',
1107
- type: 'string',
1108
- pattern: new RegExp('')
1109
- };
1110
-
1111
- checkSchemeError({
1112
- data,
1113
- errorScheme,
1114
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1115
- placeholders: ['//']
1116
- })
1117
- });
1118
- });
1119
-
1120
- it('null reg exp', () => {
1121
- const errorScheme: Scheme = {
1122
- key: 'a',
1123
- type: 'string',
1124
- pattern: new RegExp(null as unknown as string)
1125
- };
1126
-
1127
- checkSchemeError({
1128
- data,
1129
- errorScheme,
1130
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1131
- placeholders: ['/null/']
1132
- })
1133
- });
1134
- });
1135
-
1136
- it('empty reg exp array', () => {
1137
- const errorScheme: Scheme = {
1138
- key: 'a',
1139
- type: 'string',
1140
- pattern: [new RegExp('')]
1141
- };
1142
-
1143
- checkSchemeError({
1144
- data,
1145
- errorScheme,
1146
- message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1147
- placeholders: ['[//]']
1148
- })
1149
- });
1150
- });
1151
- });
1152
-
1153
- it('by string', () => {
1154
- const str: string = 'abc';
1155
- const data = {
1156
- a: str
1157
- };
1158
-
1159
- expect(ejv(data, [{
1160
- key: 'a',
1161
- type: 'string',
1162
- pattern: 'ab+c'
1163
- }])).to.be.null;
1164
-
1165
-
1166
- const error: EjvError | null = ejv(data, [{
1167
- key: 'a',
1168
- type: 'string',
1169
- pattern: 'ac'
1170
- }]);
1171
-
1172
- expect(error).to.be.instanceof(EjvError);
1173
-
1174
- if (!error) {
1175
- throw new Error('spec failed');
1176
- }
1177
-
1178
- expect(error.type).to.be.eql(ERROR_TYPE.PATTERN);
1179
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN, {
1180
- placeholders: ['/ac/']
1181
- }));
1182
- expect(error.path).to.be.eql('a');
1183
- expect(error.data).to.be.deep.equal(data);
1184
- expect(error.errorData).to.be.eql(str);
1185
- });
1186
-
1187
- it('by string[]', () => {
1188
- const str: string = 'abc';
1189
- const data = {
1190
- a: str
1191
- };
1192
-
1193
- expect(ejv(data, [{
1194
- key: 'a',
1195
- type: 'string',
1196
- pattern: ['ab+c']
1197
- }])).to.be.null;
1198
-
1199
- expect(ejv(data, [{
1200
- key: 'a',
1201
- type: 'string',
1202
- pattern: ['ac', 'ab+c']
1203
- }])).to.be.null;
1204
-
1205
- expect(ejv(data, [{
1206
- key: 'a',
1207
- type: 'string',
1208
- pattern: ['ab+c', 'ac']
1209
- }])).to.be.null;
1210
-
1211
-
1212
- const error: EjvError | null = ejv(data, [{
1213
- key: 'a',
1214
- type: 'string',
1215
- pattern: ['abcc', 'ac']
1216
- }]);
1217
-
1218
- expect(error).to.be.instanceof(EjvError);
1219
-
1220
- if (!error) {
1221
- throw new Error('spec failed');
1222
- }
1223
-
1224
- expect(error.type).to.be.eql(ERROR_TYPE.PATTERN_ONE_OF);
1225
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN_ONE_OF, {
1226
- placeholders: ['[/abcc/, /ac/]']
1227
- }));
1228
- expect(error.path).to.be.eql('a');
1229
- expect(error.data).to.be.deep.equal(data);
1230
- expect(error.errorData).to.be.eql(str);
1231
- });
1232
-
1233
- it('by RegExp', () => {
1234
- const str: string = 'abc';
1235
- const data = {
1236
- a: str
1237
- };
1238
-
1239
- expect(ejv(data, [{
1240
- key: 'a',
1241
- type: 'string',
1242
- pattern: /ab+c/
1243
- }])).to.be.null;
1244
-
1245
-
1246
- const error: EjvError | null = ejv(data, [{
1247
- key: 'a',
1248
- type: 'string',
1249
- pattern: /ac/
1250
- }]);
1251
-
1252
- expect(error).to.be.instanceof(EjvError);
1253
-
1254
- if (!error) {
1255
- throw new Error('spec failed');
1256
- }
1257
-
1258
- expect(error.type).to.be.eql(ERROR_TYPE.PATTERN);
1259
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN, {
1260
- placeholders: [/ac/.toString()]
1261
- }));
1262
- expect(error.path).to.be.eql('a');
1263
- expect(error.data).to.be.deep.equal(data);
1264
- expect(error.errorData).to.be.eql(str);
1265
- });
1266
-
1267
- it('by RegExp[]', () => {
1268
- const str: string = 'abc';
1269
- const data = {
1270
- a: str
1271
- };
1272
-
1273
- expect(ejv(data, [{
1274
- key: 'a',
1275
- type: 'string',
1276
- pattern: [/ab+c/]
1277
- }])).to.be.null;
1278
-
1279
- expect(ejv(data, [{
1280
- key: 'a',
1281
- type: 'string',
1282
- pattern: [/ac/, /ab+c/]
1283
- }])).to.be.null;
1284
-
1285
- expect(ejv(data, [{
1286
- key: 'a',
1287
- type: 'string',
1288
- pattern: [/ab+c/, /ac/]
1289
- }])).to.be.null;
1290
-
1291
-
1292
- const error: EjvError | null = ejv(data, [{
1293
- key: 'a',
1294
- type: 'string',
1295
- pattern: [/abcc/, /ac/]
1296
- }]);
1297
-
1298
- expect(error).to.be.instanceof(EjvError);
1299
-
1300
- if (!error) {
1301
- throw new Error('spec failed');
1302
- }
1303
-
1304
- expect(error.type).to.be.eql(ERROR_TYPE.PATTERN_ONE_OF);
1305
- expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN_ONE_OF, {
1306
- placeholders: ['[/abcc/, /ac/]']
1307
- }));
1308
- expect(error.path).to.be.eql('a');
1309
- expect(error.data).to.be.deep.equal(data);
1310
- expect(error.errorData).to.be.eql(str);
1311
- });
1312
-
1313
- describe('special case', () => {
1314
- it('array of object has string', () => {
1315
- const error: EjvError | null = ejv({
1316
- a: [{
1317
- b: 'ejv'
1318
- }]
1319
- }, [{
1320
- key: 'a',
1321
- type: 'array',
1322
- items: [{
1323
- type: 'object',
1324
- properties: [{
1325
- key: 'b',
1326
- type: 'string',
1327
- pattern: /ejv/
1328
- }]
1329
- }]
1330
- }]);
1331
-
1332
- expect(error).to.be.null;
1333
- });
1334
- });
1335
- });
1336
- });
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('StringScheme', () => {
13
+ describe('type', () => {
14
+ describe('mismatch', () => {
15
+ typeTesterArr
16
+ .filter((obj: TypeTester): boolean => obj.type !== 'string')
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: 'string'
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: ['string']
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 = 'ejv';
46
+ const typeArr: string[] = ['boolean', 'number'];
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: 'string',
80
+ optional: true
81
+ }])).to.be.null;
82
+ });
83
+
84
+ it('single type', () => {
85
+ expect(ejv({
86
+ a: 'ejv'
87
+ }, [{
88
+ key: 'a',
89
+ type: 'string'
90
+ }])).to.be.null;
91
+ });
92
+
93
+ it('multiple types', () => {
94
+ expect(ejv({
95
+ a: 'ejv'
96
+ }, [{
97
+ key: 'a',
98
+ type: ['string', 'number']
99
+ }])).to.be.null;
100
+ });
101
+
102
+ it('multiple types', () => {
103
+ expect(ejv({
104
+ a: 'ejv'
105
+ }, [{
106
+ key: 'a',
107
+ type: ['number', 'string']
108
+ }])).to.be.null;
109
+ });
110
+ });
111
+ });
112
+
113
+ describe('enum & notEnum', () => {
114
+ describe('enum', () => {
115
+ describe('check parameter', () => {
116
+ const data = {
117
+ a: 'a'
118
+ };
119
+
120
+ it('undefined is ok', () => {
121
+ expect(ejv(data, [{
122
+ key: 'a',
123
+ type: 'string',
124
+ enum: undefined
125
+ }])).to.be.null;
126
+ });
127
+
128
+ it('null', () => {
129
+ const errorScheme: Scheme = {
130
+ key: 'a',
131
+ type: 'string',
132
+ enum: null as unknown as string[]
133
+ };
134
+
135
+ checkSchemeError({
136
+ data,
137
+ errorScheme,
138
+ message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
139
+ });
140
+ });
141
+
142
+ it('not array', () => {
143
+ const errorScheme: Scheme = {
144
+ key: 'a',
145
+ type: 'string',
146
+ enum: 'a' as unknown as string[]
147
+ };
148
+
149
+ checkSchemeError({
150
+ data,
151
+ errorScheme,
152
+ message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_ARRAY)
153
+ });
154
+ });
155
+
156
+ it('not string', () => {
157
+ const errorScheme: Scheme = {
158
+ key: 'a',
159
+ type: 'string',
160
+ enum: [10]
161
+ };
162
+
163
+ checkSchemeError({
164
+ data,
165
+ errorScheme,
166
+ message: createErrorMsg(ERROR_MESSAGE.ENUM_SHOULD_BE_STRINGS)
167
+ });
168
+ });
169
+ });
170
+
171
+ it('fail', () => {
172
+ const enumArr: string[] = ['b', 'c'];
173
+
174
+ const data = {
175
+ a: 'a'
176
+ };
177
+
178
+ const error: EjvError | null = ejv(data, [{
179
+ key: 'a',
180
+ type: 'string',
181
+ enum: enumArr
182
+ }]);
183
+
184
+ expect(error).to.be.instanceof(EjvError);
185
+
186
+ if (!error) {
187
+ throw new Error('spec failed');
188
+ }
189
+
190
+ expect(error.type).to.be.eql(ERROR_TYPE.ONE_VALUE_OF);
191
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.ONE_VALUE_OF, {
192
+ placeholders: [JSON.stringify(enumArr)]
193
+ }));
194
+ expect(error.path).to.be.eql('a');
195
+ expect(error.data).to.be.deep.equal(data);
196
+ expect(error.errorData).to.be.eql('a');
197
+ });
198
+
199
+ it('ok', () => {
200
+ expect(ejv({
201
+ a: 'a'
202
+ }, [{
203
+ key: 'a',
204
+ type: 'string',
205
+ enum: ['a', 'b', 'c']
206
+ }])).to.be.null;
207
+ });
208
+ });
209
+
210
+ describe('notEnum', () => {
211
+ describe('check parameter', () => {
212
+ const data = {
213
+ a: 'a'
214
+ };
215
+
216
+ it('undefined is ok', () => {
217
+ expect(ejv(data, [{
218
+ key: 'a',
219
+ type: 'string',
220
+ notEnum: undefined
221
+ }])).to.be.null;
222
+ });
223
+
224
+ it('null', () => {
225
+ const errorScheme: Scheme = {
226
+ key: 'a',
227
+ type: 'string',
228
+ notEnum: null as unknown as string[]
229
+ };
230
+
231
+ checkSchemeError({
232
+ data,
233
+ errorScheme,
234
+ message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
235
+ });
236
+ });
237
+
238
+ it('not array', () => {
239
+ const errorScheme: Scheme = {
240
+ key: 'a',
241
+ type: 'string',
242
+ notEnum: 'a' as unknown as string[]
243
+ };
244
+
245
+ checkSchemeError({
246
+ data,
247
+ errorScheme,
248
+ message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_ARRAY)
249
+ });
250
+ });
251
+
252
+ it('not string', () => {
253
+ const errorScheme: Scheme = {
254
+ key: 'a',
255
+ type: 'string',
256
+ notEnum: [10]
257
+ };
258
+
259
+ checkSchemeError({
260
+ data,
261
+ errorScheme,
262
+ message: createErrorMsg(ERROR_MESSAGE.NOT_ENUM_SHOULD_BE_STRINGS)
263
+ });
264
+ });
265
+ });
266
+
267
+ it('fail', () => {
268
+ const enumArr: string[] = ['a', 'b', 'c'];
269
+
270
+ const data = {
271
+ a: 'a'
272
+ };
273
+
274
+ const error: EjvError | null = ejv(data, [{
275
+ key: 'a',
276
+ type: 'string',
277
+ notEnum: enumArr
278
+ }]);
279
+
280
+ expect(error).to.be.instanceof(EjvError);
281
+
282
+ if (!error) {
283
+ throw new Error('spec failed');
284
+ }
285
+
286
+ expect(error.type).to.be.eql(ERROR_TYPE.NOT_ONE_VALUE_OF);
287
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.NOT_ONE_VALUE_OF, {
288
+ placeholders: [JSON.stringify(enumArr)]
289
+ }));
290
+ expect(error.path).to.be.eql('a');
291
+ expect(error.data).to.be.deep.equal(data);
292
+ expect(error.errorData).to.be.eql('a');
293
+ });
294
+
295
+ it('ok', () => {
296
+ expect(ejv({
297
+ a: 'a'
298
+ }, [{
299
+ key: 'a',
300
+ type: 'string',
301
+ notEnum: ['b', 'c']
302
+ }])).to.be.null;
303
+ });
304
+ });
305
+ });
306
+
307
+ describe('length', () => {
308
+ describe('check parameter', () => {
309
+ const data = {
310
+ a: 'ejv'
311
+ };
312
+
313
+ it('undefined is ok', () => {
314
+ expect(ejv(data, [{
315
+ key: 'a',
316
+ type: 'string',
317
+ length: undefined
318
+ }])).to.be.null;
319
+ });
320
+
321
+ it('null', () => {
322
+ const errorScheme: Scheme = {
323
+ key: 'a',
324
+ type: 'string',
325
+ length: null as unknown as number
326
+ };
327
+
328
+ checkSchemeError({
329
+ data,
330
+ errorScheme,
331
+ message: createErrorMsg(ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER)
332
+ });
333
+ });
334
+
335
+ it('length type', () => {
336
+ const errorScheme: Scheme = {
337
+ key: 'a',
338
+ type: 'string',
339
+ length: '3' as unknown as number
340
+ };
341
+
342
+ checkSchemeError({
343
+ data,
344
+ errorScheme,
345
+ message: createErrorMsg(ERROR_MESSAGE.LENGTH_SHOULD_BE_INTEGER)
346
+ });
347
+ });
348
+ });
349
+
350
+ it('ok', () => {
351
+ expect(ejv({
352
+ a: 'ejv'
353
+ }, [{
354
+ key: 'a',
355
+ type: 'string',
356
+ length: 3
357
+ }])).to.be.null;
358
+ });
359
+
360
+ it('fail', () => {
361
+ const str: string = 'ejv';
362
+ const data = {
363
+ a: str
364
+ };
365
+
366
+ const error: EjvError | null = ejv(data, [{
367
+ key: 'a',
368
+ type: 'string',
369
+ length: 4
370
+ }]);
371
+
372
+ expect(error).to.be.instanceof(EjvError);
373
+
374
+ if (!error) {
375
+ throw new Error('spec failed');
376
+ }
377
+
378
+ expect(error.type).to.be.eql(ERROR_TYPE.LENGTH);
379
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.LENGTH, {
380
+ placeholders: [4]
381
+ }));
382
+ expect(error.path).to.be.eql('a');
383
+ expect(error.data).to.be.deep.equal(data);
384
+ expect(error.errorData).to.be.eql(str);
385
+ });
386
+ });
387
+
388
+ describe('minLength', () => {
389
+ describe('check parameter', () => {
390
+ const data = {
391
+ a: 'ejv'
392
+ };
393
+ it('undefined is ok', () => {
394
+ expect(ejv(data, [{
395
+ key: 'a',
396
+ type: 'string',
397
+ minLength: undefined
398
+ }])).to.be.null;
399
+ });
400
+
401
+ it('null', () => {
402
+ const errorScheme: Scheme = {
403
+ key: 'a',
404
+ type: 'string',
405
+ minLength: null as unknown as number
406
+ };
407
+
408
+ checkSchemeError({
409
+ data,
410
+ errorScheme,
411
+ message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER)
412
+ });
413
+ });
414
+
415
+ it('float number', () => {
416
+ const errorScheme: Scheme = {
417
+ key: 'a',
418
+ type: 'string',
419
+ minLength: 1.5
420
+ };
421
+
422
+ checkSchemeError({
423
+ data,
424
+ errorScheme,
425
+ message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER)
426
+ });
427
+ });
428
+
429
+ it('string', () => {
430
+ const errorScheme: Scheme = {
431
+ key: 'a',
432
+ type: 'string',
433
+ minLength: '1' as unknown as number
434
+ };
435
+
436
+ checkSchemeError({
437
+ data,
438
+ errorScheme,
439
+ message: createErrorMsg(ERROR_MESSAGE.MIN_LENGTH_SHOULD_BE_INTEGER)
440
+ });
441
+ });
442
+ });
443
+
444
+ it('ok', () => {
445
+ expect(ejv({
446
+ a: 'ejv'
447
+ }, [{
448
+ key: 'a',
449
+ type: 'string',
450
+ minLength: 2
451
+ }])).to.be.null;
452
+
453
+ expect(ejv({
454
+ a: 'ejv'
455
+ }, [{
456
+ key: 'a',
457
+ type: 'string',
458
+ minLength: 3
459
+ }])).to.be.null;
460
+ });
461
+
462
+ it('fail', () => {
463
+ const data = {
464
+ a: 'ejv'
465
+ };
466
+
467
+ const error: EjvError | null = ejv(data, [{
468
+ key: 'a',
469
+ type: 'string',
470
+ minLength: 4
471
+ }]);
472
+
473
+ expect(error).to.be.instanceof(EjvError);
474
+
475
+ if (!error) {
476
+ throw new Error('spec failed');
477
+ }
478
+
479
+ expect(error.type).to.be.eql(ERROR_TYPE.MIN_LENGTH);
480
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.MIN_LENGTH, {
481
+ placeholders: [4]
482
+ }));
483
+ expect(error.path).to.be.eql('a');
484
+ expect(error.data).to.be.deep.equal(data);
485
+ expect(error.errorData).to.be.eql('ejv');
486
+ });
487
+ });
488
+
489
+ describe('maxLength', () => {
490
+ describe('check parameter', () => {
491
+ const data = {
492
+ a: 'ejv'
493
+ };
494
+
495
+ it('undefined is ok', () => {
496
+ expect(ejv(data, [{
497
+ key: 'a',
498
+ type: 'string',
499
+ maxLength: undefined
500
+ }])).to.be.null;
501
+ });
502
+
503
+ it('null', () => {
504
+ const errorScheme: Scheme = {
505
+ key: 'a',
506
+ type: 'string',
507
+ maxLength: null as unknown as number
508
+ };
509
+
510
+ checkSchemeError({
511
+ data,
512
+ errorScheme,
513
+ message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER)
514
+ });
515
+ });
516
+
517
+ it('float number', () => {
518
+ const errorScheme: Scheme = {
519
+ key: 'a',
520
+ type: 'string',
521
+ maxLength: 1.5
522
+ };
523
+
524
+ checkSchemeError({
525
+ data,
526
+ errorScheme,
527
+ message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER)
528
+ });
529
+ });
530
+
531
+ it('string', () => {
532
+ const errorScheme: Scheme = {
533
+ key: 'a',
534
+ type: 'string',
535
+ maxLength: '1' as unknown as number
536
+ };
537
+
538
+ checkSchemeError({
539
+ data,
540
+ errorScheme,
541
+ message: createErrorMsg(ERROR_MESSAGE.MAX_LENGTH_SHOULD_BE_INTEGER)
542
+ });
543
+ });
544
+ });
545
+
546
+ it('fail', () => {
547
+ const data = {
548
+ a: 'ejv'
549
+ };
550
+
551
+ const error: EjvError | null = ejv(data, [{
552
+ key: 'a',
553
+ type: 'string',
554
+ maxLength: 2
555
+ }]);
556
+
557
+ expect(error).to.be.instanceof(EjvError);
558
+
559
+ if (!error) {
560
+ throw new Error('spec failed');
561
+ }
562
+
563
+ expect(error.type).to.be.eql(ERROR_TYPE.MAX_LENGTH);
564
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.MAX_LENGTH, {
565
+ placeholders: [2]
566
+ }));
567
+ expect(error.path).to.be.eql('a');
568
+ expect(error.data).to.be.deep.equal(data);
569
+ expect(error.errorData).to.be.eql(data.a);
570
+ });
571
+
572
+ it('ok', () => {
573
+ expect(ejv({
574
+ a: 'ejv'
575
+ }, [{
576
+ key: 'a',
577
+ type: 'string',
578
+ maxLength: 3
579
+ }])).to.be.null;
580
+
581
+ expect(ejv({
582
+ a: 'ejv'
583
+ }, [{
584
+ key: 'a',
585
+ type: 'string',
586
+ maxLength: 4
587
+ }])).to.be.null;
588
+ });
589
+ });
590
+
591
+ describe('format', () => {
592
+ describe('check parameter', () => {
593
+ const data = {
594
+ a: 'ejv@ejv.com'
595
+ };
596
+
597
+ it('undefined is ok', () => {
598
+ expect(ejv(data, [{
599
+ key: 'a',
600
+ type: 'string',
601
+ format: undefined
602
+ }])).to.be.null;
603
+ });
604
+
605
+ it('null', () => {
606
+ const errorScheme: Scheme = {
607
+ key: 'a',
608
+ type: 'string',
609
+ format: null as unknown as string
610
+ };
611
+
612
+ checkSchemeError({
613
+ data,
614
+ errorScheme,
615
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
616
+ placeholders: ['null']
617
+ })
618
+ });
619
+ });
620
+
621
+ describe('invalid string format', () => {
622
+ it('single format', () => {
623
+ const errorScheme: Scheme = {
624
+ key: 'a',
625
+ type: 'string',
626
+ format: 'invalidStringFormat'
627
+ };
628
+
629
+ checkSchemeError({
630
+ data,
631
+ errorScheme,
632
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
633
+ placeholders: ['invalidStringFormat']
634
+ })
635
+ });
636
+ });
637
+
638
+ it('multiple format', () => {
639
+ const errorScheme: Scheme = {
640
+ key: 'a',
641
+ type: 'string',
642
+ format: ['invalidStringFormat']
643
+ };
644
+
645
+ checkSchemeError({
646
+ data,
647
+ errorScheme,
648
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_FORMAT, {
649
+ placeholders: ['invalidStringFormat']
650
+ })
651
+ });
652
+ });
653
+ });
654
+ });
655
+
656
+ describe('email', () => {
657
+ it('single format', () => {
658
+ const data = {
659
+ a: 'ejv'
660
+ };
661
+
662
+ const error: EjvError | null = ejv(data, [{
663
+ key: 'a',
664
+ type: 'string',
665
+ format: 'email'
666
+ }]);
667
+
668
+ expect(error).to.be.instanceof(EjvError);
669
+
670
+ if (!error) {
671
+ throw new Error('spec failed');
672
+ }
673
+
674
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
675
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
676
+ placeholders: ['email']
677
+ }));
678
+ expect(error.path).to.be.eql('a');
679
+ expect(error.data).to.be.deep.equal(data);
680
+ expect(error.errorData).to.be.eql('ejv');
681
+
682
+ expect(ejv({
683
+ a: 'ejv@ejv.com'
684
+ }, [{
685
+ key: 'a',
686
+ type: 'string',
687
+ format: 'email'
688
+ }])).to.be.null;
689
+ });
690
+
691
+ it('multiple format', () => {
692
+ const formatArr: string[] = ['email', 'date'];
693
+
694
+ const data = {
695
+ a: 'ejv'
696
+ };
697
+
698
+ const error: EjvError | null = ejv(data, [{
699
+ key: 'a',
700
+ type: 'string',
701
+ format: formatArr
702
+ }]);
703
+
704
+ expect(error).to.be.instanceof(EjvError);
705
+
706
+ if (!error) {
707
+ throw new Error('spec failed');
708
+ }
709
+
710
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
711
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
712
+ placeholders: [JSON.stringify(formatArr)]
713
+ }));
714
+ expect(error.path).to.be.eql('a');
715
+ expect(error.data).to.be.deep.equal(data);
716
+ expect(error.errorData).to.be.eql('ejv');
717
+
718
+ expect(ejv({
719
+ a: 'ejv@ejv.com'
720
+ }, [{
721
+ key: 'a',
722
+ type: 'string',
723
+ format: formatArr
724
+ }])).to.be.null;
725
+ });
726
+ });
727
+
728
+ describe('json', () => {
729
+ it('single format', () => {
730
+ const data = {
731
+ a: 'ejv'
732
+ };
733
+
734
+ const error: EjvError | null = ejv(data, [{
735
+ key: 'a',
736
+ type: 'string',
737
+ format: 'json'
738
+ }]);
739
+
740
+ expect(error).to.be.instanceof(EjvError);
741
+
742
+ if (!error) {
743
+ throw new Error('spec failed');
744
+ }
745
+
746
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
747
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
748
+ placeholders: ['json']
749
+ }));
750
+ expect(error.path).to.be.eql('a');
751
+ expect(error.data).to.be.deep.equal(data);
752
+ expect(error.errorData).to.be.eql('ejv');
753
+
754
+ expect(ejv({
755
+ a: JSON.stringify({ key: 'value' })
756
+ }, [{
757
+ key: 'a',
758
+ type: 'string',
759
+ format: 'json'
760
+ }])).to.be.null;
761
+ });
762
+
763
+ it('multiple format', () => {
764
+ const formatArr: string[] = ['json', 'date'];
765
+
766
+ const data = {
767
+ a: 'ejv'
768
+ };
769
+
770
+ const error: EjvError | null = ejv(data, [{
771
+ key: 'a',
772
+ type: 'string',
773
+ format: formatArr
774
+ }]);
775
+
776
+ expect(error).to.be.instanceof(EjvError);
777
+
778
+ if (!error) {
779
+ throw new Error('spec failed');
780
+ }
781
+
782
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
783
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
784
+ placeholders: [JSON.stringify(formatArr)]
785
+ }));
786
+ expect(error.path).to.be.eql('a');
787
+ expect(error.data).to.be.deep.equal(data);
788
+ expect(error.errorData).to.be.eql('ejv');
789
+
790
+ expect(ejv({
791
+ a: JSON.stringify({ key: 'value' })
792
+ }, [{
793
+ key: 'a',
794
+ type: 'string',
795
+ format: formatArr
796
+ }])).to.be.null;
797
+ });
798
+ });
799
+
800
+ describe('date', () => {
801
+ it('single format', () => {
802
+ const data = {
803
+ a: 'ejv'
804
+ };
805
+
806
+ const error: EjvError | null = ejv(data, [{
807
+ key: 'a',
808
+ type: 'string',
809
+ format: 'date'
810
+ }]);
811
+
812
+ expect(error).to.be.instanceof(EjvError);
813
+
814
+ if (!error) {
815
+ throw new Error('spec failed');
816
+ }
817
+
818
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
819
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
820
+ placeholders: ['date']
821
+ }));
822
+ expect(error.path).to.be.eql('a');
823
+ expect(error.data).to.be.deep.equal(data);
824
+ expect(error.errorData).to.be.eql('ejv');
825
+
826
+ expect(ejv({
827
+ a: '2018-12-19'
828
+ }, [{
829
+ key: 'a',
830
+ type: 'string',
831
+ format: 'date'
832
+ }])).to.be.null;
833
+ });
834
+
835
+ it('multiple format', () => {
836
+ const formatArr: string[] = ['date'];
837
+
838
+ const data = {
839
+ a: 'ejv'
840
+ };
841
+
842
+ const error: EjvError | null = ejv(data, [{
843
+ key: 'a',
844
+ type: 'string',
845
+ format: formatArr
846
+ }]);
847
+
848
+ expect(error).to.be.instanceof(EjvError);
849
+
850
+ if (!error) {
851
+ throw new Error('spec failed');
852
+ }
853
+
854
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
855
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
856
+ placeholders: [JSON.stringify(formatArr)]
857
+ }));
858
+ expect(error.path).to.be.eql('a');
859
+ expect(error.data).to.be.deep.equal(data);
860
+ expect(error.errorData).to.be.eql('ejv');
861
+
862
+ expect(ejv({
863
+ a: '2018-12-19'
864
+ }, [{
865
+ key: 'a',
866
+ type: 'string',
867
+ format: formatArr
868
+ }])).to.be.null;
869
+ });
870
+ });
871
+
872
+ describe('time', () => {
873
+ it('single format', () => {
874
+ const data = {
875
+ a: 'ejv'
876
+ };
877
+
878
+ const error: EjvError | null = ejv(data, [{
879
+ key: 'a',
880
+ type: 'string',
881
+ format: 'time'
882
+ }]);
883
+
884
+ expect(error).to.be.instanceof(EjvError);
885
+
886
+ if (!error) {
887
+ throw new Error('spec failed');
888
+ }
889
+
890
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
891
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
892
+ placeholders: ['time']
893
+ }));
894
+ expect(error.path).to.be.eql('a');
895
+ expect(error.data).to.be.deep.equal(data);
896
+ expect(error.errorData).to.be.eql('ejv');
897
+
898
+ expect(ejv({
899
+ a: '00:27:35.123'
900
+ }, [{
901
+ key: 'a',
902
+ type: 'string',
903
+ format: 'time'
904
+ }])).to.be.null;
905
+ });
906
+
907
+ it('multiple format', () => {
908
+ const formatArr: string[] = ['time'];
909
+
910
+ const data = {
911
+ a: 'ejv'
912
+ };
913
+
914
+ const error: EjvError | null = ejv(data, [{
915
+ key: 'a',
916
+ type: 'string',
917
+ format: formatArr
918
+ }]);
919
+
920
+ expect(error).to.be.instanceof(EjvError);
921
+
922
+ if (!error) {
923
+ throw new Error('spec failed');
924
+ }
925
+
926
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
927
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
928
+ placeholders: [JSON.stringify(formatArr)]
929
+ }));
930
+ expect(error.path).to.be.eql('a');
931
+ expect(error.data).to.be.deep.equal(data);
932
+ expect(error.errorData).to.be.eql('ejv');
933
+
934
+ expect(ejv({
935
+ a: '00:27:35.123'
936
+ }, [{
937
+ key: 'a',
938
+ type: 'string',
939
+ format: formatArr
940
+ }])).to.be.null;
941
+ });
942
+ });
943
+
944
+ describe('date-time', () => {
945
+ it('single format', () => {
946
+ const data = {
947
+ a: 'ejv'
948
+ };
949
+
950
+ const error: EjvError | null = ejv(data, [{
951
+ key: 'a',
952
+ type: 'string',
953
+ format: 'date-time'
954
+ }]);
955
+
956
+ expect(error).to.be.instanceof(EjvError);
957
+
958
+ if (!error) {
959
+ throw new Error('spec failed');
960
+ }
961
+
962
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT);
963
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT, {
964
+ placeholders: ['date-time']
965
+ }));
966
+ expect(error.path).to.be.eql('a');
967
+ expect(error.data).to.be.deep.equal(data);
968
+ expect(error.errorData).to.be.eql('ejv');
969
+
970
+ expect(ejv({
971
+ a: '2018-12-19T00:27:35.123Z'
972
+ }, [{
973
+ key: 'a',
974
+ type: 'string',
975
+ format: 'date-time'
976
+ }])).to.be.null;
977
+
978
+ expect(ejv({
979
+ a: '2018-12-19T00:27:35+00:00'
980
+ }, [{
981
+ key: 'a',
982
+ type: 'string',
983
+ format: 'date-time'
984
+ }])).to.be.null;
985
+
986
+ expect(ejv({
987
+ a: '20181219T002735Z'
988
+ }, [{
989
+ key: 'a',
990
+ type: 'string',
991
+ format: 'date-time'
992
+ }])).to.be.null;
993
+ });
994
+
995
+ it('multiple format', () => {
996
+ const formatArr: string[] = ['date-time'];
997
+
998
+ const data = {
999
+ a: 'ejv'
1000
+ };
1001
+
1002
+ const error: EjvError | null = ejv(data, [{
1003
+ key: 'a',
1004
+ type: 'string',
1005
+ format: formatArr
1006
+ }]);
1007
+
1008
+ expect(error).to.be.instanceof(EjvError);
1009
+
1010
+ if (!error) {
1011
+ throw new Error('spec failed');
1012
+ }
1013
+
1014
+ expect(error.type).to.be.eql(ERROR_TYPE.FORMAT_ONE_OF);
1015
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.FORMAT_ONE_OF, {
1016
+ placeholders: [JSON.stringify(formatArr)]
1017
+ }));
1018
+ expect(error.path).to.be.eql('a');
1019
+ expect(error.data).to.be.deep.equal(data);
1020
+ expect(error.errorData).to.be.eql('ejv');
1021
+
1022
+ expect(ejv({
1023
+ a: '2018-12-19T00:27:35.123Z'
1024
+ }, [{
1025
+ key: 'a',
1026
+ type: 'string',
1027
+ format: formatArr
1028
+ }])).to.be.null;
1029
+
1030
+ expect(ejv({
1031
+ a: '2018-12-19T00:27:35+00:00'
1032
+ }, [{
1033
+ key: 'a',
1034
+ type: 'string',
1035
+ format: formatArr
1036
+ }])).to.be.null;
1037
+
1038
+ expect(ejv({
1039
+ a: '20181219T002735Z'
1040
+ }, [{
1041
+ key: 'a',
1042
+ type: 'string',
1043
+ format: formatArr
1044
+ }])).to.be.null;
1045
+ });
1046
+ });
1047
+ });
1048
+
1049
+ describe('pattern', () => {
1050
+ describe('check parameter', () => {
1051
+ const data = {
1052
+ a: 'ejv@ejv.com'
1053
+ };
1054
+
1055
+ it('undefined is ok', () => {
1056
+ expect(ejv(data, [{
1057
+ key: 'a',
1058
+ type: 'string',
1059
+ pattern: undefined
1060
+ }])).to.be.null;
1061
+ });
1062
+
1063
+ it('null', () => {
1064
+ const errorScheme: Scheme = {
1065
+ key: 'a',
1066
+ type: 'string',
1067
+ pattern: null as unknown as string
1068
+ };
1069
+
1070
+ checkSchemeError({
1071
+ data,
1072
+ errorScheme,
1073
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1074
+ placeholders: ['null']
1075
+ })
1076
+ });
1077
+ });
1078
+
1079
+ it('number', () => {
1080
+ const errorScheme: Scheme = {
1081
+ key: 'a',
1082
+ type: 'string',
1083
+ pattern: 1 as unknown as string
1084
+ };
1085
+
1086
+ checkSchemeError({
1087
+ data,
1088
+ errorScheme,
1089
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1090
+ placeholders: ['1']
1091
+ })
1092
+ });
1093
+ });
1094
+
1095
+ it('empty string', () => {
1096
+ const errorScheme: Scheme = {
1097
+ key: 'a',
1098
+ type: 'string',
1099
+ pattern: ''
1100
+ };
1101
+
1102
+ checkSchemeError({
1103
+ data,
1104
+ errorScheme,
1105
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1106
+ placeholders: ['//']
1107
+ })
1108
+ });
1109
+ });
1110
+
1111
+ it('empty array', () => {
1112
+ const errorScheme: Scheme = {
1113
+ key: 'a',
1114
+ type: 'string',
1115
+ pattern: []
1116
+ };
1117
+
1118
+ checkSchemeError({
1119
+ data,
1120
+ errorScheme,
1121
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1122
+ placeholders: ['[]']
1123
+ })
1124
+ });
1125
+ });
1126
+
1127
+ it('null array', () => {
1128
+ const errorScheme: Scheme = {
1129
+ key: 'a',
1130
+ type: 'string',
1131
+ pattern: [null as unknown as RegExp, /ab/]
1132
+ };
1133
+
1134
+ checkSchemeError({
1135
+ data,
1136
+ errorScheme,
1137
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1138
+ placeholders: ['[/null/, /ab/]']
1139
+ })
1140
+ });
1141
+ });
1142
+
1143
+ it('number array', () => {
1144
+ const errorScheme: Scheme = {
1145
+ key: 'a',
1146
+ type: 'string',
1147
+ pattern: [1, 3] as unknown as string[]
1148
+ };
1149
+
1150
+ checkSchemeError({
1151
+ data,
1152
+ errorScheme,
1153
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1154
+ placeholders: ['[1, 3]']
1155
+ })
1156
+ });
1157
+ });
1158
+
1159
+ it('empty string array', () => {
1160
+ const errorScheme: Scheme = {
1161
+ key: 'a',
1162
+ type: 'string',
1163
+ pattern: ['']
1164
+ };
1165
+
1166
+ checkSchemeError({
1167
+ data,
1168
+ errorScheme,
1169
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1170
+ placeholders: ['[//]']
1171
+ })
1172
+ });
1173
+ });
1174
+
1175
+ it('empty reg exp', () => {
1176
+ const errorScheme: Scheme = {
1177
+ key: 'a',
1178
+ type: 'string',
1179
+ pattern: new RegExp('')
1180
+ };
1181
+
1182
+ checkSchemeError({
1183
+ data,
1184
+ errorScheme,
1185
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1186
+ placeholders: ['//']
1187
+ })
1188
+ });
1189
+ });
1190
+
1191
+ it('null reg exp', () => {
1192
+ const errorScheme: Scheme = {
1193
+ key: 'a',
1194
+ type: 'string',
1195
+ pattern: new RegExp(null as unknown as string)
1196
+ };
1197
+
1198
+ checkSchemeError({
1199
+ data,
1200
+ errorScheme,
1201
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1202
+ placeholders: ['/null/']
1203
+ })
1204
+ });
1205
+ });
1206
+
1207
+ it('empty reg exp array', () => {
1208
+ const errorScheme: Scheme = {
1209
+ key: 'a',
1210
+ type: 'string',
1211
+ pattern: [new RegExp('')]
1212
+ };
1213
+
1214
+ checkSchemeError({
1215
+ data,
1216
+ errorScheme,
1217
+ message: createErrorMsg(ERROR_MESSAGE.INVALID_STRING_PATTERN, {
1218
+ placeholders: ['[//]']
1219
+ })
1220
+ });
1221
+ });
1222
+ });
1223
+
1224
+ it('by string', () => {
1225
+ const str: string = 'abc';
1226
+ const data = {
1227
+ a: str
1228
+ };
1229
+
1230
+ expect(ejv(data, [{
1231
+ key: 'a',
1232
+ type: 'string',
1233
+ pattern: 'ab+c'
1234
+ }])).to.be.null;
1235
+
1236
+
1237
+ const error: EjvError | null = ejv(data, [{
1238
+ key: 'a',
1239
+ type: 'string',
1240
+ pattern: 'ac'
1241
+ }]);
1242
+
1243
+ expect(error).to.be.instanceof(EjvError);
1244
+
1245
+ if (!error) {
1246
+ throw new Error('spec failed');
1247
+ }
1248
+
1249
+ expect(error.type).to.be.eql(ERROR_TYPE.PATTERN);
1250
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN, {
1251
+ placeholders: ['/ac/']
1252
+ }));
1253
+ expect(error.path).to.be.eql('a');
1254
+ expect(error.data).to.be.deep.equal(data);
1255
+ expect(error.errorData).to.be.eql(str);
1256
+ });
1257
+
1258
+ it('by string[]', () => {
1259
+ const str: string = 'abc';
1260
+ const data = {
1261
+ a: str
1262
+ };
1263
+
1264
+ expect(ejv(data, [{
1265
+ key: 'a',
1266
+ type: 'string',
1267
+ pattern: ['ab+c']
1268
+ }])).to.be.null;
1269
+
1270
+ expect(ejv(data, [{
1271
+ key: 'a',
1272
+ type: 'string',
1273
+ pattern: ['ac', 'ab+c']
1274
+ }])).to.be.null;
1275
+
1276
+ expect(ejv(data, [{
1277
+ key: 'a',
1278
+ type: 'string',
1279
+ pattern: ['ab+c', 'ac']
1280
+ }])).to.be.null;
1281
+
1282
+
1283
+ const error: EjvError | null = ejv(data, [{
1284
+ key: 'a',
1285
+ type: 'string',
1286
+ pattern: ['abcc', 'ac']
1287
+ }]);
1288
+
1289
+ expect(error).to.be.instanceof(EjvError);
1290
+
1291
+ if (!error) {
1292
+ throw new Error('spec failed');
1293
+ }
1294
+
1295
+ expect(error.type).to.be.eql(ERROR_TYPE.PATTERN_ONE_OF);
1296
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN_ONE_OF, {
1297
+ placeholders: ['[/abcc/, /ac/]']
1298
+ }));
1299
+ expect(error.path).to.be.eql('a');
1300
+ expect(error.data).to.be.deep.equal(data);
1301
+ expect(error.errorData).to.be.eql(str);
1302
+ });
1303
+
1304
+ it('by RegExp', () => {
1305
+ const str: string = 'abc';
1306
+ const data = {
1307
+ a: str
1308
+ };
1309
+
1310
+ expect(ejv(data, [{
1311
+ key: 'a',
1312
+ type: 'string',
1313
+ pattern: /ab+c/
1314
+ }])).to.be.null;
1315
+
1316
+
1317
+ const error: EjvError | null = ejv(data, [{
1318
+ key: 'a',
1319
+ type: 'string',
1320
+ pattern: /ac/
1321
+ }]);
1322
+
1323
+ expect(error).to.be.instanceof(EjvError);
1324
+
1325
+ if (!error) {
1326
+ throw new Error('spec failed');
1327
+ }
1328
+
1329
+ expect(error.type).to.be.eql(ERROR_TYPE.PATTERN);
1330
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN, {
1331
+ placeholders: [/ac/.toString()]
1332
+ }));
1333
+ expect(error.path).to.be.eql('a');
1334
+ expect(error.data).to.be.deep.equal(data);
1335
+ expect(error.errorData).to.be.eql(str);
1336
+ });
1337
+
1338
+ it('by RegExp[]', () => {
1339
+ const str: string = 'abc';
1340
+ const data = {
1341
+ a: str
1342
+ };
1343
+
1344
+ expect(ejv(data, [{
1345
+ key: 'a',
1346
+ type: 'string',
1347
+ pattern: [/ab+c/]
1348
+ }])).to.be.null;
1349
+
1350
+ expect(ejv(data, [{
1351
+ key: 'a',
1352
+ type: 'string',
1353
+ pattern: [/ac/, /ab+c/]
1354
+ }])).to.be.null;
1355
+
1356
+ expect(ejv(data, [{
1357
+ key: 'a',
1358
+ type: 'string',
1359
+ pattern: [/ab+c/, /ac/]
1360
+ }])).to.be.null;
1361
+
1362
+
1363
+ const error: EjvError | null = ejv(data, [{
1364
+ key: 'a',
1365
+ type: 'string',
1366
+ pattern: [/abcc/, /ac/]
1367
+ }]);
1368
+
1369
+ expect(error).to.be.instanceof(EjvError);
1370
+
1371
+ if (!error) {
1372
+ throw new Error('spec failed');
1373
+ }
1374
+
1375
+ expect(error.type).to.be.eql(ERROR_TYPE.PATTERN_ONE_OF);
1376
+ expect(error.message).to.be.eql(createErrorMsg(ERROR_MESSAGE.PATTERN_ONE_OF, {
1377
+ placeholders: ['[/abcc/, /ac/]']
1378
+ }));
1379
+ expect(error.path).to.be.eql('a');
1380
+ expect(error.data).to.be.deep.equal(data);
1381
+ expect(error.errorData).to.be.eql(str);
1382
+ });
1383
+
1384
+ describe('special case', () => {
1385
+ it('array of object has string', () => {
1386
+ const error: EjvError | null = ejv({
1387
+ a: [{
1388
+ b: 'ejv'
1389
+ }]
1390
+ }, [{
1391
+ key: 'a',
1392
+ type: 'array',
1393
+ items: [{
1394
+ type: 'object',
1395
+ properties: [{
1396
+ key: 'b',
1397
+ type: 'string',
1398
+ pattern: /ejv/
1399
+ }]
1400
+ }]
1401
+ }]);
1402
+
1403
+ expect(error).to.be.null;
1404
+ });
1405
+ });
1406
+ });
1407
+ });