co.validation 2.7.2 → 3.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 (41) hide show
  1. package/lib/createValidation.d.ts +6 -0
  2. package/lib/createValidation.d.ts.map +1 -0
  3. package/lib/createValidation.js +153 -204
  4. package/lib/createValidation.js.map +1 -0
  5. package/lib/helpers.d.ts +2 -0
  6. package/lib/helpers.d.ts.map +1 -0
  7. package/lib/helpers.js +4 -8
  8. package/lib/helpers.js.map +1 -0
  9. package/lib/index.d.ts +7 -0
  10. package/lib/index.d.ts.map +1 -0
  11. package/lib/index.js +18 -38
  12. package/lib/index.js.map +1 -0
  13. package/lib/messageProcessor.d.ts +7 -0
  14. package/lib/messageProcessor.d.ts.map +1 -0
  15. package/lib/messageProcessor.js +45 -51
  16. package/lib/messageProcessor.js.map +1 -0
  17. package/lib/operators.d.ts +4 -0
  18. package/lib/operators.d.ts.map +1 -0
  19. package/lib/operators.js +60 -71
  20. package/lib/operators.js.map +1 -0
  21. package/lib/types.d.ts +123 -0
  22. package/lib/types.d.ts.map +1 -0
  23. package/lib/types.js +3 -0
  24. package/lib/types.js.map +1 -0
  25. package/lib/validators.d.ts +18 -0
  26. package/lib/validators.d.ts.map +1 -0
  27. package/lib/validators.js +164 -196
  28. package/lib/validators.js.map +1 -0
  29. package/package.json +13 -21
  30. package/tsconfig.json +32 -0
  31. package/lib/__tests__/component.arrayValidation.js +0 -201
  32. package/lib/__tests__/component.objectValidation.js +0 -556
  33. package/lib/__tests__/component.reduxFormValidation.js +0 -20
  34. package/lib/__tests__/component.valueValidation.js +0 -923
  35. package/lib/__tests__/integration.objectValidation.js +0 -64
  36. package/lib/__tests__/integration.registrationForm.js +0 -234
  37. package/lib/__tests__/integration.valueValidation.js +0 -448
  38. package/lib/__tests__/operators.js +0 -28
  39. package/lib/__tests__/unit.operators.js +0 -125
  40. package/lib/createObjectValidation.js +0 -34
  41. package/lib/createValueValidation.js +0 -71
@@ -1,923 +0,0 @@
1
- "use strict";
2
-
3
- var _ = require("..");
4
-
5
- describe('createValidation', () => {
6
- it('supports any', () => {
7
- expect((0, _.createValidation)({
8
- type: 'any'
9
- })('')).toEqual({
10
- data: '',
11
- errors: undefined
12
- });
13
- expect((0, _.createValidation)({
14
- type: 'any'
15
- })(5)).toEqual({
16
- data: 5,
17
- errors: undefined
18
- });
19
- });
20
- it('supports required', () => {
21
- expect((0, _.createValidation)({
22
- type: 'required'
23
- })('')).toEqual({
24
- data: undefined,
25
- errors: ['Required']
26
- });
27
- expect((0, _.createValidation)({
28
- type: 'required',
29
- getMessage: () => 'Required Field'
30
- })('')).toEqual({
31
- data: undefined,
32
- errors: ['Required Field']
33
- });
34
- expect((0, _.createValidation)({
35
- type: 'required',
36
- getMessage: () => 'Required'
37
- })('a')).toEqual({
38
- data: 'a',
39
- errors: undefined
40
- });
41
- expect((0, _.createValidation)({
42
- type: 'required',
43
- getMessage: () => 'Required'
44
- })(5)).toEqual({
45
- data: 5,
46
- errors: undefined
47
- });
48
- expect((0, _.createValidation)({
49
- type: 'required',
50
- getMessage: () => 'Required'
51
- })(0)).toEqual({
52
- data: 0,
53
- errors: undefined
54
- });
55
- expect((0, _.createValidation)({
56
- type: 'required',
57
- getMessage: () => 'Required'
58
- })(false)).toEqual({
59
- data: false,
60
- errors: undefined
61
- });
62
- });
63
- it('supports custom', () => {
64
- const biggerThanX = (value, allValues) => {
65
- if (value < allValues.x) {
66
- throw 'bigger';
67
- }
68
-
69
- return value;
70
- };
71
-
72
- expect((0, _.createValidation)({
73
- type: 'custom',
74
- validator: biggerThanX
75
- })(5, {
76
- x: 10
77
- })).toEqual({
78
- data: undefined,
79
- errors: ['Error']
80
- });
81
- expect((0, _.createValidation)({
82
- type: 'custom',
83
- validator: biggerThanX,
84
- getMessage: bigger => bigger
85
- })(5, {
86
- x: 10
87
- })).toEqual({
88
- data: undefined,
89
- errors: ['bigger']
90
- });
91
- expect((0, _.createValidation)({
92
- type: 'custom',
93
- validator: biggerThanX,
94
- getMessage: () => 'Required'
95
- })(50, {
96
- x: 10
97
- })).toEqual({
98
- data: 50,
99
- errors: undefined
100
- });
101
- });
102
- it('supports equals', () => {
103
- expect((0, _.createValidation)({
104
- type: 'equals',
105
- to: '123'
106
- })('1234')).toEqual({
107
- data: undefined,
108
- errors: ['Must be equal to 123']
109
- });
110
- expect((0, _.createValidation)({
111
- type: 'equals',
112
- to: 123
113
- })('123')).toEqual({
114
- data: undefined,
115
- errors: ['Must be equal to 123']
116
- });
117
- expect((0, _.createValidation)({
118
- type: 'equals',
119
- to: true
120
- })(1)).toEqual({
121
- data: undefined,
122
- errors: ['Must be equal to true']
123
- });
124
- expect((0, _.createValidation)({
125
- type: 'equals',
126
- to: '123',
127
- getMessage: to => to
128
- })('1234')).toEqual({
129
- data: undefined,
130
- errors: ['123']
131
- });
132
- expect((0, _.createValidation)({
133
- type: 'equals',
134
- to: '123'
135
- })('123')).toEqual({
136
- data: '123',
137
- errors: undefined
138
- });
139
- expect((0, _.createValidation)({
140
- type: 'equals',
141
- to: 123
142
- })(123)).toEqual({
143
- data: 123,
144
- errors: undefined
145
- });
146
- expect((0, _.createValidation)({
147
- type: 'equals',
148
- to: true
149
- })(true)).toEqual({
150
- data: true,
151
- errors: undefined
152
- });
153
- expect((0, _.createValidation)({
154
- type: 'equals',
155
- to: false
156
- })(false)).toEqual({
157
- data: false,
158
- errors: undefined
159
- });
160
- });
161
- it('supports equalsToField', () => {
162
- expect((0, _.createValidation)({
163
- type: 'equalsToField',
164
- field: 'otherField'
165
- })('1234', {
166
- otherField: '123'
167
- })).toEqual({
168
- data: undefined,
169
- errors: ['Must be equal to otherField']
170
- });
171
- expect((0, _.createValidation)({
172
- type: 'equalsToField',
173
- field: 'otherField'
174
- })(null, {
175
- otherField: '123'
176
- })).toEqual({
177
- data: undefined,
178
- errors: ['Must be equal to otherField']
179
- });
180
- expect((0, _.createValidation)({
181
- type: 'equalsToField',
182
- field: 'otherField'
183
- })('123', {
184
- otherField: 123
185
- })).toEqual({
186
- data: undefined,
187
- errors: ['Must be equal to otherField']
188
- });
189
- expect((0, _.createValidation)({
190
- type: 'equalsToField',
191
- field: 'otherField'
192
- })(1, {
193
- otherField: true
194
- })).toEqual({
195
- data: undefined,
196
- errors: ['Must be equal to otherField']
197
- });
198
- expect((0, _.createValidation)({
199
- type: 'equalsToField',
200
- field: 'otherField',
201
- getMessage: to => to
202
- })('1234', {
203
- otherField: '123'
204
- })).toEqual({
205
- data: undefined,
206
- errors: ['otherField']
207
- });
208
- expect((0, _.createValidation)({
209
- type: 'equalsToField',
210
- field: 'otherField'
211
- })('123', {
212
- otherField: '123'
213
- })).toEqual({
214
- data: '123',
215
- errors: undefined
216
- });
217
- expect((0, _.createValidation)({
218
- type: 'equalsToField',
219
- field: 'otherField'
220
- })(123, {
221
- otherField: 123
222
- })).toEqual({
223
- data: 123,
224
- errors: undefined
225
- });
226
- expect((0, _.createValidation)({
227
- type: 'equalsToField',
228
- field: 'otherField'
229
- })(true, {
230
- otherField: true
231
- })).toEqual({
232
- data: true,
233
- errors: undefined
234
- });
235
- expect((0, _.createValidation)({
236
- type: 'equalsToField',
237
- field: 'otherField'
238
- })(false, {
239
- otherField: false
240
- })).toEqual({
241
- data: false,
242
- errors: undefined
243
- });
244
- });
245
- it('supports regex', () => {
246
- expect((0, _.createValidation)({
247
- type: 'regex',
248
- regex: /^[0-9]*$/i
249
- })('1234a')).toEqual({
250
- data: undefined,
251
- errors: ['Invalid format']
252
- });
253
- expect((0, _.createValidation)({
254
- type: 'regex',
255
- regex: /^[0-9]*$/i
256
- })('123 ')).toEqual({
257
- data: undefined,
258
- errors: ['Invalid format']
259
- });
260
- expect((0, _.createValidation)({
261
- type: 'regex',
262
- regex: /^[0-9]*$/i
263
- })(1)).toEqual({
264
- data: 1,
265
- errors: undefined
266
- });
267
- expect((0, _.createValidation)({
268
- type: 'regex',
269
- regex: /^[0-9]*$/i,
270
- getMessage: to => 'test'
271
- })('1234a')).toEqual({
272
- data: undefined,
273
- errors: ['test']
274
- });
275
- expect((0, _.createValidation)({
276
- type: 'regex',
277
- regex: /^[0-9]*$/i
278
- })('123')).toEqual({
279
- data: '123',
280
- errors: undefined
281
- });
282
- });
283
- it('supports maxLength', () => {
284
- expect((0, _.createValidation)({
285
- type: 'maxLength',
286
- max: 3
287
- })('1234')).toEqual({
288
- data: undefined,
289
- errors: ['Maximum number of characters - 3']
290
- });
291
- expect((0, _.createValidation)({
292
- type: 'maxLength',
293
- max: 3,
294
- getMessage: max => max
295
- })('1234')).toEqual({
296
- data: undefined,
297
- errors: ['3']
298
- });
299
- expect((0, _.createValidation)({
300
- type: 'maxLength',
301
- max: 3,
302
- getMessage: max => max
303
- })('123')).toEqual({
304
- data: '123',
305
- errors: undefined
306
- });
307
- expect((0, _.createValidation)({
308
- type: 'maxLength',
309
- max: 3,
310
- getMessage: max => max
311
- })('12')).toEqual({
312
- data: '12',
313
- errors: undefined
314
- });
315
- });
316
- it('supports maxLength with emoji', () => {
317
- expect((0, _.createValidation)({
318
- type: 'maxLength',
319
- max: 4,
320
- getMessage: max => max
321
- })('❤️123')).toEqual({
322
- data: '❤️123',
323
- errors: undefined
324
- });
325
- expect((0, _.createValidation)({
326
- type: 'maxLength',
327
- max: 3
328
- })('❤️123')).toEqual({
329
- data: undefined,
330
- errors: ['Maximum number of characters - 3']
331
- });
332
- });
333
- it('supports minLength', () => {
334
- expect((0, _.createValidation)({
335
- type: 'minLength',
336
- min: 3,
337
- getMessage: min => min
338
- })('1234')).toEqual({
339
- data: '1234',
340
- errors: undefined
341
- });
342
- expect((0, _.createValidation)({
343
- type: 'minLength',
344
- min: 3,
345
- getMessage: min => min
346
- })('123')).toEqual({
347
- data: '123',
348
- errors: undefined
349
- });
350
- expect((0, _.createValidation)({
351
- type: 'minLength',
352
- min: 3,
353
- getMessage: min => min
354
- })('12')).toEqual({
355
- data: undefined,
356
- errors: ['3']
357
- });
358
- expect((0, _.createValidation)({
359
- type: 'minLength',
360
- min: 3
361
- })('12')).toEqual({
362
- data: undefined,
363
- errors: ['Minimum number of characters - 3']
364
- });
365
- });
366
- it('supports minLength with emoji', () => {
367
- expect((0, _.createValidation)({
368
- type: 'minLength',
369
- min: 3,
370
- getMessage: min => min
371
- })('❤️')).toEqual({
372
- data: undefined,
373
- errors: ['3']
374
- });
375
- expect((0, _.createValidation)({
376
- type: 'minLength',
377
- min: 3
378
- })('❤️1')).toEqual({
379
- data: undefined,
380
- errors: ['Minimum number of characters - 3']
381
- });
382
- expect((0, _.createValidation)({
383
- type: 'minLength',
384
- min: 3
385
- })('❤️123')).toEqual({
386
- data: '❤️123',
387
- errors: undefined
388
- });
389
- });
390
- it('supports number', () => {
391
- expect((0, _.createValidation)({
392
- type: 'number'
393
- })('1234')).toEqual({
394
- data: 1234,
395
- errors: undefined
396
- });
397
- expect((0, _.createValidation)({
398
- type: 'number'
399
- })('1234,5')).toEqual({
400
- data: 1234.5,
401
- errors: undefined
402
- });
403
- expect((0, _.createValidation)({
404
- type: 'number'
405
- })('1234.5')).toEqual({
406
- data: 1234.5,
407
- errors: undefined
408
- });
409
- expect((0, _.createValidation)({
410
- type: 'number'
411
- })(123)).toEqual({
412
- data: 123,
413
- errors: undefined
414
- });
415
- expect((0, _.createValidation)({
416
- type: 'number'
417
- })(null)).toEqual({
418
- data: null,
419
- errors: undefined
420
- });
421
- expect((0, _.createValidation)({
422
- type: 'number'
423
- })(undefined)).toEqual({
424
- data: undefined,
425
- errors: undefined
426
- });
427
- expect((0, _.createValidation)({
428
- type: 'number'
429
- })('')).toEqual({
430
- data: null
431
- });
432
- expect((0, _.createValidation)({
433
- type: 'number',
434
- getMessage: () => 'Number'
435
- })('12x')).toEqual({
436
- data: undefined,
437
- errors: ['Number']
438
- });
439
- expect((0, _.createValidation)({
440
- type: 'number'
441
- })('12x')).toEqual({
442
- data: undefined,
443
- errors: ['Must be a number']
444
- });
445
- });
446
- it('supports minValue', () => {
447
- expect((0, _.createValidation)({
448
- type: 'minValue',
449
- min: 1
450
- })('')).toEqual({
451
- data: null
452
- });
453
- expect((0, _.createValidation)({
454
- type: 'minValue',
455
- min: 1
456
- })(0)).toEqual({
457
- data: undefined,
458
- errors: ['Must be at least 1']
459
- });
460
- expect((0, _.createValidation)({
461
- type: 'minValue',
462
- min: 1
463
- })(-1)).toEqual({
464
- data: undefined,
465
- errors: ['Must be at least 1']
466
- });
467
- expect((0, _.createValidation)({
468
- type: 'minValue',
469
- min: 3,
470
- getMessage: () => 'MinValue'
471
- })(123)).toEqual({
472
- data: 123,
473
- errors: undefined
474
- });
475
- expect((0, _.createValidation)({
476
- type: 'minValue',
477
- min: 233,
478
- getMessage: () => 'MinValue'
479
- })(123)).toEqual({
480
- data: undefined,
481
- errors: ['MinValue']
482
- });
483
- expect((0, _.createValidation)({
484
- type: 'minValue',
485
- min: 333
486
- })(22)).toEqual({
487
- data: undefined,
488
- errors: ['Must be at least 333']
489
- });
490
- });
491
- it('supports maxValue', () => {
492
- expect((0, _.createValidation)({
493
- type: 'maxValue',
494
- max: 10
495
- })('')).toEqual({
496
- data: null
497
- });
498
- expect((0, _.createValidation)({
499
- type: 'maxValue',
500
- max: 10
501
- })(11)).toEqual({
502
- data: undefined,
503
- errors: ['Must be not more than 10']
504
- });
505
- expect((0, _.createValidation)({
506
- type: 'maxValue',
507
- max: 10
508
- })(15)).toEqual({
509
- data: undefined,
510
- errors: ['Must be not more than 10']
511
- });
512
- expect((0, _.createValidation)({
513
- type: 'maxValue',
514
- max: 50,
515
- getMessage: () => 'MaxValue'
516
- })(25)).toEqual({
517
- data: 25,
518
- errors: undefined
519
- });
520
- expect((0, _.createValidation)({
521
- type: 'maxValue',
522
- max: 5,
523
- getMessage: () => 'MaxValue'
524
- })(10)).toEqual({
525
- data: undefined,
526
- errors: ['MaxValue']
527
- });
528
- expect((0, _.createValidation)({
529
- type: 'maxValue',
530
- max: 100
531
- })(200)).toEqual({
532
- data: undefined,
533
- errors: ['Must be not more than 100']
534
- });
535
- });
536
- it('supports string', () => {
537
- expect((0, _.createValidation)({
538
- type: 'string',
539
- getMessage: () => 'String'
540
- })('1234')).toEqual({
541
- data: '1234',
542
- errors: undefined
543
- });
544
- expect((0, _.createValidation)({
545
- type: 'string',
546
- getMessage: () => 'String'
547
- })('o ya !')).toEqual({
548
- data: 'o ya !',
549
- errors: undefined
550
- });
551
- expect((0, _.createValidation)({
552
- type: 'string',
553
- getMessage: () => 'String'
554
- })(12)).toEqual({
555
- data: undefined,
556
- errors: ['String']
557
- });
558
- expect((0, _.createValidation)({
559
- type: 'string'
560
- })(true)).toEqual({
561
- data: undefined,
562
- errors: ['Must be a string']
563
- });
564
- });
565
- it('supports boolean', () => {
566
- expect((0, _.createValidation)({
567
- type: 'boolean',
568
- getMessage: () => 'Boolean'
569
- })(undefined)).toEqual({
570
- data: undefined,
571
- errors: undefined
572
- });
573
- expect((0, _.createValidation)({
574
- type: 'boolean',
575
- getMessage: () => 'Boolean'
576
- })(true)).toEqual({
577
- data: true,
578
- errors: undefined
579
- });
580
- expect((0, _.createValidation)({
581
- type: 'boolean',
582
- getMessage: () => 'Boolean'
583
- })(false)).toEqual({
584
- data: false,
585
- errors: undefined
586
- });
587
- expect((0, _.createValidation)({
588
- type: 'boolean',
589
- getMessage: () => 'Boolean'
590
- })('true')).toEqual({
591
- data: true,
592
- errors: undefined
593
- });
594
- expect((0, _.createValidation)({
595
- type: 'boolean',
596
- getMessage: () => 'Boolean'
597
- })('false')).toEqual({
598
- data: false,
599
- errors: undefined
600
- });
601
- expect((0, _.createValidation)({
602
- type: 'boolean',
603
- getMessage: () => 'Boolean'
604
- })('FAlse')).toEqual({
605
- data: false,
606
- errors: undefined
607
- });
608
- expect((0, _.createValidation)({
609
- type: 'boolean',
610
- getMessage: () => 'Boolean'
611
- })('truec')).toEqual({
612
- data: undefined,
613
- errors: ['Boolean']
614
- });
615
- expect((0, _.createValidation)({
616
- type: 'boolean',
617
- getMessage: () => 'Boolean'
618
- })(0)).toEqual({
619
- data: undefined,
620
- errors: ['Boolean']
621
- });
622
- expect((0, _.createValidation)({
623
- type: 'boolean'
624
- })('truec')).toEqual({
625
- data: undefined,
626
- errors: ['Must be a boolean']
627
- });
628
- });
629
- it('supports email', () => {
630
- expect((0, _.createValidation)({
631
- type: 'email',
632
- getMessage: () => 'Wrong email'
633
- })('test@test.com')).toEqual({
634
- data: 'test@test.com',
635
- errors: undefined
636
- });
637
- expect((0, _.createValidation)({
638
- type: 'email',
639
- getMessage: () => 'Wrong email'
640
- })('email')).toEqual({
641
- data: undefined,
642
- errors: ['Wrong email']
643
- });
644
- expect((0, _.createValidation)({
645
- type: 'email',
646
- getMessage: () => 'Wrong email'
647
- })('email@')).toEqual({
648
- data: undefined,
649
- errors: ['Wrong email']
650
- });
651
- expect((0, _.createValidation)({
652
- type: 'email',
653
- getMessage: () => 'Wrong email'
654
- })('email@.com')).toEqual({
655
- data: undefined,
656
- errors: ['Wrong email']
657
- });
658
- expect((0, _.createValidation)({
659
- type: 'email'
660
- })('email@.com')).toEqual({
661
- data: undefined,
662
- errors: ['Invalid email format']
663
- });
664
- expect((0, _.createValidation)({
665
- type: 'email'
666
- })('email.@mail.com')).toEqual({
667
- data: undefined,
668
- errors: ['Invalid email format']
669
- });
670
- });
671
- it('supports alphanumeric', () => {
672
- expect((0, _.createValidation)({
673
- type: 'alphanumeric',
674
- getMessage: () => 'alphanumeric'
675
- })('te123Aa')).toEqual({
676
- data: 'te123Aa',
677
- errors: undefined
678
- });
679
- expect((0, _.createValidation)({
680
- type: 'alphanumeric',
681
- getMessage: () => 'alphanumeric'
682
- })('asd_')).toEqual({
683
- data: undefined,
684
- errors: ['alphanumeric']
685
- });
686
- expect((0, _.createValidation)({
687
- type: 'alphanumeric'
688
- })('Aa 1')).toEqual({
689
- data: undefined,
690
- errors: ['Only alphanumeric characters']
691
- });
692
- });
693
- it('supports alphanumeric', () => {
694
- expect((0, _.createValidation)({
695
- type: 'alphanumeric',
696
- getMessage: () => 'alphanumeric'
697
- })('te123Aa')).toEqual({
698
- data: 'te123Aa',
699
- errors: undefined
700
- });
701
- expect((0, _.createValidation)({
702
- type: 'alphanumeric',
703
- getMessage: () => 'alphanumeric'
704
- })('asd_')).toEqual({
705
- data: undefined,
706
- errors: ['alphanumeric']
707
- });
708
- expect((0, _.createValidation)({
709
- type: 'alphanumeric'
710
- })('Aa 1')).toEqual({
711
- data: undefined,
712
- errors: ['Only alphanumeric characters']
713
- });
714
- });
715
- it('supports oneOf', () => {
716
- expect((0, _.createValidation)({
717
- type: 'oneOf',
718
- validators: [{
719
- type: 'equals',
720
- to: 1,
721
- getMessage: to => to
722
- }, {
723
- type: 'equals',
724
- to: 2,
725
- getMessage: to => to
726
- }]
727
- })('3')).toEqual({
728
- data: undefined,
729
- errors: ['1', '2']
730
- });
731
- expect((0, _.createValidation)({
732
- type: 'oneOf',
733
- validators: [{
734
- type: 'equals',
735
- to: 1,
736
- getMessage: to => to
737
- }, {
738
- type: 'equals',
739
- to: 2,
740
- getMessage: to => to
741
- }]
742
- })(1)).toEqual({
743
- data: 1,
744
- errors: undefined
745
- });
746
- expect((0, _.createValidation)({
747
- type: 'oneOf',
748
- validators: [{
749
- type: 'number',
750
- getMessage: () => 'Number'
751
- }, {
752
- type: 'email',
753
- getMessage: () => 'Email'
754
- }]
755
- })('xz')).toEqual({
756
- data: undefined,
757
- errors: ['Number', 'Email']
758
- });
759
- expect((0, _.createValidation)({
760
- type: 'oneOf',
761
- validators: [{
762
- type: 'number',
763
- getMessage: () => 'Number'
764
- }, {
765
- type: 'email',
766
- getMessage: () => 'Email'
767
- }]
768
- })('123')).toEqual({
769
- data: 123,
770
- errors: undefined
771
- });
772
- expect((0, _.createValidation)({
773
- type: 'oneOf',
774
- validators: [{
775
- type: 'number',
776
- getMessage: () => 'Number'
777
- }, {
778
- type: 'email',
779
- getMessage: () => 'Email'
780
- }]
781
- })('test@test.com')).toEqual({
782
- data: 'test@test.com',
783
- errors: undefined
784
- });
785
- });
786
- it('supports all', () => {
787
- expect((0, _.createValidation)({
788
- type: 'all',
789
- validators: [{
790
- type: 'email',
791
- getMessage: () => 'Email'
792
- }, {
793
- type: 'maxLength',
794
- max: 1,
795
- getMessage: () => 'MaxLength'
796
- }]
797
- })('xz')).toEqual({
798
- data: undefined,
799
- errors: ['Email', 'MaxLength']
800
- });
801
- expect((0, _.createValidation)({
802
- type: 'all',
803
- validators: [{
804
- type: 'number',
805
- getMessage: () => 'Number'
806
- }, {
807
- type: 'maxValue',
808
- max: 11,
809
- getMessage: () => 'MaxValue'
810
- }]
811
- })('12')).toEqual({
812
- data: undefined,
813
- errors: ['MaxValue']
814
- });
815
- expect((0, _.createValidation)({
816
- type: 'all',
817
- validators: [{
818
- type: 'email',
819
- getMessage: () => 'Email'
820
- }, {
821
- type: 'maxLength',
822
- max: 1,
823
- getMessage: () => 'MaxLength'
824
- }]
825
- })('x')).toEqual({
826
- data: undefined,
827
- errors: ['Email']
828
- });
829
- expect((0, _.createValidation)({
830
- type: 'all',
831
- validators: [{
832
- type: 'number',
833
- getMessage: () => 'Number'
834
- }, {
835
- type: 'maxValue',
836
- max: 1,
837
- getMessage: () => 'MaxValue'
838
- }]
839
- })('1')).toEqual({
840
- data: 1,
841
- errors: undefined
842
- });
843
- });
844
- it('supports nesting of all/oneOf', () => {
845
- expect((0, _.createValidation)({
846
- type: 'all',
847
- validators: [{
848
- type: 'oneOf',
849
- validators: [{
850
- type: 'number',
851
- getMessage: () => 'Number'
852
- }, {
853
- type: 'email',
854
- getMessage: () => 'Email'
855
- }]
856
- }, {
857
- type: 'maxLength',
858
- max: 1,
859
- getMessage: () => 'MaxLength'
860
- }]
861
- })('xz')).toEqual({
862
- data: undefined,
863
- errors: ['Number', 'Email', 'MaxLength']
864
- });
865
- expect((0, _.createValidation)({
866
- type: 'all',
867
- validators: [{
868
- type: 'oneOf',
869
- validators: [{
870
- type: 'number',
871
- getMessage: () => 'Number'
872
- }, {
873
- type: 'email',
874
- getMessage: () => 'Email'
875
- }]
876
- }, {
877
- type: 'maxLength',
878
- max: 15,
879
- getMessage: () => 'MaxLength'
880
- }]
881
- })('test@test.com')).toEqual({
882
- data: 'test@test.com',
883
- errors: undefined
884
- });
885
- });
886
- it('can handle login validation', () => {
887
- const loginValidation = (0, _.createValidation)({
888
- type: 'all',
889
- validators: [{
890
- type: 'required',
891
- getMessage: () => 'Required'
892
- }, {
893
- type: 'oneOf',
894
- validators: [{
895
- type: 'all',
896
- validators: [{
897
- type: 'alphanumeric',
898
- getMessage: () => 'AlphaNumeric'
899
- }, {
900
- type: 'minLength',
901
- min: 3,
902
- getMessage: () => '{min}'
903
- }]
904
- }, {
905
- type: 'email',
906
- getMessage: () => 'Email'
907
- }]
908
- }]
909
- });
910
- expect(loginValidation('')).toEqual({
911
- data: undefined,
912
- errors: ['Required']
913
- });
914
- expect(loginValidation('aa')).toEqual({
915
- data: undefined,
916
- errors: ['3', 'Email']
917
- });
918
- expect(loginValidation('aa3')).toEqual({
919
- data: 'aa3',
920
- errors: undefined
921
- });
922
- });
923
- });