-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
object.js
2986 lines (2482 loc) · 105 KB
/
object.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';
// Load modules
const Lab = require('lab');
const Joi = require('../..');
const Helper = require('../helper');
// Declare internals
const internals = {};
// Test shortcuts
const { describe, it, expect } = exports.lab = Lab.script();
describe('object', () => {
it('can be called on its own', () => {
const object = Joi.object;
expect(() => object()).to.throw('Must be invoked on a Joi instance.');
});
it('converts a json string to an object', async () => {
const value = await Joi.object().validate('{"hi":true}');
expect(value.hi).to.equal(true);
});
it('errors on non-object string', async () => {
const err = await expect(Joi.object().validate('a string')).to.reject('"value" must be an object');
expect(err.details).to.equal([{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]);
});
it('validates an object', () => {
const schema = Joi.object().required();
Helper.validate(schema, [
[{}, true],
[{ hi: true }, true],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}]
]);
});
it('return object reference when no rules specified', async () => {
const schema = Joi.object({
a: Joi.object()
});
const item = { x: 5 };
const value = await schema.validate({ a: item });
expect(value.a).to.equal(item);
});
it('retains ignored values', async () => {
const schema = Joi.object();
const value = await schema.validate({ a: 5 });
expect(value.a).to.equal(5);
});
it('retains skipped values', async () => {
const schema = Joi.object({ b: 5 }).unknown(true);
const value = await schema.validate({ b: '5', a: 5 });
expect(value.a).to.equal(5);
expect(value.b).to.equal(5);
});
it('allows any key when schema is undefined', async () => {
await Joi.object().validate({ a: 4 });
await Joi.object(undefined).validate({ a: 4 });
});
it('allows any key when schema is null', async () => {
await Joi.object(null).validate({ a: 4 });
});
it('throws on invalid object schema', () => {
expect(() => {
Joi.object(4);
}).to.throw('Object schema must be a valid object');
});
it('throws on joi object schema', () => {
expect(() => {
Joi.object(Joi.object());
}).to.throw('Object schema cannot be a joi schema');
});
it('skips conversion when value is undefined', async () => {
const value = await Joi.object({ a: Joi.object() }).validate(undefined);
expect(value).to.not.exist();
});
it('errors on array', async () => {
const err = await expect(Joi.object().validate([1, 2, 3])).to.reject('"value" must be an object');
expect(err.details).to.equal([{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]);
});
it('should prevent extra keys from existing by default', () => {
const schema = Joi.object({ item: Joi.string().required() }).required();
Helper.validate(schema, [
[{ item: 'something' }, true],
[{ item: 'something', item2: 'something else' }, false, null, {
message: '"item2" is not allowed',
details: [{
message: '"item2" is not allowed',
path: ['item2'],
type: 'object.allowUnknown',
context: { child: 'item2', label: 'item2', key: 'item2' }
}]
}],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}]
]);
});
it('should validate count when min is set', () => {
const schema = Joi.object().min(3);
Helper.validate(schema, [
[{ item: 'something' }, false, null, {
message: '"value" must have at least 3 children',
details: [{
message: '"value" must have at least 3 children',
path: [],
type: 'object.min',
context: { limit: 3, label: 'value', key: undefined }
}]
}],
[{ item: 'something', item2: 'something else' }, false, null, {
message: '"value" must have at least 3 children',
details: [{
message: '"value" must have at least 3 children',
path: [],
type: 'object.min',
context: { limit: 3, label: 'value', key: undefined }
}]
}],
[{ item: 'something', item2: 'something else', item3: 'something something else' }, true],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}]
]);
});
it('should validate count when max is set', () => {
const schema = Joi.object().max(2);
Helper.validate(schema, [
[{ item: 'something' }, true],
[{ item: 'something', item2: 'something else' }, true],
[{ item: 'something', item2: 'something else', item3: 'something something else' }, false, null, {
message: '"value" must have less than or equal to 2 children',
details: [{
message: '"value" must have less than or equal to 2 children',
path: [],
type: 'object.max',
context: { limit: 2, label: 'value', key: undefined }
}]
}],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}]
]);
});
it('should validate count when min and max is set', () => {
const schema = Joi.object().max(3).min(2);
Helper.validate(schema, [
[{ item: 'something' }, false, null, {
message: '"value" must have at least 2 children',
details: [{
message: '"value" must have at least 2 children',
path: [],
type: 'object.min',
context: { limit: 2, label: 'value', key: undefined }
}]
}],
[{ item: 'something', item2: 'something else' }, true],
[{ item: 'something', item2: 'something else', item3: 'something something else' }, true],
[{ item: 'something', item2: 'something else', item3: 'something something else', item4: 'item4' }, false, null, {
message: '"value" must have less than or equal to 3 children',
details: [{
message: '"value" must have less than or equal to 3 children',
path: [],
type: 'object.max',
context: { limit: 3, label: 'value', key: undefined }
}]
}],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}]
]);
});
it('should validate count when length is set', () => {
const schema = Joi.object().length(2);
Helper.validate(schema, [
[{ item: 'something' }, false, null, {
message: '"value" must have 2 children',
details: [{
message: '"value" must have 2 children',
path: [],
type: 'object.length',
context: { limit: 2, label: 'value', key: undefined }
}]
}],
[{ item: 'something', item2: 'something else' }, true],
[{ item: 'something', item2: 'something else', item3: 'something something else' }, false, null, {
message: '"value" must have 2 children',
details: [{
message: '"value" must have 2 children',
path: [],
type: 'object.length',
context: { limit: 2, label: 'value', key: undefined }
}]
}],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}]
]);
});
it('should validate constructor when type is set', () => {
const schema = Joi.object().type(RegExp);
Helper.validate(schema, [
[{ item: 'something' }, false, null, {
message: '"value" must be an instance of "RegExp"',
details: [{
message: '"value" must be an instance of "RegExp"',
path: [],
type: 'object.type',
context: { type: 'RegExp', label: 'value', key: undefined }
}]
}],
['', false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}],
[new Date(), false, null, {
message: '"value" must be an instance of "RegExp"',
details: [{
message: '"value" must be an instance of "RegExp"',
path: [],
type: 'object.type',
context: { type: 'RegExp', label: 'value', key: undefined }
}]
}],
[/abcd/, true],
[new RegExp(), true]
]);
});
it('should traverse an object and validate all properties in the top level', () => {
const schema = Joi.object({
num: Joi.number()
});
Helper.validate(schema, [
[{ num: 1 }, true],
[{ num: [1, 2, 3] }, false, null, {
message: 'child "num" fails because ["num" must be a number]',
details: [{
message: '"num" must be a number',
path: ['num'],
type: 'number.base',
context: { label: 'num', key: 'num', value: [1, 2, 3] }
}]
}]
]);
});
it('should traverse an object and child objects and validate all properties', () => {
const schema = Joi.object({
num: Joi.number(),
obj: Joi.object({
item: Joi.string()
})
});
Helper.validate(schema, [
[{ num: 1 }, true],
[{ num: [1, 2, 3] }, false, null, {
message: 'child "num" fails because ["num" must be a number]',
details: [{
message: '"num" must be a number',
path: ['num'],
type: 'number.base',
context: { label: 'num', key: 'num', value: [1, 2, 3] }
}]
}],
[{ num: 1, obj: { item: 'something' } }, true],
[{ num: 1, obj: { item: 123 } }, false, null, {
message: 'child "obj" fails because [child "item" fails because ["item" must be a string]]',
details: [{
message: '"item" must be a string',
path: ['obj', 'item'],
type: 'string.base',
context: { value: 123, label: 'item', key: 'item' }
}]
}]
]);
});
it('should traverse an object several levels', () => {
const schema = Joi.object({
obj: Joi.object({
obj: Joi.object({
obj: Joi.object({
item: Joi.boolean()
})
})
})
});
Helper.validate(schema, [
[{ num: 1 }, false, null, {
message: '"num" is not allowed',
details: [{
message: '"num" is not allowed',
path: ['num'],
type: 'object.allowUnknown',
context: { child: 'num', label: 'num', key: 'num' }
}]
}],
[{ obj: {} }, true],
[{ obj: { obj: {} } }, true],
[{ obj: { obj: { obj: {} } } }, true],
[{ obj: { obj: { obj: { item: true } } } }, true],
[{ obj: { obj: { obj: { item: 10 } } } }, false, null, {
message: 'child "obj" fails because [child "obj" fails because [child "obj" fails because [child "item" fails because ["item" must be a boolean]]]]',
details: [{
message: '"item" must be a boolean',
path: ['obj', 'obj', 'obj', 'item'],
type: 'boolean.base',
context: { label: 'item', key: 'item' }
}]
}]
]);
});
it('should traverse an object several levels with required levels', () => {
const schema = Joi.object({
obj: Joi.object({
obj: Joi.object({
obj: Joi.object({
item: Joi.boolean()
})
}).required()
})
});
Helper.validate(schema, [
[null, false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}],
[undefined, true],
[{}, true],
[{ obj: {} }, false, null, {
message: 'child "obj" fails because [child "obj" fails because ["obj" is required]]',
details: [{
message: '"obj" is required',
path: ['obj', 'obj'],
type: 'any.required',
context: { label: 'obj', key: 'obj' }
}]
}],
[{ obj: { obj: {} } }, true],
[{ obj: { obj: { obj: {} } } }, true],
[{ obj: { obj: { obj: { item: true } } } }, true],
[{ obj: { obj: { obj: { item: 10 } } } }, false, null, {
message: 'child "obj" fails because [child "obj" fails because [child "obj" fails because [child "item" fails because ["item" must be a boolean]]]]',
details: [{
message: '"item" must be a boolean',
path: ['obj', 'obj', 'obj', 'item'],
type: 'boolean.base',
context: { label: 'item', key: 'item' }
}]
}]
]);
});
it('should traverse an object several levels with required levels (without Joi.obj())', () => {
const schema = {
obj: {
obj: {
obj: {
item: Joi.boolean().required()
}
}
}
};
Helper.validate(schema, [
[null, false, null, {
message: '"value" must be an object',
details: [{
message: '"value" must be an object',
path: [],
type: 'object.base',
context: { label: 'value', key: undefined }
}]
}],
[undefined, true],
[{}, true],
[{ obj: {} }, true],
[{ obj: { obj: {} } }, true],
[{ obj: { obj: { obj: {} } } }, false, null, {
message: 'child "obj" fails because [child "obj" fails because [child "obj" fails because [child "item" fails because ["item" is required]]]]',
details: [{
message: '"item" is required',
path: ['obj', 'obj', 'obj', 'item'],
type: 'any.required',
context: { label: 'item', key: 'item' }
}]
}],
[{ obj: { obj: { obj: { item: true } } } }, true],
[{ obj: { obj: { obj: { item: 10 } } } }, false, null, {
message: 'child "obj" fails because [child "obj" fails because [child "obj" fails because [child "item" fails because ["item" must be a boolean]]]]',
details: [{
message: '"item" must be a boolean',
path: ['obj', 'obj', 'obj', 'item'],
type: 'boolean.base',
context: { label: 'item', key: 'item' }
}]
}]
]);
});
it('errors on unknown keys when functions allows', async () => {
const schema = Joi.object({ a: Joi.number() }).options({ skipFunctions: true });
const obj = { a: 5, b: 'value' };
const err = await expect(schema.validate(obj)).to.reject('"b" is not allowed');
expect(err.details).to.equal([{
message: '"b" is not allowed',
path: ['b'],
type: 'object.allowUnknown',
context: { child: 'b', label: 'b', key: 'b' }
}]);
});
it('validates both valid() and with()', () => {
const schema = Joi.object({
first: Joi.valid('value'),
second: Joi.any()
}).with('first', 'second');
Helper.validate(schema, [
[{ first: 'value' }, false, null, {
message: '"first" missing required peer "second"',
details: [{
message: '"first" missing required peer "second"',
path: ['first'],
type: 'object.with',
context: {
main: 'first',
mainWithLabel: 'first',
peer: 'second',
peerWithLabel: 'second',
label: 'first',
key: 'first'
}
}]
}]
]);
});
it('validates referenced arrays in valid()', () => {
const ref = Joi.ref('$x');
const schema = Joi.object({
foo: Joi.valid(ref)
});
Helper.validate(schema, [
[{ foo: 'bar' }, true, { context: { x: 'bar' } }],
[{ foo: 'bar' }, true, { context: { x: ['baz', 'bar'] } }],
[{ foo: 'bar' }, false, { context: { x: 'baz' } }, {
message: 'child "foo" fails because ["foo" must be one of [context:x]]',
details: [{
message: '"foo" must be one of [context:x]',
path: ['foo'],
type: 'any.allowOnly',
context: { value: 'bar', valids: [ref], label: 'foo', key: 'foo' }
}]
}],
[{ foo: 'bar' }, false, { context: { x: ['baz', 'qux'] } }, {
message: 'child "foo" fails because ["foo" must be one of [context:x]]',
details: [{
message: '"foo" must be one of [context:x]',
path: ['foo'],
type: 'any.allowOnly',
context: { value: 'bar', valids: [ref], label: 'foo', key: 'foo' }
}]
}],
[{ foo: 'bar' }, false, null, {
message: 'child "foo" fails because ["foo" must be one of [context:x]]',
details: [{
message: '"foo" must be one of [context:x]',
path: ['foo'],
type: 'any.allowOnly',
context: { value: 'bar', valids: [ref], label: 'foo', key: 'foo' }
}]
}]
]);
});
it('errors on unknown nested keys with the correct path', async () => {
const schema = Joi.object({ a: Joi.object().keys({}) });
const obj = { a: { b: 'value' } };
const err = await expect(schema.validate(obj)).to.reject('child "a" fails because ["b" is not allowed]');
expect(err.details).to.equal([{
message: '"b" is not allowed',
path: ['a', 'b'],
type: 'object.allowUnknown',
context: { child: 'b', label: 'b', key: 'b' }
}]);
});
it('errors on unknown nested keys with the correct path at the root level', async () => {
const schema = Joi.object({ a: Joi.object().keys({}) });
const obj = { c: 'hello' };
const err = await expect(schema.validate(obj)).to.reject('"c" is not allowed');
expect(err.details).to.equal([{
message: '"c" is not allowed',
path: ['c'],
type: 'object.allowUnknown',
context: { child: 'c', label: 'c', key: 'c' }
}]);
});
it('should work on prototype-less objects', async () => {
const input = Object.create(null);
const schema = Joi.object().keys({
a: Joi.number()
});
input.a = 1337;
await Joi.validate(input, schema);
});
it('should be able to use rename safely with a fake hasOwnProperty', async () => {
const input = { a: 1, hasOwnProperty: 'foo' };
const schema = Joi.object().rename('b', 'a');
const err = await expect(Joi.validate(input, schema)).to.reject();
expect(err).to.be.an.error('"value" cannot rename child "b" because override is disabled and target "a" exists');
expect(err.details).to.equal([{
message: '"value" cannot rename child "b" because override is disabled and target "a" exists',
path: [],
type: 'object.rename.override',
context: { from: 'b', to: 'a', label: 'value', key: undefined }
}]);
});
it('should be able to use object.with() safely with a fake hasOwnProperty', async () => {
const input = { a: 1, hasOwnProperty: 'foo' };
const schema = Joi.object({ a: 1 }).with('a', 'b');
const err = await expect(Joi.validate(input, schema)).to.reject();
expect(err).to.be.an.error('"hasOwnProperty" is not allowed. "a" missing required peer "b"');
expect(err.details).to.equal([
{
message: '"hasOwnProperty" is not allowed',
path: ['hasOwnProperty'],
type: 'object.allowUnknown',
context:
{
child: 'hasOwnProperty',
label: 'hasOwnProperty',
key: 'hasOwnProperty'
}
},
{
message: '"a" missing required peer "b"',
path: ['a'],
type: 'object.with',
context:
{
main: 'a',
mainWithLabel: 'a',
peer: 'b',
peerWithLabel: 'b',
label: 'a',
key: 'a'
}
}
]);
});
it('should apply labels with nested objects', () => {
const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).with('a', ['b.c']);
const error = schema.validate({ a: 1 , b: { d: 2 } }).error;
expect(error).to.be.an.error('"first" missing required peer "second"');
expect(error.details).to.equal([{
message: '"first" missing required peer "second"',
path: ['a'],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'second',
label: 'a',
key: 'a'
}
}]);
});
describe('keys()', () => {
it('allows any key', async () => {
const a = Joi.object({ a: 4 });
const b = a.keys();
const err = await expect(a.validate({ b: 3 })).to.reject('"b" is not allowed');
expect(err.details).to.equal([{
message: '"b" is not allowed',
path: ['b'],
type: 'object.allowUnknown',
context: { child: 'b', label: 'b', key: 'b' }
}]);
await b.validate({ b: 3 });
});
it('forbids all keys', async () => {
const a = Joi.object();
const b = a.keys({});
await a.validate({ b: 3 });
const err = await expect(b.validate({ b: 3 })).to.reject('"b" is not allowed');
expect(err.details).to.equal([{
message: '"b" is not allowed',
path: ['b'],
type: 'object.allowUnknown',
context: { child: 'b', label: 'b', key: 'b' }
}]);
});
it('adds to existing keys', async () => {
const a = Joi.object({ a: 1 });
const b = a.keys({ b: 2 });
const err = await expect(a.validate({ a: 1, b: 2 })).to.reject('"b" is not allowed');
expect(err.details).to.equal([{
message: '"b" is not allowed',
path: ['b'],
type: 'object.allowUnknown',
context: { child: 'b', label: 'b', key: 'b' }
}]);
await b.validate({ a: 1, b: 2 });
});
it('overrides existing keys', () => {
const a = Joi.object({ a: 1 });
const b = a.keys({ a: Joi.string() });
Helper.validate(a, [
[{ a: 1 }, true, null, { a: 1 }],
[{ a: '1' }, true, null, { a: 1 }],
[{ a: '2' }, false, null, {
message: 'child "a" fails because ["a" must be one of [1]]',
details: [{
message: '"a" must be one of [1]',
path: ['a'],
type: 'any.allowOnly',
context: { value: 2, valids: [1], label: 'a', key: 'a' }
}]
}]
]);
Helper.validate(b, [
[{ a: 1 }, false, null, {
message: 'child "a" fails because ["a" must be a string]',
details: [{
message: '"a" must be a string',
path: ['a'],
type: 'string.base',
context: { value: 1, label: 'a', key: 'a' }
}]
}],
[{ a: '1' }, true, null, { a: '1' }]
]);
});
it('strips keys flagged with strip', async () => {
const schema = Joi.object({
a: Joi.string().strip(),
b: Joi.string()
});
const value = await schema.validate({ a: 'test', b: 'test' });
expect(value.a).to.not.exist();
expect(value.b).to.equal('test');
});
it('strips keys after validation', async () => {
const schema = Joi.object({
a: Joi.string().strip(),
b: Joi.string().default(Joi.ref('a'))
});
const value = await schema.validate({ a: 'test' });
expect(value.a).to.not.exist();
expect(value.b).to.equal('test');
});
it('strips keys while preserving transformed values', () => {
const schema = Joi.object({
a: Joi.number().strip(),
b: Joi.number().min(Joi.ref('a'))
});
const result = schema.validate({ a: '1', b: '2' });
expect(result.error).to.not.exist();
expect(result.value.a).to.not.exist();
expect(result.value.b).to.equal(2);
const result2 = schema.validate({ a: '1', b: '0' });
expect(result2.error).to.be.an.error('child "b" fails because ["b" must be larger than or equal to 1]');
expect(result2.error.details).to.equal([{
message: '"b" must be larger than or equal to 1',
path: ['b'],
type: 'number.min',
context: { limit: 1, value: 0, label: 'b', key: 'b' }
}]);
});
it('does not alter the original object when stripping keys', async () => {
const schema = Joi.object({
a: Joi.string().strip(),
b: Joi.string()
});
const valid = {
a: 'test',
b: 'test'
};
const value = await schema.validate(valid);
expect(value.a).to.not.exist();
expect(valid.a).to.equal('test');
expect(value.b).to.equal('test');
expect(valid.b).to.equal('test');
});
it('should strip from an alternative', async () => {
const schema = Joi.object({
a: [Joi.boolean().strip()]
});
const valid = {
a: true
};
const value = await schema.validate(valid);
expect(value).to.equal({});
});
});
describe('append()', () => {
it('should append schema', async () => {
const schema = Joi.object()
.keys({ a: Joi.string() })
.append({ b: Joi.string() });
await schema.validate({ a: 'x', b: 'y' });
});
it('should not change schema if it is null', async () => {
const schema = Joi.object()
.keys({ a: Joi.string() })
.append(null);
await schema.validate({ a: 'x' });
});
it('should not change schema if it is undefined', async () => {
const schema = Joi.object()
.keys({ a: Joi.string() })
.append(undefined);
await schema.validate({ a: 'x' });
});
it('should not change schema if it is empty-object', async () => {
const schema = Joi.object()
.keys({ a: Joi.string() })
.append({});
await schema.validate({ a: 'x' });
});
});
describe('unknown()', () => {
it('avoids unnecessary cloning when called twice', () => {
const schema = Joi.object().unknown();
expect(schema.unknown()).to.shallow.equal(schema);
});
it('allows local unknown without applying to children', () => {
const schema = Joi.object({
a: {
b: Joi.number()
}
}).unknown();
Helper.validate(schema, [
[{ a: { b: 5 } }, true],
[{ a: { b: 'x' } }, false, null, {
message: 'child "a" fails because [child "b" fails because ["b" must be a number]]',
details: [{
message: '"b" must be a number',
path: ['a', 'b'],
type: 'number.base',
context: { label: 'b', key: 'b', value: 'x' }
}]
}],
[{ a: { b: 5 }, c: 'ignore' }, true],
[{ a: { b: 5, c: 'ignore' } }, false, null, {
message: 'child "a" fails because ["c" is not allowed]',
details: [{
message: '"c" is not allowed',
path: ['a', 'c'],
type: 'object.allowUnknown',
context: { child: 'c', label: 'c', key: 'c' }
}]
}]
]);
});
it('forbids local unknown without applying to children', () => {
const schema = Joi.object({
a: Joi.object({
b: Joi.number()
}).unknown()
}).options({ allowUnknown: false });
Helper.validate(schema, [
[{ a: { b: 5 } }, true],
[{ a: { b: 'x' } }, false, null, {
message: 'child "a" fails because [child "b" fails because ["b" must be a number]]',
details: [{
message: '"b" must be a number',
path: ['a', 'b'],
type: 'number.base',
context: { label: 'b', key: 'b', value: 'x' }
}]
}],
[{ a: { b: 5 }, c: 'ignore' }, false, null, {
message: '"c" is not allowed',
details: [{
message: '"c" is not allowed',
path: ['c'],
type: 'object.allowUnknown',
context: { child: 'c', label: 'c', key: 'c' }
}]
}],
[{ a: { b: 5, c: 'ignore' } }, true]
]);
});
it('overrides stripUnknown at a local level', () => {
const schema = Joi.object({
a: Joi.object({
b: Joi.number(),
c: Joi.object({
d: Joi.number()
})
}).unknown()
}).options({ allowUnknown: false, stripUnknown: true });
Helper.validate(schema, [
[{ a: { b: 5 } }, true, null, { a: { b: 5 } }],
[{ a: { b: 'x' } }, false, null, {
message: 'child "a" fails because [child "b" fails because ["b" must be a number]]',
details: [{
message: '"b" must be a number',
path: ['a', 'b'],
type: 'number.base',
context: { label: 'b', key: 'b', value: 'x' }
}]
}],
[{ a: { b: 5 }, d: 'ignore' }, true, null, { a: { b: 5 } }],
[{ a: { b: 5, d: 'ignore' } }, true, null, { a: { b: 5, d: 'ignore' } }],
[{ a: { b: 5, c: { e: 'ignore' } } }, true, null, { a: { b: 5, c: {} } }]
]);