Skip to content

Commit

Permalink
Revert "Refs can now generate default even when found"
Browse files Browse the repository at this point in the history
This reverts commit e60b712.
  • Loading branch information
Marsup committed Mar 18, 2016
1 parent e5b5293 commit 258e505
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 52 deletions.
1 change: 0 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,6 @@ References support the following arguments:
- `options` - optional settings:
- `separator` - overrides the default `.` hierarchy separator.
- `contextPrefix` - overrides the default `$` context prefix signifier.
- `defaultFor` - optional array defining what should be considered as candidate for default in addition to not finding the reference.
Note that references can only be used where explicitly supported such as in `valid()` or `invalid()` rules. If upwards
(parents) references are needed, use [`object.assert()`](#objectassertref-schema-message).
Expand Down
20 changes: 1 addition & 19 deletions lib/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,16 @@ exports.create = function (key, options) {

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

let ref = function (value, validationOptions) {
const ref = function (value, validationOptions) {

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

if (options && options.defaultFor) {
Hoek.assert(Array.isArray(options.defaultFor), 'defaultFor must be an array');

const oldRef = ref;

ref = function (value, validationOptions) {

const result = oldRef(value, validationOptions);

if (result !== settings.default && settings.defaultFor.indexOf(result) !== -1) {
return Hoek.clone(settings.default);
}

return result;
};
}

ref.isContext = (key[0] === ((settings && settings.contextPrefix) || '$'));
ref.key = (ref.isContext ? key.slice(1) : key);
ref.path = ref.key.split((settings && settings.separator) || '.');
ref.depth = ref.path.length;
ref.root = ref.path[0];
ref.settings = settings;
ref.isJoi = true;

ref.toString = function () {
Expand Down
32 changes: 0 additions & 32 deletions test/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,38 +438,6 @@ describe('ref', () => {
done();
});

it('uses ref default', (done) => {

const schema = Joi.object({
a: Joi.number().min(Joi.ref('b', { default: 2 })),
b: Joi.any()
});

Helper.validate(schema, [
[{ a: 5 }, true],
[{ b: 5 }, true],
[{ a: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]'],
[{ a: 0, b: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 1]']
], done);
});

it('uses ref default with defaultFor', (done) => {

const schema = Joi.object({
a: Joi.number().min(Joi.ref('b', { default: 2, defaultFor: ['', 3] })),
b: Joi.any()
});

Helper.validate(schema, [
[{ a: 5 }, true],
[{ b: 5 }, true],
[{ a: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]'],
[{ a: 0, b: 1 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 1]'],
[{ a: 1, b: '' }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]'],
[{ a: 1, b: 3 }, false, null, 'child "a" fails because ["a" must be larger than or equal to 2]']
], done);
});

describe('create()', () => {

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

0 comments on commit 258e505

Please sign in to comment.