Skip to content

Commit 258e505

Browse files
committed
Revert "Refs can now generate default even when found"
This reverts commit e60b712.
1 parent e5b5293 commit 258e505

File tree

3 files changed

+1
-52
lines changed

3 files changed

+1
-52
lines changed

API.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,6 @@ References support the following arguments:
15451545
- `options` - optional settings:
15461546
- `separator` - overrides the default `.` hierarchy separator.
15471547
- `contextPrefix` - overrides the default `$` context prefix signifier.
1548-
- `defaultFor` - optional array defining what should be considered as candidate for default in addition to not finding the reference.
15491548
15501549
Note that references can only be used where explicitly supported such as in `valid()` or `invalid()` rules. If upwards
15511550
(parents) references are needed, use [`object.assert()`](#objectassertref-schema-message).

lib/ref.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,16 @@ exports.create = function (key, options) {
1616

1717
const settings = Hoek.clone(options); // options can be reused and modified
1818

19-
let ref = function (value, validationOptions) {
19+
const ref = function (value, validationOptions) {
2020

2121
return Hoek.reach(ref.isContext ? validationOptions.context : value, ref.key, settings);
2222
};
2323

24-
if (options && options.defaultFor) {
25-
Hoek.assert(Array.isArray(options.defaultFor), 'defaultFor must be an array');
26-
27-
const oldRef = ref;
28-
29-
ref = function (value, validationOptions) {
30-
31-
const result = oldRef(value, validationOptions);
32-
33-
if (result !== settings.default && settings.defaultFor.indexOf(result) !== -1) {
34-
return Hoek.clone(settings.default);
35-
}
36-
37-
return result;
38-
};
39-
}
40-
4124
ref.isContext = (key[0] === ((settings && settings.contextPrefix) || '$'));
4225
ref.key = (ref.isContext ? key.slice(1) : key);
4326
ref.path = ref.key.split((settings && settings.separator) || '.');
4427
ref.depth = ref.path.length;
4528
ref.root = ref.path[0];
46-
ref.settings = settings;
4729
ref.isJoi = true;
4830

4931
ref.toString = function () {

test/ref.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -438,38 +438,6 @@ describe('ref', () => {
438438
done();
439439
});
440440

441-
it('uses ref default', (done) => {
442-
443-
const schema = Joi.object({
444-
a: Joi.number().min(Joi.ref('b', { default: 2 })),
445-
b: Joi.any()
446-
});
447-
448-
Helper.validate(schema, [
449-
[{ a: 5 }, true],
450-
[{ b: 5 }, true],
451-
[{ a: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]'],
452-
[{ a: 0, b: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 1]']
453-
], done);
454-
});
455-
456-
it('uses ref default with defaultFor', (done) => {
457-
458-
const schema = Joi.object({
459-
a: Joi.number().min(Joi.ref('b', { default: 2, defaultFor: ['', 3] })),
460-
b: Joi.any()
461-
});
462-
463-
Helper.validate(schema, [
464-
[{ a: 5 }, true],
465-
[{ b: 5 }, true],
466-
[{ a: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]'],
467-
[{ a: 0, b: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 1]'],
468-
[{ a: 1, b: '' }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]'],
469-
[{ a: 1, b: 3 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]']
470-
], done);
471-
});
472-
473441
describe('create()', () => {
474442

475443
it('throws when key is missing', (done) => {

0 commit comments

Comments
 (0)