ejv 1.1.11 → 2.0.0

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