-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.js
executable file
·1361 lines (1092 loc) · 42.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const Code = require('@hapi/code');
const Lab = require('@hapi/lab');
const Joi = require('..');
const Helper = require('./helper');
const internals = {};
const { describe, it } = exports.lab = Lab.script();
const { expect } = Code;
describe('Joi', () => {
it('keeps schema immutable', () => {
const a = Joi.string();
const b = a.valid('b');
Helper.validate(a, [
['a', true],
['b', true],
[5, false, {
message: '"value" must be a string',
path: [],
type: 'string.base',
context: { value: 5, label: 'value' }
}]
]);
Helper.validate(b, [
['a', false, {
message: '"value" must be [b]',
path: [],
type: 'any.only',
context: { value: 'a', valids: ['b'], label: 'value' }
}],
['b', true],
[5, false, {
message: '"value" must be [b]',
path: [],
type: 'any.only',
context: { value: 5, valids: ['b'], label: 'value' }
}]
]);
});
it('validates a compiled complex literal', () => {
const schema = Joi.compile(['key', 5, { a: true, b: [/^a/, 'boom'] }]);
Helper.validate(schema, [
['key', true],
[5, true],
['other', false, {
message: '"value" must be one of [key, 5, object]',
path: [],
type: 'alternatives.types',
context: {
label: 'value',
types: ['key', 5, 'object'],
value: 'other'
}
}],
[6, false, {
message: '"value" must be one of [key, 5, object]',
path: [],
type: 'alternatives.types',
context: {
label: 'value',
types: ['key', 5, 'object'],
value: 6
}
}],
[{ c: 5 }, false, {
message: '"c" is not allowed',
path: ['c'],
type: 'object.unknown',
context: { child: 'c', label: 'c', key: 'c', value: 5 }
}],
[{}, true],
[{ b: 'abc' }, true],
[{ a: true, b: 'boom' }, true],
[{ a: 5, b: 'a' }, false, {
message: '"a" must be [true]',
path: ['a'],
type: 'any.only',
context: { label: 'a', key: 'a', value: 5, valids: [true] }
}]
]);
});
it('validates regex directly', () => {
Helper.validate(Joi.compile(/^5$/), [['5', true]]);
Helper.validate(Joi.compile(/.{2}/), [['6', false, {
message: '"value" with value "6" fails to match the required pattern: /.{2}/',
path: [],
type: 'string.pattern.base',
context: {
name: undefined,
regex: /.{2}/,
value: '6',
label: 'value'
}
}]]);
});
it('validates an array of valid types', () => {
const schema = Joi.object({
auth: [
Joi.object({
mode: Joi.string().valid('required', 'optional', 'try').allow(null)
}).allow(null),
Joi.string(),
Joi.boolean()
]
});
Helper.validate(schema, [
[{ auth: { mode: 'none' } }, false, '"auth.mode" must be one of [required, optional, try, null]'],
[{ auth: { mode: 'try' } }, true],
[{ something: undefined }, false, {
message: '"something" is not allowed',
path: ['something'],
type: 'object.unknown',
context: { child: 'something', label: 'something', key: 'something' }
}],
[{ auth: { something: undefined } }, false, {
message: '"auth.something" is not allowed',
path: ['auth', 'something'],
type: 'object.unknown',
context: { child: 'something', label: 'auth.something', key: 'something' }
}],
[{ auth: null }, true],
[{ auth: undefined }, true],
[{}, true],
[{ auth: true }, true],
[{ auth: 123 }, false, {
message: '"auth" must be one of [object, string, boolean]',
path: ['auth'],
type: 'alternatives.types',
context: { types: ['object', 'string', 'boolean'], label: 'auth', key: 'auth', value: 123 }
}]
]);
});
it('validates alternatives', () => {
const schema = Joi.object({
auth: Joi.alternatives([
Joi.object({
mode: Joi.string().valid('required', 'optional', 'try').allow(null)
}).allow(null),
Joi.string(),
Joi.boolean()
])
});
const err = schema.validate({ auth: { mode: 'none' } }).error;
expect(err).to.be.an.error('"auth.mode" must be one of [required, optional, try, null]');
Helper.validate(schema, [
[{ auth: { mode: 'try' } }, true],
[{ something: undefined }, false, {
message: '"something" is not allowed',
path: ['something'],
type: 'object.unknown',
context: { child: 'something', label: 'something', key: 'something' }
}],
[{ auth: { something: undefined } }, false, {
message: '"auth.something" is not allowed',
path: ['auth', 'something'],
type: 'object.unknown',
context: { child: 'something', label: 'auth.something', key: 'something' }
}],
[{ auth: null }, true],
[{ auth: undefined }, true],
[{}, true],
[{ auth: true }, true],
[{ auth: 123 }, false, {
message: '"auth" must be one of [object, string, boolean]',
path: ['auth'],
type: 'alternatives.types',
context: { types: ['object', 'string', 'boolean'], label: 'auth', key: 'auth', value: 123 }
}]
]);
});
it('validates required alternatives', () => {
const schema = Joi.object({
a: Joi.alternatives([
Joi.string().required(),
Joi.boolean().required()
])
});
Helper.validate(schema, [
[{ a: null }, false, {
message: '"a" must be one of [string, boolean]',
path: ['a'],
type: 'alternatives.types',
context: { types: ['string', 'boolean'], label: 'a', key: 'a', value: null }
}],
[{ a: undefined }, true],
[{}, true],
[{ a: true }, true],
[{ a: 'true' }, true],
[{ a: 123 }, false, {
message: '"a" must be one of [string, boolean]',
path: ['a'],
type: 'alternatives.types',
context: { types: ['string', 'boolean'], label: 'a', key: 'a', value: 123 }
}],
[{ a: { c: 1 } }, false, {
message: '"a" must be one of [string, boolean]',
path: ['a'],
type: 'alternatives.types',
context: { types: ['string', 'boolean'], label: 'a', key: 'a', value: { c: 1 } }
}],
[{ b: undefined }, false, {
message: '"b" is not allowed',
path: ['b'],
type: 'object.unknown',
context: { child: 'b', label: 'b', key: 'b' }
}]
]);
});
it('validates required [] alternatives', () => {
const schema = Joi.object({
a: [
Joi.string().required(),
Joi.boolean().required()
]
});
Helper.validate(schema, [
[{ a: null }, false, {
message: '"a" must be one of [string, boolean]',
path: ['a'],
type: 'alternatives.types',
context: { types: ['string', 'boolean'], label: 'a', key: 'a', value: null }
}],
[{ a: undefined }, true],
[{}, true],
[{ a: true }, true],
[{ a: 'true' }, true],
[{ a: 123 }, false, {
message: '"a" must be one of [string, boolean]',
path: ['a'],
type: 'alternatives.types',
context: { types: ['string', 'boolean'], label: 'a', key: 'a', value: 123 }
}],
[{ a: { c: 1 } }, false, {
message: '"a" must be one of [string, boolean]',
path: ['a'],
type: 'alternatives.types',
context: { types: ['string', 'boolean'], label: 'a', key: 'a', value: { c: 1 } }
}],
[{ b: undefined }, false, {
message: '"b" is not allowed',
path: ['b'],
type: 'object.unknown',
context: { child: 'b', label: 'b', key: 'b' }
}]
]);
});
it('validates an array of string with valid', () => {
const schema = Joi.object({
brand: Joi.array().items(Joi.string().valid('amex', 'visa'))
});
Helper.validate(schema, [
[{ brand: ['amex'] }, true],
[{ brand: ['visa', 'mc'] }, false, {
message: '"brand[1]" must be one of [amex, visa]',
path: ['brand', 1],
type: 'any.only',
context: { value: 'mc', valids: ['amex', 'visa'], label: 'brand[1]', key: 1 }
}]
]);
});
it('validates pre and post convert value', () => {
const schema = Joi.number().valid(5);
Helper.validate(schema, [
[5, true],
['5', true, 5]
]);
});
it('does not change object when validation fails', () => {
const schema = Joi.object({
a: Joi.number().valid(2)
});
const obj = {
a: '5'
};
const { error, value } = schema.validate(obj);
expect(error).to.exist();
expect(value.a).to.equal('5');
});
it('does not set optional keys when missing', () => {
const schema = Joi.object({
a: Joi.number()
});
const obj = {};
const value = schema.validate(obj).value;
expect(value.hasOwnProperty('a')).to.equal(false);
});
it('invalidates pre and post convert value', () => {
const schema = Joi.number().invalid(5);
Helper.validate(schema, [
[5, false, {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 5, invalids: [5], label: 'value' }
}],
['5', false, {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 5, invalids: [5], label: 'value' }
}]
]);
});
it('invalidates missing peers', () => {
const schema = Joi.object({
username: Joi.string(),
password: Joi.string()
}).with('username', 'password').without('password', 'access_token');
Helper.validate(schema, [[{ username: 'bob' }, false, '"username" missing required peer "password"']]);
});
it('validates config where the root item is a joi type', () => {
Helper.validate(Joi.boolean().allow(null), [[true, true]]);
Helper.validate(Joi.object(), [[{ auth: { mode: 'try' } }, true]]);
Helper.validate(Joi.object(), [[true, false, '"value" must be of type object']]);
Helper.validate(Joi.string(), [[true, false, '"value" must be a string']]);
Helper.validate(Joi.object({ param: Joi.string().required() }), [[{ param: 'item' }, true]]);
});
it('converts string to number', () => {
const schema = Joi.object({
a: Joi.number()
});
const input = { a: '5' };
Helper.validate(schema, [[input, true, { a: 5 }]]);
expect(input.a).to.equal('5');
});
it('allows unknown keys in objects if no schema was given', () => {
Helper.validate(Joi.object(), [[{ foo: 'bar' }, true]]);
});
it('fails on unknown keys in objects if a schema was given', () => {
Helper.validate(Joi.object({}), [[{ foo: 'bar' }, false, {
message: '"foo" is not allowed',
path: ['foo'],
type: 'object.unknown',
context: { child: 'foo', label: 'foo', key: 'foo', value: 'bar' }
}]]);
Helper.validate(Joi.compile({}), [[{ foo: 'bar' }, false, '"foo" is not allowed']]);
Helper.validate(Joi.compile({ other: Joi.number() }), [[{ foo: 'bar' }, false, '"foo" is not allowed']]);
});
it('local unknown setting always overrides global allowUnknown setting', () => {
const input = {
a: {
b: "h",
c: "f"
},
d: "h"
};
Helper.validate(
Joi.object({
a: Joi.object().keys({
b: Joi.string()
}).unknown(true)
}),
{ allowUnknown: false },
[[input, false, '"d" is not allowed']]
);
Helper.validate(
Joi.object({
a: Joi.object().keys({
b: Joi.string()
}).unknown(false)
}),
{ allowUnknown: true },
[[input, false, '"a.c" is not allowed']]
);
Helper.validate(
Joi.object({
a: Joi.object().keys({
b: Joi.string()
})
}),
{ allowUnknown: true },
[[input, false, '"a.c" is not allowed']]
);
});
it('validates required key with multiple options', () => {
const config = {
module: Joi.alternatives([
Joi.object({
compile: Joi.function().required(),
execute: Joi.function()
}),
Joi.string()
]).required()
};
Helper.validate(Joi.compile(config), [
[{}, false, '"module" is required'],
[{ module: 'test' }, true],
[{ module: {} }, false, '"module.compile" is required'],
[{ module: { compile() { } } }, true]
]);
});
it('validates key with required alternatives', () => {
const config = {
module: Joi.alt().try(
Joi.object({
compile: Joi.function().required(),
execute: Joi.function()
}).required(),
Joi.string().required()
)
};
expect(Joi.compile(config).validate({}).error).to.not.exist();
});
it('validates required key with alternatives', () => {
const config = {
module: Joi.alt().try(
Joi.object({
compile: Joi.function().required(),
execute: Joi.function()
}),
Joi.string()
).required()
};
Helper.validate(Joi.compile(config), [[{}, false, '"module" is required']]);
});
it('validates object successfully when config has an array of types', () => {
const schema = {
f: [Joi.number(), Joi.boolean()],
g: [Joi.string(), Joi.object()]
};
const obj = {
f: true,
g: 'test'
};
Helper.validate(Joi.compile(schema), [[obj, true]]);
});
it('validates object successfully when config allows for optional key and key is missing', () => {
const schema = {
h: Joi.number(),
i: Joi.string(),
j: Joi.object()
};
const obj = {
h: 12,
i: 'test'
};
Helper.validate(Joi.compile(schema), [[obj, true]]);
});
it('fails validation', () => {
const schema = {
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
};
const obj = {
a: 10,
b: 'a',
c: '[email protected]'
};
Helper.validate(Joi.compile(schema), [[obj, false, '"a" must be less than or equal to 3']]);
});
it('fails validation when the wrong types are supplied', () => {
const schema = {
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
};
const obj = {
a: 'a',
b: 'a',
c: '[email protected]'
};
Helper.validate(Joi.compile(schema), [[obj, false, '"a" must be a number']]);
});
it('fails validation when missing a required parameter', () => {
const obj = {
c: 10
};
Helper.validate(Joi.compile({ a: Joi.string().required() }), [[obj, false, '"a" is required']]);
});
it('fails validation when missing a required parameter within an object config', () => {
const obj = {
a: {}
};
Helper.validate(Joi.compile({ a: Joi.object({ b: Joi.string().required() }) }), [[obj, false, '"a.b" is required']]);
});
it('fails validation when parameter is required to be an Array but is given as string', () => {
const obj = {
a: 'an array'
};
Helper.validate(Joi.object({ a: Joi.array() }), [[obj, false, '"a" must be an array']]);
});
it('fails validation when parameter is required to be an Array but is given as a json that is incorrect (object instead of array)', () => {
const obj = {
a: '{"b":2}'
};
Helper.validate(Joi.object({ a: Joi.object({ b: Joi.string().required() }) }), [[obj, false, '"a" must be of type object']]);
});
it('fails validation when config is an array and fails', () => {
const schema = {
d: [Joi.string(), Joi.boolean()],
e: [Joi.number(), Joi.object()]
};
const obj = {
d: 10,
e: 'a'
};
Helper.validate(Joi.compile(schema), [[obj, false, '"d" must be one of [string, boolean]']]);
});
it('fails validation when config is an array and fails with extra keys', () => {
const schema = {
d: [Joi.string(), Joi.boolean()],
e: [Joi.number(), Joi.object()]
};
const obj = {
a: 10,
b: 'a'
};
Helper.validate(Joi.compile(schema), [[obj, false, '"a" is not allowed']]);
});
it('fails validation with extra keys', () => {
const schema = {
a: Joi.number()
};
const obj = {
a: 1,
b: 'a'
};
Helper.validate(Joi.compile(schema), [[obj, false, '"b" is not allowed']]);
});
it('validates missing optional key with string condition', () => {
const schema = {
key: Joi.string().alphanum(false).min(8)
};
Helper.validate(Joi.compile(schema), [[{}, true]]);
});
it('validates with extra keys and remove them when stripUnknown is set', () => {
const schema = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
});
const obj = {
a: 1,
b: 'a',
d: 'c'
};
Helper.validate(schema, { stripUnknown: true, allowUnknown: true }, [[obj, true, { a: 1, b: 'a' }]]);
});
it('validates with extra keys and remove them when stripUnknown (as an object) is set', () => {
const schema = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
});
const obj = {
a: 1,
b: 'a',
d: 'c'
};
Helper.validate(schema, { stripUnknown: { arrays: false, objects: true }, allowUnknown: true }, [[obj, true, { a: 1, b: 'a' }]]);
});
it('validates dependencies when stripUnknown is set', () => {
const schema = Joi.object({
a: Joi.number(),
b: Joi.string()
}).and('a', 'b');
const obj = {
a: 1,
foo: 'bar'
};
Helper.validate(schema, { stripUnknown: true }, [[obj, false, {
message: '"value" contains [a] without its required peers [b]',
path: [],
type: 'object.and',
context: {
present: ['a'],
presentWithLabels: ['a'],
missing: ['b'],
missingWithLabels: ['b'],
label: 'value',
value: { a: 1 }
}
}]]);
});
it('validates dependencies when stripUnknown (as an object) is set', () => {
const schema = Joi.object({
a: Joi.number(),
b: Joi.string()
})
.and('a', 'b');
const obj = {
a: 1,
foo: 'bar'
};
Helper.validate(schema, { stripUnknown: { arrays: false, objects: true } }, [[obj, false, {
message: '"value" contains [a] without its required peers [b]',
path: [],
type: 'object.and',
context: {
present: ['a'],
presentWithLabels: ['a'],
missing: ['b'],
missingWithLabels: ['b'],
label: 'value',
value: { a: 1 }
}
}]]);
});
it('fails to validate with incorrect property when asked to strip unknown keys without aborting early', () => {
const schema = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
});
const obj = {
a: 1,
b: 'f',
d: 'c'
};
Helper.validate(schema, { stripUnknown: true, abortEarly: false }, [[obj, false, '"b" must be one of [a, b, c]']]);
});
it('fails to validate with incorrect property when asked to strip unknown keys (as an object) without aborting early', () => {
const schema = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
});
const obj = {
a: 1,
b: 'f',
d: 'c'
};
Helper.validate(schema, { stripUnknown: { arrays: false, objects: true }, abortEarly: false }, [[obj, false, '"b" must be one of [a, b, c]']]);
});
it('should pass validation with extra keys when allowUnknown is set', () => {
const schema = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c'),
c: Joi.string().email().optional()
});
const obj = {
a: 1,
b: 'a',
d: 'c'
};
Helper.validate(schema, { allowUnknown: true }, [[obj, true, { a: 1, b: 'a', d: 'c' }]]);
});
it('should pass validation with extra keys set', () => {
const localConfig = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c')
}).prefs({ allowUnknown: true });
const obj = {
a: 1,
b: 'a',
d: 'c'
};
Helper.validate(localConfig, [[obj, true, { a: 1, b: 'a', d: 'c' }]]);
});
it('should pass validation with extra keys and remove them when stripUnknown is set locally', () => {
const localConfig = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c')
}).prefs({ stripUnknown: true, allowUnknown: true });
const obj = {
a: 1,
b: 'a',
d: 'c'
};
Helper.validate(localConfig, [
[obj, true, { a: 1, b: 'a' }],
[{ a: 1, b: 'a' }, true, { a: 1, b: 'a' }]
]);
});
it('should pass validation with extra keys and remove them when stripUnknown (as an object) is set locally', () => {
const localConfig = Joi.object({
a: Joi.number().min(0).max(3),
b: Joi.string().valid('a', 'b', 'c')
}).prefs({ stripUnknown: { arrays: false, objects: true }, allowUnknown: true });
const obj = {
a: 1,
b: 'a',
d: 'c'
};
Helper.validate(localConfig, [
[obj, true, { a: 1, b: 'a' }],
[{ a: 1, b: 'a' }, true, { a: 1, b: 'a' }]
]);
});
it('should work when the skipFunctions setting is enabled', () => {
const schema = Joi.object({ username: Joi.string() }).prefs({ skipFunctions: true });
const input = { username: 'test', func() { } };
Helper.validate(schema, [[input, true]]);
});
it('should work when the skipFunctions setting is disabled', () => {
const schema = Joi.object({ username: Joi.string() });
const input = { username: 'test', func() { } };
Helper.validate(schema, { skipFunctions: false }, [[input, false, '"func" is not allowed']]);
});
it('should not convert values when convert is false', () => {
const schema = Joi.object({
arr: Joi.array().items(Joi.string())
});
const input = { arr: 'foo' };
Helper.validate(schema, { convert: false }, [[input, false, '"arr" must be an array']]);
});
it('full errors when abortEarly is false', () => {
const schema = Joi.object({
a: Joi.string(),
b: Joi.string()
});
const input = { a: 1, b: 2 };
const errOne = schema.validate(input).error;
const errFull = schema.validate(input, { abortEarly: false }).error;
expect(errFull.details.length).to.be.greaterThan(errOne.details.length);
});
it('errors multiple times when abortEarly is false in a complex object', () => {
const schema = Joi.object({
test: Joi.array().items(Joi.object().keys({
foo: Joi.string().required().max(3),
bar: Joi.string().max(5)
})),
test2: Joi.object({
test3: Joi.array().items(Joi.object().keys({
foo: Joi.string().required().max(3),
bar: Joi.string().max(5),
baz: Joi.object({
test4: Joi.array().items(Joi.object().keys({
foo: Joi.string().required().max(3),
bar: Joi.string().max(5)
}))
})
}))
})
});
const input = {
test: [{
foo: 'test1',
bar: 'testfailed'
}],
test2: {
test3: [{
foo: '123'
}, {
foo: 'test1',
bar: 'testfailed'
}, {
foo: '123',
baz: {
test4: [{
foo: 'test1',
baz: '123'
}]
}
}]
}
};
Helper.validate(schema, { abortEarly: false }, [[input, false, {
message: '"test[0].foo" length must be less than or equal to 3 characters long. "test[0].bar" length must be less than or equal to 5 characters long. "test2.test3[1].foo" length must be less than or equal to 3 characters long. "test2.test3[1].bar" length must be less than or equal to 5 characters long. "test2.test3[2].baz.test4[0].foo" length must be less than or equal to 3 characters long. "test2.test3[2].baz.test4[0].baz" is not allowed',
details: [{
message: '"test[0].foo" length must be less than or equal to 3 characters long',
path: ['test', 0, 'foo'],
type: 'string.max',
context: { limit: 3, value: 'test1', key: 'foo', label: 'test[0].foo', encoding: undefined }
}, {
message: '"test[0].bar" length must be less than or equal to 5 characters long',
path: ['test', 0, 'bar'],
type: 'string.max',
context: { limit: 5, value: 'testfailed', key: 'bar', label: 'test[0].bar', encoding: undefined }
}, {
message: '"test2.test3[1].foo" length must be less than or equal to 3 characters long',
path: ['test2', 'test3', 1, 'foo'],
type: 'string.max',
context: { limit: 3, value: 'test1', key: 'foo', label: 'test2.test3[1].foo', encoding: undefined }
}, {
message: '"test2.test3[1].bar" length must be less than or equal to 5 characters long',
path: ['test2', 'test3', 1, 'bar'],
type: 'string.max',
context: { limit: 5, value: 'testfailed', key: 'bar', label: 'test2.test3[1].bar', encoding: undefined }
}, {
message: '"test2.test3[2].baz.test4[0].foo" length must be less than or equal to 3 characters long',
path: ['test2', 'test3', 2, 'baz', 'test4', 0, 'foo'],
type: 'string.max',
context: {
limit: 3,
value: 'test1',
key: 'foo',
label: 'test2.test3[2].baz.test4[0].foo',
encoding: undefined
}
}, {
message: '"test2.test3[2].baz.test4[0].baz" is not allowed',
path: ['test2', 'test3', 2, 'baz', 'test4', 0, 'baz'],
type: 'object.unknown',
context: { key: 'baz', label: 'test2.test3[2].baz.test4[0].baz', child: 'baz', value: '123' }
}]
}]]);
});
it('accepts no options', () => {
Helper.validate(Joi.string(), [['test', true]]);
Helper.validate(Joi.number(), { convert: false }, [['5', false, '"value" must be a number']]);
});
it('accepts null options', () => {
Helper.validate(Joi.string(), null, [['test', true]]);
});
it('accepts undefined options', () => {
Helper.validate(Joi.string(), undefined, [['test', true]]);
});
describe('assert()', () => {
it('respects abortEarly option', () => {
try {
Joi.assert({}, Joi.object().keys({ a: Joi.required(), b: Joi.required() }), { abortEarly: false });
throw new Error('should not reach that');
}
catch (err) {
expect(err.details.length).to.equal(2);
}
});
});
describe('attempt()', () => {
it('throws on invalid value', () => {
expect(() => {