-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
errors.js
executable file
·624 lines (479 loc) · 20 KB
/
errors.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
'use strict';
// Load modules
const Lab = require('lab');
const Code = require('code');
const Joi = require('../lib');
// 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('errors', () => {
it('has an isJoi property', (done) => {
Joi.validate('bar', Joi.valid('foo'), (err) => {
expect(err).to.exist();
expect(err.isJoi).to.be.true();
done();
});
});
it('supports custom errors when validating types', (done) => {
const schema = Joi.object({
email: Joi.string().email(),
date: Joi.date(),
alphanum: Joi.string().alphanum(),
min: Joi.string().min(3),
max: Joi.string().max(3),
required: Joi.string().required(),
xor: Joi.string(),
renamed: Joi.string().valid('456'),
notEmpty: Joi.string().required()
}).rename('renamed', 'required').without('required', 'xor').without('xor', 'required');
const input = {
email: 'invalid-email',
date: 'invalid-date',
alphanum: '\b\n\f\r\t',
min: 'ab',
max: 'abcd',
required: 'hello',
xor: '123',
renamed: '456',
notEmpty: ''
};
const lang = {
any: {
empty: '3'
},
date: {
base: '18'
},
string: {
base: '13',
min: '14',
max: '15',
alphanum: '16',
email: '19'
},
object: {
without: '7',
rename: {
override: '11'
}
}
};
Joi.validate(input, schema, { abortEarly: false, language: lang }, (err, value) => {
expect(err).to.exist();
expect(err.name).to.equal('ValidationError');
expect(err.message).to.equal('"value" 11. child "email" fails because ["email" 19]. child "date" fails because ["date" 18]. child "alphanum" fails because ["alphanum" 16]. child "min" fails because ["min" 14]. child "max" fails because ["max" 15]. child "notEmpty" fails because ["notEmpty" 3]. "required" 7. "xor" 7');
done();
});
});
it('does not prefix with key when language uses context.key', (done) => {
Joi.valid('sad').options({ language: { any: { allowOnly: 'my hero "{{key}}" is not {{valids}}' } } }).validate(5, (err, value) => {
expect(err.message).to.equal('my hero "value" is not [sad]');
done();
});
});
it('escapes unsafe keys', (done) => {
const schema = {
'a()': Joi.number()
};
Joi.validate({ 'a()': 'x' }, schema, (err, value) => {
expect(err.message).to.equal('child "a()" fails because ["a()" must be a number]');
Joi.validate({ 'b()': 'x' }, schema, (err2, value2) => {
expect(err2.message).to.equal('"b()" is not allowed');
done();
});
});
});
it('returns error type in validation error', (done) => {
const input = {
notNumber: '',
notString: true,
notBoolean: 9
};
const schema = {
notNumber: Joi.number().required(),
notString: Joi.string().required(),
notBoolean: Joi.boolean().required()
};
Joi.validate(input, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.details).to.have.length(3);
expect(err.details[0].type).to.equal('number.base');
expect(err.details[1].type).to.equal('string.base');
expect(err.details[2].type).to.equal('boolean.base');
done();
});
});
it('returns a full path to an error value on an array (items)', (done) => {
const schema = Joi.array().items(Joi.array().items({ x: Joi.number() }));
const input = [
[{ x: 1 }],
[{ x: 1 }, { x: 'a' }]
];
schema.validate(input, (err, value) => {
expect(err).to.exist();
expect(err.details[0].path).to.equal('1.1.x');
done();
});
});
it('returns a full path to an error value on an array (items forbidden)', (done) => {
const schema = Joi.array().items(Joi.array().items(Joi.object({ x: Joi.string() }).forbidden()));
const input = [
[{ x: 1 }],
[{ x: 1 }, { x: 'a' }]
];
schema.validate(input, (err, value) => {
expect(err).to.exist();
expect(err.details[0].path).to.equal('1.1');
done();
});
});
it('returns a full path to an error value on an object', (done) => {
const schema = {
x: Joi.array().items({ x: Joi.number() })
};
const input = {
x: [{ x: 1 }, { x: 'a' }]
};
Joi.validate(input, schema, (err, value) => {
expect(err).to.exist();
expect(err.details[0].path).to.equal('x.1.x');
done();
});
});
it('overrides root key language', (done) => {
Joi.string().options({ language: { root: 'blah' } }).validate(4, (err, value) => {
expect(err.message).to.equal('"blah" must be a string');
done();
});
});
it('overrides label key language', (done) => {
Joi.string().options({ language: { key: 'my own {{!key}} ' } }).validate(4, (err, value) => {
expect(err.message).to.equal('my own value must be a string');
done();
});
});
it('overrides wrapArrays', (done) => {
Joi.array().items(Joi.boolean()).options({ language: { messages: { wrapArrays: false } } }).validate([4], (err, value) => {
expect(err.message).to.equal('"value" at position 0 fails because "0" must be a boolean');
done();
});
});
it('allows html escaping', (done) => {
Joi.string().options({ language: { root: 'blah' } }).label('bleh').validate(4, (err, value) => {
expect(err.message).to.equal('"bleh" must be a string');
done();
});
});
it('provides context with the error', (done) => {
Joi.object({ length: Joi.number().min(3).required() }).validate({ length: 1 }, (err) => {
expect(err.details).to.equal([{
message: '"length" must be larger than or equal to 3',
path: 'length',
type: 'number.min',
context: {
limit: 3,
key: 'length',
value: 1
}
}]);
done();
});
});
it('has a name that is ValidationError', (done) => {
const schema = Joi.number();
schema.validate('a', (validateErr) => {
expect(validateErr).to.exist();
expect(validateErr.name).to.be.equal('ValidationError');
try {
Joi.assert('a', schema);
throw new Error('should not reach that');
}
catch (assertErr) {
expect(assertErr.name).to.be.equal('ValidationError');
}
try {
Joi.assert('a', schema, 'foo');
throw new Error('should not reach that');
}
catch (assertErr) {
expect(assertErr.name).to.be.equal('ValidationError');
}
try {
Joi.assert('a', schema, new Error('foo'));
throw new Error('should not reach that');
}
catch (assertErr) {
expect(assertErr.name).to.equal('Error');
done();
}
});
});
describe('annotate()', () => {
it('annotates error', (done) => {
const object = {
a: 'm',
y: {
b: {
c: 10
}
}
};
const schema = {
a: Joi.string().valid('a', 'b', 'c', 'd'),
y: Joi.object({
u: Joi.string().valid(['e', 'f', 'g', 'h']).required(),
b: Joi.string().valid('i', 'j').allow(false),
d: Joi.object({
x: Joi.string().valid('k', 'l').required(),
c: Joi.number()
})
})
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"y\": {\n \"b\" \u001b[31m[3]\u001b[0m: {\n \"c\": 10\n },\n \u001b[41m\"u\"\u001b[0m\u001b[31m [2]: -- missing --\u001b[0m\n },\n "a" \u001b[31m[1]\u001b[0m: \"m\"\n}\n\u001b[31m\n[1] "a" must be one of [a, b, c, d]\n[2] "u" is required\n[3] "b" must be a string\u001b[0m');
done();
});
});
it('annotates error without colors if requested', (done) => {
const object = {
a: 'm'
};
const schema = {
a: Joi.string().valid('a', 'b', 'c', 'd')
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate(true)).to.equal('{\n "a" [1]: \"m\"\n}\n\n[1] "a" must be one of [a, b, c, d]');
done();
});
});
it('annotates error within array', (done) => {
const object = {
a: [1, 2, 3, 4, 2, 5]
};
const schema = {
a: Joi.array().items(Joi.valid(1, 2))
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"a\": [\n 1,\n 2,\n 3, \u001b[31m[1]\u001b[0m\n 4, \u001b[31m[2]\u001b[0m\n 2,\n 5 \u001b[31m[3]\u001b[0m\n ]\n}\n\u001b[31m\n[1] \"2\" must be one of [1, 2]\n[2] \"3\" must be one of [1, 2]\n[3] \"5\" must be one of [1, 2]\u001b[0m');
done();
});
});
it('annotates error within array multiple times on the same element', (done) => {
const object = {
a: [2, 3, 4]
};
const schema = {
a: Joi.array().items(Joi.number().min(4).max(2))
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"a\": [\n 2, \u001b[31m[1]\u001b[0m\n 3, \u001b[31m[2, 3]\u001b[0m\n 4 \u001b[31m[4]\u001b[0m\n ]\n}\n\u001b[31m\n[1] \"0\" must be larger than or equal to 4\n[2] \"1\" must be larger than or equal to 4\n[3] \"1\" must be less than or equal to 2\n[4] \"2\" must be less than or equal to 2\u001b[0m');
done();
});
});
it('annotates error within array when it is an object', (done) => {
const object = {
a: [{ b: 2 }]
};
const schema = {
a: Joi.array().items(Joi.number())
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"a\": [\n { \u001b[31m[1]\u001b[0m\n \"b\": 2\n }\n ]\n}\n\u001b[31m\n[1] \"0\" must be a number\u001b[0m');
done();
});
});
it('annotates error within multiple arrays and multiple times on the same element', (done) => {
const object = {
a: [2, 3, 4],
b: [2, 3, 4]
};
const schema = {
a: Joi.array().items(Joi.number().min(4).max(2)),
b: Joi.array().items(Joi.number().min(4).max(2))
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"a\": [\n 2, \u001b[31m[1]\u001b[0m\n 3, \u001b[31m[2, 3]\u001b[0m\n 4 \u001b[31m[4]\u001b[0m\n ],\n \"b\": [\n 2, \u001b[31m[5]\u001b[0m\n 3, \u001b[31m[6, 7]\u001b[0m\n 4 \u001b[31m[8]\u001b[0m\n ]\n}\n\u001b[31m\n[1] \"0\" must be larger than or equal to 4\n[2] \"1\" must be larger than or equal to 4\n[3] \"1\" must be less than or equal to 2\n[4] \"2\" must be less than or equal to 2\n[5] \"0\" must be larger than or equal to 4\n[6] \"1\" must be larger than or equal to 4\n[7] \"1\" must be less than or equal to 2\n[8] \"2\" must be less than or equal to 2\u001b[0m');
done();
});
});
it('displays alternatives fail as a single line', (done) => {
const schema = {
x: [
Joi.string(),
Joi.number(),
Joi.date()
]
};
Joi.validate({ x: true }, schema, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"x\" \u001b[31m[1, 2, 3]\u001b[0m: true\n}\n\u001b[31m\n[1] "x" must be a string\n[2] "x" must be a number\n[3] "x" must be a number of milliseconds or valid date string\u001b[0m');
done();
});
});
it('annotates circular input', (done) => {
const schema = {
x: Joi.object({
y: Joi.object({
z: Joi.number()
})
})
};
const input = {};
input.x = input;
Joi.validate(input, schema, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"x\" \u001b[31m[1]\u001b[0m: \"[Circular ~]\"\n}\n\u001b[31m\n[1] \"x\" is not allowed\u001b[0m');
done();
});
});
it('annotates deep circular input', (done) => {
const schema = {
x: Joi.object({
y: Joi.object({
z: Joi.number()
})
})
};
const input = { x: { y: {} } };
input.x.y.z = input.x.y;
Joi.validate(input, schema, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"x\": {\n \"y\": {\n \"z\" \u001b[31m[1]\u001b[0m: \"[Circular ~.x.y]\"\n }\n }\n}\n\u001b[31m\n[1] \"z\" must be a number\u001b[0m');
done();
});
});
it('annotates deep circular input with extra keys', (done) => {
const schema = {
x: Joi.object({
y: Joi.object({
z: Joi.number()
})
})
};
const input = { x: { y: { z: 1, foo: {} } } };
input.x.y.foo = input.x.y;
Joi.validate(input, schema, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"x\": {\n \"y\": {\n \"z\": 1,\n \"foo\" \u001b[31m[1]\u001b[0m: \"[Circular ~.x.y]\"\n }\n }\n}\n\u001b[31m\n[1] \"foo\" is not allowed\u001b[0m');
done();
});
});
it('prints NaN, Infinity and -Infinity correctly in errors', (done) => {
const schema = {
x: Joi.object({
y: Joi.date().allow(null),
z: Joi.date().allow(null),
u: Joi.date().allow(null),
g: Joi.date().allow(null),
h: Joi.date().allow(null),
i: Joi.date().allow(null),
k: Joi.date().allow(null),
p: Joi.date().allow(null),
f: Joi.date().allow(null)
})
};
const input = {
x: {
y: NaN,
z: Infinity,
u: -Infinity,
g: Symbol('foo'),
h: -Infinity,
i: Infinity,
k: (a) => a,
p: Symbol('bar'),
f: function (x) {
return [{ y: 2 }];
}
}
};
Joi.validate(input, schema, (err, value) => {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"x\": {\n \"z\": Infinity,\n \"u\": -Infinity,\n \"g\": Symbol(foo),\n \"h\": -Infinity,\n \"i\": Infinity,\n \"k\": (a) => a,\n \"p\": Symbol(bar),\n \"f\": function (x) {\\n\\n return [{ y: 2 }];\\n },\n \"y\" \u001b[31m[1]\u001b[0m: NaN\n }\n}\n\u001b[31m\n[1] \"y\" must be a number of milliseconds or valid date string\u001b[0m');
done();
});
});
it('pinpoints an error in a stringified JSON object', (done) => {
const object = {
a: '{"c":"string"}'
};
const schema = {
a: Joi.object({
b: Joi.string()
})
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate(true)).to.equal('{\n \"a\" [1]: \"{\\\"c\\\":\\\"string\\\"}\"\n}\n\n[1] \"c\" is not allowed');
done();
});
});
it('pinpoints several errors in a stringified JSON object', (done) => {
const object = {
a: '{"b":-1.5}'
};
const schema = {
a: Joi.object({
b: Joi.number().integer().positive()
})
};
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate(true)).to.equal('{\n "a" [1, 2]: "{\\"b\\":-1.5}"\n}\n\n[1] "b" must be an integer\n[2] "b" must be a positive number');
done();
});
});
it('pinpoints an error in a stringified JSON object (deep)', (done) => {
const object = {
a: '{"b":{"c":{"d":1}}}'
};
const schema = Joi.object({
a: Joi.object({
b: Joi.object({
c: {
d: Joi.string()
}
})
})
});
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(err.annotate(true)).to.equal('{\n "a" [1]: "{\\"b\\":{\\"c\\":{\\"d\\":1}}}"\n}\n\n[1] "d" must be a string');
done();
});
});
it('handles child to uncle relationship inside a child', (done) => {
const object = {
response: {
options: {
stripUnknown: true
}
}
};
const schema = Joi.object({
response: Joi.object({
modify: Joi.boolean(),
options: Joi.object()
})
.assert('options.stripUnknown', Joi.ref('modify'), 'meet requirement of having peer modify set to true')
});
Joi.validate(object, schema, { abortEarly: false }, (err, value) => {
expect(err).to.exist();
expect(() => {
err.annotate(true);
}).to.not.throw();
done();
});
});
});
});