/
any.js
executable file
·1771 lines (1301 loc) · 54 KB
/
any.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 Code = require('code');
const Joi = require('../lib');
const Helper = require('./helper');
// Declare internals
const internals = {};
// Test shortcuts
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;
describe('any', () => {
describe('equal()', () => {
it('validates valid values', (done) => {
Helper.validate(Joi.equal(4), [
[4, true],
[5, false, null, '"value" must be one of [4]']
], done);
});
});
describe('not()', () => {
it('validates invalid values', (done) => {
Helper.validate(Joi.not(5), [
[4, true],
[5, false, null, '"value" contains an invalid value']
], done);
});
});
describe('exist()', () => {
it('validates required values', (done) => {
Helper.validate(Joi.exist(), [
[4, true],
[undefined, false, null, '"value" is required']
], done);
});
});
describe('strict()', () => {
it('validates without converting', (done) => {
const schema = Joi.object({
array: Joi.array().items(Joi.string().min(5), Joi.number().min(3))
}).strict();
Helper.validate(schema, [
[{ array: ['12345'] }, true],
[{ array: ['1'] }, false, null, 'child "array" fails because ["array" at position 0 does not match any of the allowed types]'],
[{ array: [3] }, true],
[{ array: ['12345', 3] }, true],
[{ array: ['3'] }, false, null, 'child "array" fails because ["array" at position 0 does not match any of the allowed types]'],
[{ array: [1] }, false, null, 'child "array" fails because ["array" at position 0 does not match any of the allowed types]']
], done);
});
it('can be disabled', (done) => {
const schema = Joi.object({
array: Joi.array().items(Joi.string().min(5), Joi.number().min(3))
}).strict().strict(false);
Helper.validate(schema, [
[{ array: ['12345'] }, true],
[{ array: ['1'] }, false, null, 'child "array" fails because ["array" at position 0 does not match any of the allowed types]'],
[{ array: [3] }, true],
[{ array: ['12345', 3] }, true],
[{ array: ['3'] }, true],
[{ array: [1] }, false, null, 'child "array" fails because ["array" at position 0 does not match any of the allowed types]']
], done);
});
});
describe('options()', () => {
it('adds to existing options', (done) => {
const schema = Joi.object({ b: Joi.number().strict().options({ convert: true }) });
const input = { b: '2' };
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.b).to.equal(2);
done();
});
});
it('throws with an invalid option', (done) => {
expect(() => {
Joi.any().options({ foo: 'bar' });
}).to.throw('"foo" is not allowed');
done();
});
it('throws with an invalid option type', (done) => {
expect(() => {
Joi.any().options({ convert: 'yes' });
}).to.throw('"convert" must be a boolean');
done();
});
it('throws with an invalid option value', (done) => {
expect(() => {
Joi.any().options({ presence: 'yes' });
}).to.throw('"presence" must be one of [required, optional, forbidden, ignore]');
done();
});
it('does not throw with multiple options including presence key', (done) => {
expect(() => {
Joi.any().options({ presence: 'optional', raw: true });
}).to.not.throw();
done();
});
});
describe('label()', () => {
it('adds to existing options', (done) => {
const schema = Joi.object({ b: Joi.string().email().label('Custom label') });
const input = { b: 'not_a_valid_email' };
schema.validate(input, (err, value) => {
expect(err).to.exist();
expect(err.details[0].message).to.equal('"Custom label" must be a valid email');
done();
});
});
it('throws when label is missing', (done) => {
expect(() => {
Joi.label();
}).to.throw('Label name must be a non-empty string');
done();
});
it('can describe a label', (done) => {
const schema = Joi.object().label('lbl').describe();
expect(schema).to.equal({ type: 'object', label: 'lbl', flags: {} });
done();
});
it('does not leak into sub objects', (done) => {
const schema = Joi.object({ a: Joi.number() }).label('foo');
schema.validate({ a: 'a' }, (err, value) => {
expect(err).to.exist();
expect(err.message).to.equal('child "a" fails because ["a" must be a number]');
done();
});
});
it('does not leak into sub objects from an array', (done) => {
const schema = Joi.array().items(Joi.object({ a: Joi.number() }).label('foo')).label('bar');
schema.validate([{ a: 'a' }], (err, value) => {
expect(err).to.exist();
expect(err.message).to.equal('"bar" at position 0 fails because [child "a" fails because ["a" must be a number]]');
done();
});
});
it('does not leak into unknown keys', (done) => {
const schema = Joi.object({ a: Joi.number() }).label('foo');
schema.validate({ b: 'a' }, (err, value) => {
expect(err).to.exist();
expect(err.message).to.equal('"b" is not allowed');
done();
});
});
});
describe('strict()', () => {
it('adds to existing options', (done) => {
const schema = Joi.object({ b: Joi.number().options({ convert: true }).strict() });
const input = { b: '2' };
schema.validate(input, (err, value) => {
expect(err).to.exist();
expect(value.b).to.equal('2');
done();
});
});
});
describe('raw()', () => {
it('gives the raw input', (done) => {
const tests = [
[Joi.array(), '[1,2,3]'],
[Joi.binary(), 'abc'],
[Joi.boolean(), false],
[Joi.date().format('YYYYMMDD'), '19700101'],
[Joi.number(), '12'],
[Joi.object(), '{ "a": 1 }'],
[Joi.any().strict(), 'abc']
];
tests.forEach((test) => {
const baseSchema = test[0];
const input = test[1];
const schemas = [
baseSchema.raw(),
baseSchema.raw(true)
];
schemas.forEach((schema) => {
schema.raw().validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value).to.equal(input);
});
});
});
done();
});
});
describe('default()', () => {
it('sets the value', (done) => {
const schema = Joi.object({ foo: Joi.string().default('test') });
const input = {};
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.equal('test');
done();
});
});
it('throws when value is a method and no description is provided', (done) => {
expect(() => {
Joi.object({
foo: Joi.string().default(() => {
return 'test';
})
});
}).to.throw();
done();
});
it('allows passing description as a property of a default method', (done) => {
const defaultFn = function () {
return 'test';
};
defaultFn.description = 'test';
expect(() => {
Joi.object({ foo: Joi.string().default(defaultFn) });
}).to.not.throw();
done();
});
it('sets the value when passing a method', (done) => {
const schema = Joi.object({
foo: Joi.string().default(() => {
return 'test';
}, 'testing')
});
const input = {};
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.equal('test');
done();
});
});
it('executes the default method each time validate is called', (done) => {
let count = 0;
const schema = Joi.object({
foo: Joi.number().default(() => {
return ++count;
}, 'incrementer')
});
const input = {};
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.equal(1);
});
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.equal(2);
});
done();
});
it('passes a clone of the context if the default method accepts an argument', (done) => {
const schema = Joi.object({
foo: Joi.string().default((context) => {
return context.bar + 'ing';
}, 'testing'),
bar: Joi.string()
});
const input = { bar: 'test' };
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.equal('testing');
done();
});
});
it('does not modify the original object when modifying the clone in a default method', (done) => {
const defaultFn = function (context) {
context.bar = 'broken';
return 'test';
};
defaultFn.description = 'testing';
const schema = Joi.object({
foo: Joi.string().default(defaultFn),
bar: Joi.string()
});
const input = { bar: 'test' };
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.bar).to.equal('test');
expect(value.foo).to.equal('test');
done();
});
});
it('passes undefined as the context if the default method has no parent', (done) => {
let c;
let methodCalled = false;
const schema = Joi.string().default((context) => {
methodCalled = true;
c = context;
return 'test';
}, 'testing');
schema.validate(undefined, (err, value) => {
expect(err).to.not.exist();
expect(methodCalled).to.equal(true);
expect(c).to.equal(undefined);
expect(value).to.equal('test');
done();
});
});
it('allows passing a method with no description to default when the object being validated is a function', (done) => {
const defaultFn = function () {
return 'just a function';
};
let schema;
expect(() => {
schema = Joi.func().default(defaultFn);
}).to.not.throw();
schema.validate(undefined, (err, value) => {
expect(err).to.not.exist();
expect(value).to.equal(defaultFn);
done();
});
});
it('allows passing a method that generates a default method when validating a function', (done) => {
const defaultFn = function () {
return 'just a function';
};
const defaultGeneratorFn = function () {
return defaultFn;
};
defaultGeneratorFn.description = 'generate a default fn';
let schema;
expect(() => {
schema = Joi.func().default(defaultGeneratorFn);
}).to.not.throw();
schema.validate(undefined, (err, value) => {
expect(err).to.not.exist();
expect(value).to.equal(defaultFn);
done();
});
});
it('allows passing a ref as a default without a description', (done) => {
const schema = Joi.object({
a: Joi.string(),
b: Joi.string().default(Joi.ref('a'))
});
schema.validate({ a: 'test' }, (err, value) => {
expect(err).to.not.exist();
expect(value.a).to.equal('test');
expect(value.b).to.equal('test');
done();
});
});
it('ignores description when passing a ref as a default', (done) => {
const schema = Joi.object({
a: Joi.string(),
b: Joi.string().default(Joi.ref('a'), 'this is a ref')
});
schema.validate({ a: 'test' }, (err, value) => {
expect(err).to.not.exist();
expect(value.a).to.equal('test');
expect(value.b).to.equal('test');
done();
});
});
it('catches errors in default methods', (done) => {
const defaultFn = function () {
throw new Error('boom');
};
defaultFn.description = 'broken method';
const schema = Joi.string().default(defaultFn);
schema.validate(undefined, (err, value) => {
expect(err).to.exist();
expect(err.details).to.have.length(1);
expect(err.details[0].message).to.contain('threw an error when running default method');
expect(err.details[0].type).to.equal('any.default');
expect(err.details[0].context).to.be.an.instanceof(Error);
expect(err.details[0].context.message).to.equal('boom');
done();
});
});
it('should not overide a value when value is given', (done) => {
const schema = Joi.object({ foo: Joi.string().default('bar') });
const input = { foo: 'test' };
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.equal('test');
done();
});
});
it('sets value based on condition (outer)', (done) => {
const schema = Joi.object({
a: Joi.boolean(),
b: Joi.boolean().default(false).when('a', { is: true, then: Joi.required(), otherwise: Joi.forbidden() })
});
schema.validate({ a: false }, (err, value) => {
expect(err).to.not.exist();
expect(value.b).to.equal(false);
done();
});
});
it('sets value based on condition (inner)', (done) => {
const schema = Joi.object({
a: Joi.boolean(),
b: Joi.boolean().when('a', { is: true, then: Joi.default(false), otherwise: Joi.forbidden() })
});
schema.validate({ a: true }, (err, value) => {
expect(err).to.not.exist();
expect(value.b).to.equal(false);
done();
});
});
it('sets value based on multiple conditions (without otherwise)', (done) => {
const schema = Joi.object({
a: Joi.number().required(),
b: Joi.number()
.when('a', { is: 0, then: Joi.valid(1) })
.when('a', { is: 1, then: Joi.valid(2) })
.when('a', { is: 2, then: Joi.valid(3) })
});
Helper.validate(schema, [
[{ a: 0, b: 1 }, true],
[{ a: 0, b: 2 }, false, null, 'child "b" fails because ["b" must be one of [1]]'],
[{ a: 1, b: 2 }, true],
[{ a: 1, b: 3 }, false, null, 'child "b" fails because ["b" must be one of [2]]'],
[{ a: 2, b: 3 }, true],
[{ a: 2, b: 4 }, false, null, 'child "b" fails because ["b" must be one of [3]]'],
[{ a: 42, b: 128 }, true]
], done);
});
it('sets value based on multiple conditions (with otherwise)', (done) => {
const schema = Joi.object({
a: Joi.number().required(),
b: Joi.number()
.when('a', { is: 0, then: Joi.valid(1) })
.when('a', { is: 1, otherwise: Joi.valid(2) })
.when('a', { is: 2, then: Joi.valid(3) })
});
Helper.validate(schema, [
[{ a: 0, b: 1 }, true],
[{ a: 0, b: 2 }, false, null, 'child "b" fails because ["b" must be one of [1]]'],
[{ a: 1, b: 2 }, true],
[{ a: 1, b: 3 }, true],
[{ a: 2, b: 3 }, false, null, 'child "b" fails because ["b" must be one of [2]]'],
[{ a: 2, b: 2 }, true],
[{ a: 42, b: 128 }, false, null, 'child "b" fails because ["b" must be one of [2]]'],
[{ a: 42, b: 2 }, true]
], done);
});
it('sets value based on multiple conditions (with base rules)', (done) => {
const schema = Joi.object({
a: Joi.number().required(),
b: Joi.number().valid(10)
.when('a', { is: 0, then: Joi.valid(1) })
.when('a', { is: 1, then: Joi.valid(2) })
.when('a', { is: 2, then: Joi.valid(3) })
});
Helper.validate(schema, [
[{ a: 0, b: 1 }, true],
[{ a: 0, b: 2 }, false, null, 'child "b" fails because ["b" must be one of [10, 1]]'],
[{ a: 0, b: 10 }, true],
[{ a: 1, b: 2 }, true],
[{ a: 1, b: 3 }, false, null, 'child "b" fails because ["b" must be one of [10, 2]]'],
[{ a: 1, b: 10 }, true],
[{ a: 2, b: 3 }, true],
[{ a: 2, b: 4 }, false, null, 'child "b" fails because ["b" must be one of [10, 3]]'],
[{ a: 2, b: 10 }, true],
[{ a: 42, b: 128 }, false, null, 'child "b" fails because ["b" must be one of [10]]'],
[{ a: 42, b: 10 }, true]
], done);
});
it('creates deep defaults', (done) => {
const schema = Joi.object({
a: Joi.number().default(42),
b: Joi.object({
c: Joi.boolean().default(true),
d: Joi.string()
}).default()
}).default();
Helper.validate(schema, [
[undefined, true, null, { a: 42, b: { c: true } }],
[{ a: 24 }, true, null, { a: 24, b: { c: true } }]
], done);
});
it('should not affect objects other than object when called without an argument', (done) => {
const schema = Joi.object({
a: Joi.number().default()
}).default();
Helper.validate(schema, [
[undefined, true, null, {}],
[{ a: 24 }, true, null, { a: 24 }]
], done);
});
it('should set default value as a clone', (done) => {
const defaultValue = { bar: 'val' };
const schema = Joi.object({ foo: Joi.object().default(defaultValue) });
const input = {};
schema.validate(input, (err, value) => {
expect(err).to.not.exist();
expect(value.foo).to.not.shallow.equal(defaultValue);
expect(value.foo).to.only.include({ bar: 'val' });
value.foo.bar = 'mutated';
schema.validate(input, (err2, value2) => {
expect(err2).to.not.exist();
expect(value2.foo).to.not.shallow.equal(defaultValue);
expect(value2.foo).to.only.include({ bar: 'val' });
done();
});
});
});
it('should not apply default values if the noDefaults option is enquire', (done) => {
const schema = Joi.object({
a: Joi.string().default('foo'),
b: Joi.number()
});
const input = { b: 42 };
Joi.validate(input, schema, { noDefaults: true }, (err, value) => {
expect(err).to.not.exist();
expect(value.a).to.not.exist();
expect(value.b).to.be.equal(42);
done();
});
});
it('should not apply default values from functions if the noDefaults option is enquire', (done) => {
const func = function (context) {
return 'foo';
};
func.description = 'test parameter';
const schema = Joi.object({
a: Joi.string().default(func),
b: Joi.number()
});
const input = { b: 42 };
Joi.validate(input, schema, { noDefaults: true }, (err, value) => {
expect(err).to.not.exist();
expect(value.a).to.not.exist();
expect(value.b).to.be.equal(42);
done();
});
});
it('should not apply default values from references if the noDefaults option is enquire', (done) => {
const schema = Joi.object({
a: Joi.string().default(Joi.ref('b')),
b: Joi.number()
});
const input = { b: 42 };
Joi.validate(input, schema, { noDefaults: true }, (err, value) => {
expect(err).to.not.exist();
expect(value.a).to.not.exist();
expect(value.b).to.be.equal(42);
done();
});
});
});
describe('optional()', () => {
it('validates optional with default required', (done) => {
const schema = Joi.object({
a: Joi.any(),
b: Joi.any(),
c: {
d: Joi.any()
}
}).options({ presence: 'required' });
Helper.validate(schema, [
[{ a: 5 }, false, null, 'child "b" fails because ["b" is required]'],
[{ a: 5, b: 6 }, false, null, 'child "c" fails because ["c" is required]'],
[{ a: 5, b: 6, c: {} }, false, null, 'child "c" fails because [child "d" fails because ["d" is required]]'],
[{ a: 5, b: 6, c: { d: 7 } }, true],
[{}, false, null, 'child "a" fails because ["a" is required]'],
[{ b: 5 }, false, null, 'child "a" fails because ["a" is required]']
], done);
});
});
describe('forbidden()', () => {
it('validates forbidden', (done) => {
const schema = {
a: Joi.number(),
b: Joi.forbidden()
};
Helper.validate(schema, [
[{ a: 5 }, true],
[{ a: 5, b: 6 }, false, null, 'child "b" fails because ["b" is not allowed]'],
[{ a: 'a' }, false, null, 'child "a" fails because ["a" must be a number]'],
[{}, true],
[{ b: undefined }, true],
[{ b: null }, false, null, 'child "b" fails because ["b" is not allowed]']
], done);
});
});
describe('strip()', () => {
it('validates and returns undefined', (done) => {
const schema = Joi.string().strip();
schema.validate('test', (err, value) => {
expect(err).to.not.exist();
expect(value).to.not.exist();
done();
});
});
it('validates and returns an error', (done) => {
const schema = Joi.string().strip();
schema.validate(1, (err, value) => {
expect(err).to.exist();
expect(err.message).to.equal('"value" must be a string');
done();
});
});
});
describe('description()', () => {
it('sets the description', (done) => {
const b = Joi.description('my description');
expect(b._description).to.equal('my description');
done();
});
it('throws when description is missing', (done) => {
expect(() => {
Joi.description();
}).to.throw('Description must be a non-empty string');
done();
});
});
describe('notes()', () => {
it('sets the notes', (done) => {
const b = Joi.notes(['a']).notes('my notes');
expect(b._notes).to.equal(['a', 'my notes']);
done();
});
it('throws when notes are missing', (done) => {
expect(() => {
Joi.notes();
}).to.throw('Notes must be a non-empty string or array');
done();
});
it('throws when notes are invalid', (done) => {
expect(() => {
Joi.notes(5);
}).to.throw('Notes must be a non-empty string or array');
done();
});
});
describe('tags()', () => {
it('sets the tags', (done) => {
const b = Joi.tags(['tag1', 'tag2']).tags('tag3');
expect(b._tags).to.include('tag1');
expect(b._tags).to.include('tag2');
expect(b._tags).to.include('tag3');
done();
});
it('throws when tags are missing', (done) => {
expect(() => {
Joi.tags();
}).to.throw('Tags must be a non-empty string or array');
done();
});
it('throws when tags are invalid', (done) => {
expect(() => {
Joi.tags(5);
}).to.throw('Tags must be a non-empty string or array');
done();
});
});
describe('meta()', () => {
it('sets the meta', (done) => {
const meta = { prop: 'val', prop2: 3 };
let b = Joi.meta(meta);
expect(b.describe().meta).to.equal([meta]);
b = b.meta({ other: true });
expect(b.describe().meta).to.equal([meta, {
other: true
}]);
done();
});
it('throws when meta is missing', (done) => {
expect(() => {
Joi.meta();
}).to.throw('Meta cannot be undefined');
done();
});
});
describe('example()', () => {
it('sets an example', (done) => {
const schema = Joi.valid(5, 6, 7).example(5);
expect(schema._examples).to.include(5);
expect(schema.describe().examples).to.equal([5]);
done();
});
it('does not flatten examples', (done) => {
const schema = Joi.array().items(5, 6, 7).example([5, 6]);
expect(schema._examples).to.equal([[5, 6]]);
expect(schema.describe().examples).to.equal([[5, 6]]);
done();
});
it('throws when tags are missing', (done) => {
expect(() => {
Joi.example();
}).to.throw('Missing example');
done();
});
it('throws when example fails own rules', (done) => {
expect(() => {
Joi.valid(5, 6, 7).example(4);
}).to.throw('Bad example: "value" must be one of [5, 6, 7]');
done();
});
});
describe('unit()', () => {
it('sets the unit', (done) => {
const b = Joi.unit('milliseconds');
expect(b._unit).to.equal('milliseconds');
expect(b.describe().unit).to.equal('milliseconds');
done();
});
it('throws when unit is missing', (done) => {
expect(() => {
Joi.unit();
}).to.throw('Unit name must be a non-empty string');
done();
});
});
describe('_validate()', () => {
it('checks value after conversion', (done) => {
const schema = Joi.number().invalid(2);
Joi.validate('2', schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
done();
});
});
});
describe('validate()', () => {
it('accepts only value (sync way)', (done) => {
const schema = Joi.number();
const result = schema.validate('2');
expect(result).to.equal({ value: 2, error: null });
done();
});