ref.js
10.6 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
// Load modules
var Lab = require('lab');
var Code = require('code');
var Joi = require('../lib');
var Helper = require('./helper');
// 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('ref', function () {
it('uses ref as a valid value', function (done) {
var schema = Joi.object({
a: Joi.ref('b'),
b: Joi.any()
});
schema.validate({ a: 5, b: 6 }, function (err, value) {
expect(err).to.exist();
expect(err.message).to.equal('child "a" fails because ["a" must be one of [ref:b]]');
Helper.validate(schema, [
[{ a: 5 }, false],
[{ b: 5 }, true],
[{ a: 5, b: 5 }, true],
[{ a: '5', b: '5' }, true]
], done);
});
});
it('uses ref as a valid value (empty key)', function (done) {
var schema = Joi.object({
a: Joi.ref(''),
'': Joi.any()
});
schema.validate({ a: 5, '': 6 }, function (err, value) {
expect(err).to.exist();
expect(err.message).to.equal('child "a" fails because ["a" must be one of [ref:]]');
Helper.validate(schema, [
[{ a: 5 }, false],
[{ '': 5 }, true],
[{ a: 5, '': 5 }, true],
[{ a: '5', '': '5' }, true]
], done);
});
});
it('uses ref with nested keys as a valid value', function (done) {
var schema = Joi.object({
a: Joi.ref('b.c'),
b: {
c: Joi.any()
}
});
schema.validate({ a: 5, b: { c: 6 } }, function (err, value) {
expect(err).to.exist();
expect(err.message).to.equal('child "a" fails because ["a" must be one of [ref:b.c]]');
Helper.validate(schema, [
[{ a: 5 }, false],
[{ b: { c: 5 } }, true],
[{ a: 5, b: 5 }, false],
[{ a: '5', b: { c: '5' } }, true]
], done);
});
});
it('uses ref with combined nested keys in sub child', function (done) {
var ref = Joi.ref('b.c');
expect(ref.root).to.equal('b');
var schema = Joi.object({
a: ref,
b: {
c: Joi.any()
}
});
var input = { a: 5, b: { c: 5 } };
schema.validate(input, function (err, value) {
expect(err).to.not.exist();
var parent = Joi.object({
e: schema
});
parent.validate({ e: input }, function (err2, value2) {
expect(err2).to.not.exist();
done();
});
});
});
it('uses ref reach options', function (done) {
var ref = Joi.ref('b/c', { separator: '/' });
expect(ref.root).to.equal('b');
var schema = Joi.object({
a: ref,
b: {
c: Joi.any()
}
});
schema.validate({ a: 5, b: { c: 5 } }, function (err, value) {
expect(err).to.not.exist();
done();
});
});
it('ignores the order in which keys are defined', function (done) {
var ab = Joi.object({
a: {
c: Joi.number()
},
b: Joi.ref('a.c')
});
ab.validate({ a: { c: '5' }, b: 5 }, function (err, value) {
expect(err).to.not.exist();
var ba = Joi.object({
b: Joi.ref('a.c'),
a: {
c: Joi.number()
}
});
ba.validate({ a: { c: '5' }, b: 5 }, function (err2, value2) {
expect(err2).to.not.exist();
done();
});
});
});
it('uses ref as default value', function (done) {
var schema = Joi.object({
a: Joi.default(Joi.ref('b')),
b: Joi.any()
});
schema.validate({ b: 6 }, function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal({ a: 6, b: 6 });
done();
});
});
it('uses ref as default value regardless of order', function (done) {
var ab = Joi.object({
a: Joi.default(Joi.ref('b')),
b: Joi.number()
});
ab.validate({ b: '6' }, function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal({ a: 6, b: 6 });
var ba = Joi.object({
b: Joi.number(),
a: Joi.default(Joi.ref('b'))
});
ba.validate({ b: '6' }, function (err2, value2) {
expect(err2).to.not.exist();
expect(value2).to.deep.equal({ a: 6, b: 6 });
done();
});
});
});
it('ignores the order in which keys are defined with alternatives', function (done) {
var a = { c: Joi.number() };
var b = [Joi.ref('a.c'), Joi.ref('c')];
var c = Joi.number();
Helper.validate({ a: a, b: b, c: c }, [
[{ a: {} }, true],
[{ a: { c: '5' }, b: 5 }, true],
[{ a: { c: '5' }, b: 6, c: '6' }, true],
[{ a: { c: '5' }, b: 7, c: '6' }, false]
]);
Helper.validate({ b: b, a: a, c: c }, [
[{ a: {} }, true],
[{ a: { c: '5' }, b: 5 }, true],
[{ a: { c: '5' }, b: 6, c: '6' }, true],
[{ a: { c: '5' }, b: 7, c: '6' }, false]
]);
Helper.validate({ b: b, c: c, a: a }, [
[{ a: {} }, true],
[{ a: { c: '5' }, b: 5 }, true],
[{ a: { c: '5' }, b: 6, c: '6' }, true],
[{ a: { c: '5' }, b: 7, c: '6' }, false]
]);
Helper.validate({ a: a, c: c, b: b }, [
[{ a: {} }, true],
[{ a: { c: '5' }, b: 5 }, true],
[{ a: { c: '5' }, b: 6, c: '6' }, true],
[{ a: { c: '5' }, b: 7, c: '6' }, false]
]);
Helper.validate({ c: c, a: a, b: b }, [
[{ a: {} }, true],
[{ a: { c: '5' }, b: 5 }, true],
[{ a: { c: '5' }, b: 6, c: '6' }, true],
[{ a: { c: '5' }, b: 7, c: '6' }, false]
]);
Helper.validate({ c: c, b: b, a: a }, [
[{ a: {} }, true],
[{ a: { c: '5' }, b: 5 }, true],
[{ a: { c: '5' }, b: 6, c: '6' }, true],
[{ a: { c: '5' }, b: 7, c: '6' }, false]
], done);
});
it('uses context as default value', function (done) {
var schema = Joi.object({
a: Joi.default(Joi.ref('$x')),
b: Joi.any()
});
Joi.validate({ b: 6 }, schema, { context: { x: 22 } }, function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal({ a: 22, b: 6 });
done();
});
});
it('uses context as default value with custom prefix', function (done) {
var schema = Joi.object({
a: Joi.default(Joi.ref('%x', { contextPrefix: '%' })),
b: Joi.any()
});
Joi.validate({ b: 6 }, schema, { context: { x: 22 } }, function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal({ a: 22, b: 6 });
done();
});
});
it('uses context as a valid value', function (done) {
var schema = Joi.object({
a: Joi.ref('$x'),
b: Joi.any()
});
Joi.validate({ a: 5, b: 6 }, schema, { context: { x: 22 } }, function (err, value) {
expect(err).to.exist();
expect(err.message).to.equal('child "a" fails because ["a" must be one of [context:x]]');
Helper.validateOptions(schema, [
[{ a: 5 }, false],
[{ a: 22 }, true],
[{ b: 5 }, true],
[{ a: 22, b: 5 }, true],
[{ a: '22', b: '5' }, false]
], { context: { x: 22 } }, done);
});
});
it('uses context in when condition', function (done) {
var schema = {
a: Joi.boolean().when('$x', { is: Joi.exist(), otherwise: Joi.forbidden() })
};
Helper.validate(schema, [
[{}, true],
[{ a: 'x' }, false],
[{ a: true }, false],
[{}, true, { context: {} }],
[{ a: 'x' }, false, { context: {} }],
[{ a: true }, false, { context: {} }],
[{}, true, { context: { x: 1 } }],
[{ a: 'x' }, false, { context: { x: 1 } }],
[{ a: true }, true, { context: { x: 1 } }]
], done);
});
it('uses nested context in when condition', function (done) {
var schema = {
a: Joi.boolean().when('$x.y', { is: Joi.exist(), otherwise: Joi.forbidden() })
};
Helper.validate(schema, [
[{}, true],
[{ a: 'x' }, false],
[{ a: true }, false],
[{}, true, { context: {} }],
[{ a: 'x' }, false, { context: {} }],
[{ a: true }, false, { context: {} }],
[{}, true, { context: { x: 1 } }],
[{ a: 'x' }, false, { context: { x: 1 } }],
[{ a: true }, false, { context: { x: 1 } }],
[{}, true, { context: { x: {} } }],
[{ a: 'x' }, false, { context: { x: {} } }],
[{ a: true }, false, { context: { x: {} } }],
[{}, true, { context: { x: { y: 1 } } }],
[{ a: 'x' }, false, { context: { x: { y: 1 } } }],
[{ a: true }, true, { context: { x: { y: 1 } } }]
], done);
});
it('describes schema with ref', function (done) {
var desc = Joi.compile(Joi.ref('a.b')).describe();
expect(Joi.isRef(desc.valids[0])).to.be.true();
done();
});
describe('#create', function () {
it('throws when key is missing', function (done) {
expect(function () {
Joi.ref(5);
}).to.throw('Invalid reference key: 5');
done();
});
it('finds root with default separator', function (done) {
expect(Joi.ref('a.b.c').root).to.equal('a');
done();
});
it('finds root with default separator and options', function (done) {
expect(Joi.ref('a.b.c', {}).root).to.equal('a');
done();
});
it('finds root with custom separator', function (done) {
expect(Joi.ref('a+b+c', { separator: '+' }).root).to.equal('a');
done();
});
});
});