errors.js
15.0 KB
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
// Load modules
var Lab = require('lab');
var Code = require('code');
var Joi = require('../lib');
// Declare internals
var internals = {};
// Test shortcuts
var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;
describe('errors', function () {
it('supports custom errors when validating types', function (done) {
var 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');
var input = {
email: 'invalid-email',
date: 'invalid-date',
alphanum: '\b\n\f\r\t',
min: 'ab',
max: 'abcd',
required: 'hello',
xor: '123',
renamed: '456',
notEmpty: ''
};
var 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 }, function (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', function (done) {
Joi.valid('sad').options({ language: { any: { allowOnly: 'my hero "{{key}}" is not {{valids}}' } } }).validate(5, function (err, value) {
expect(err.message).to.equal('my hero "value" is not [sad]');
done();
});
});
it('escapes unsafe keys', function (done) {
var schema = {
'a()': Joi.number()
};
Joi.validate({ 'a()': 'x' }, schema, function (err, value) {
expect(err.message).to.equal('child "a()" fails because ["a()" must be a number]');
Joi.validate({ 'b()': 'x' }, schema, function (err2, value2) {
expect(err2.message).to.equal('"b()" is not allowed');
done();
});
});
});
it('returns error type in validation error', function (done) {
var input = {
notNumber: '',
notString: true,
notBoolean: 9
};
var schema = {
notNumber: Joi.number().required(),
notString: Joi.string().required(),
notBoolean: Joi.boolean().required()
};
Joi.validate(input, schema, { abortEarly: false }, function (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)', function (done) {
var schema = Joi.array().items(Joi.array().items({ x: Joi.number() }));
var input = [
[{ x: 1 }],
[{ x: 1 }, { x: 'a' }]
];
schema.validate(input, function (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)', function (done) {
var schema = Joi.array().items(Joi.array().items(Joi.object({ x: Joi.string() }).forbidden()));
var input = [
[{ x: 1 }],
[{ x: 1 }, { x: 'a' }]
];
schema.validate(input, function (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', function (done) {
var schema = {
x: Joi.array().items({ x: Joi.number() })
};
var input = {
x: [{ x: 1 }, { x: 'a' }]
};
Joi.validate(input, schema, function (err, value) {
expect(err).to.exist();
expect(err.details[0].path).to.equal('x.1.x');
done();
});
});
it('overrides root key language', function (done) {
Joi.string().options({ language: { root: 'blah' } }).validate(4, function (err, value) {
expect(err.message).to.equal('"blah" must be a string');
done();
});
});
it('overrides label key language', function (done) {
Joi.string().options({ language: { key: 'my own {{!key}} ' } }).validate(4, function (err, value) {
expect(err.message).to.equal('my own value must be a string');
done();
});
});
it('overrides wrapArrays', function (done) {
Joi.array().items(Joi.boolean()).options({ language: { messages: { wrapArrays: false } } }).validate([4], function (err, value) {
expect(err.message).to.equal('"value" at position 0 fails because "0" must be a boolean');
done();
});
});
it('allows html escaping', function (done) {
Joi.string().options({ language: { root: 'blah', label: 'bleh' } }).validate(4, function (err, value) {
expect(err.message).to.equal('"bleh" must be a string');
done();
});
});
it('provides context with the error', function (done) {
Joi.object({ length: Joi.number().min(3).required() }).validate({ length: 1 }, function (err) {
expect(err.details).to.deep.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', function (done) {
var schema = Joi.number();
schema.validate('a', function (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', function () {
it('annotates error', function (done) {
var object = {
a: 'm',
y: {
b: {
c: 10
}
}
};
var 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 }, function (err, value) {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"y\": {\n \"b\" \u001b[31m[1]\u001b[0m: {\n \"c\": 10\n },\n \u001b[41m\"u\"\u001b[0m\u001b[31m [2]: -- missing --\u001b[0m\n },\n "a" \u001b[31m[3]\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 within array', function (done) {
var object = {
a: [1, 2, 3, 4, 2, 5]
};
var schema = {
a: Joi.array().items(Joi.valid(1, 2))
};
Joi.validate(object, schema, { abortEarly: false }, function (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', function (done) {
var object = {
a: [2, 3, 4]
};
var schema = {
a: Joi.array().items(Joi.number().min(4).max(2))
};
Joi.validate(object, schema, { abortEarly: false }, function (err, value) {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"a\": [\n 2, \u001b[31m[1]\u001b[0m\n 3, \u001b[31m[3, 2]\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', function (done) {
var object = {
a: [{ b: 2 }]
};
var schema = {
a: Joi.array().items(Joi.number())
};
Joi.validate(object, schema, { abortEarly: false }, function (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', function (done) {
var object = {
a: [2, 3, 4],
b: [2, 3, 4]
};
var 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 }, function (err, value) {
expect(err).to.exist();
expect(err.annotate()).to.equal('{\n \"a\": [\n 2, \u001b[31m[1]\u001b[0m\n 3, \u001b[31m[3, 2]\u001b[0m\n 4 \u001b[31m[4]\u001b[0m\n ],\n \"b\": [\n 2, \u001b[31m[5]\u001b[0m\n 3, \u001b[31m[7, 6]\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', function (done) {
var schema = {
x: [
Joi.string(),
Joi.number(),
Joi.date()
]
};
Joi.validate({ x: true }, schema, function (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', function (done) {
var schema = {
x: Joi.object({
y: Joi.object({
z: Joi.number()
})
})
};
var input = {};
input.x = input;
Joi.validate(input, schema, function (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', function (done) {
var schema = {
x: Joi.object({
y: Joi.object({
z: Joi.number()
})
})
};
var input = { x: { y: {} } };
input.x.y.z = input.x.y;
Joi.validate(input, schema, function (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', function (done) {
var schema = {
x: Joi.object({
y: Joi.object({
z: Joi.number()
})
})
};
var input = { x: { y: { z: 1, foo: {} } } };
input.x.y.foo = input.x.y;
Joi.validate(input, schema, function (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();
});
});
});
});