Skip to content

Commit

Permalink
Fix all references usages. Fixes #1030.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Nov 11, 2016
1 parent 3841154 commit 9f6fb73
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internals.Alternatives = class extends Any {
const item = this._inner.matches[i];
const schema = item.schema;
if (!schema) {
const failed = item.is._validate(item.ref(state.parent, options), null, options, state.parent).errors;
const failed = item.is._validate(item.ref(state.reference || state.parent, options), null, options, state.parent).errors;

if (failed) {
if (item.otherwise) {
Expand Down
2 changes: 1 addition & 1 deletion lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ internals.compare = function (type, compare) {
compareTo = Date.now();
}
else if (isRef) {
compareTo = internals.Date.toDate(date(state.parent, options));
compareTo = internals.Date.toDate(date(state.reference || state.parent, options));

if (!compareTo) {
return this.createError('date.ref', { ref: date.key }, state, options);
Expand Down
4 changes: 2 additions & 2 deletions lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internals.Number = class extends Any {

return this._test('multiple', base, function (value, state, options) {

const divisor = isRef ? base(state.parent, options) : base;
const divisor = isRef ? base(state.reference || state.parent, options) : base;

if (isRef && (typeof divisor !== 'number' || !isFinite(divisor))) {
return this.createError('number.ref', { ref: base.key }, state, options);
Expand Down Expand Up @@ -144,7 +144,7 @@ internals.compare = function (type, compare) {

let compareTo;
if (isRef) {
compareTo = limit(state.parent, options);
compareTo = limit(state.reference || state.parent, options);

if (!(typeof compareTo === 'number' && !isNaN(compareTo))) {
return this.createError('number.ref', { ref: limit.key }, state, options);
Expand Down
2 changes: 1 addition & 1 deletion lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ internals.compare = function (type, compare) {

let compareTo;
if (isRef) {
compareTo = limit(state.parent, options);
compareTo = limit(state.reference || state.parent, options);

if (!Hoek.isInteger(compareTo)) {
return this.createError('string.ref', { ref: limit.key }, state, options);
Expand Down
29 changes: 29 additions & 0 deletions test/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,35 @@ describe('alternatives', () => {
[{ b: 5 }, false, null, 'child "a" fails because ["a" is required]']
], done);
});

it('validates with nested whens', (done) => {

// If ((b === 0 && a === 123) ||
// (b !== 0 && a === anything))
// then c === 456
// else c === 789
const schema = Joi.object({
a: Joi.number().required(),
b: Joi.number().required(),
c: Joi.when('a', {
is: Joi.when('b', {
is: Joi.valid(0),
then: Joi.valid(123)
}),
then: Joi.valid(456),
otherwise: Joi.valid(789)
})
});

Helper.validate(schema, [
[{ a: 123, b: 0, c: 456 }, true],
[{ a: 0, b: 1, c: 456 }, true],
[{ a: 0, b: 0, c: 789 }, true],
[{ a: 123, b: 456, c: 456 }, true],
[{ a: 0, b: 0, c: 456 }, false, null, 'child "c" fails because ["c" must be one of [789]]'],
[{ a: 123, b: 456, c: 789 }, false, null, 'child "c" fails because ["c" must be one of [456]]']
], done);
});
});

describe('describe()', () => {
Expand Down
20 changes: 20 additions & 0 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ describe('date', () => {
], done);
});

it('accepts references as min date within a when', (done) => {

const schema = Joi.object({
a: Joi.date().required(),
b: Joi.date().required(),
c: Joi.number().required().when('a', {
is: Joi.date().min(Joi.ref('b')), // a >= b
then: Joi.number().valid(0)
})
});

Helper.validate(schema, [
[{ a: 123, b: 123, c: 0 }, true],
[{ a: 123, b: 456, c: 42 }, true],
[{ a: 456, b: 123, c: 0 }, true],
[{ a: 123, b: 123, c: 42 }, false, null, 'child "c" fails because ["c" must be one of [0]]'],
[{ a: 456, b: 123, c: 42 }, false, null, 'child "c" fails because ["c" must be one of [0]]']
], done);
});

it('accepts context references as min date', (done) => {

const schema = Joi.object({ b: Joi.date().min(Joi.ref('$a')) });
Expand Down
39 changes: 39 additions & 0 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,26 @@ describe('number', () => {
], done);
});

it('accepts references as min value within a when', (done) => {

const schema = Joi.object({
a: Joi.number().required(),
b: Joi.number().required(),
c: Joi.number().required().when('a', {
is: Joi.number().min(Joi.ref('b')), // a >= b
then: Joi.number().valid(0)
})
});

Helper.validate(schema, [
[{ a: 0, b: 1, c: 42 }, true],
[{ a: 1, b: 1, c: 0 }, true],
[{ a: 2, b: 1, c: 0 }, true],
[{ a: 1, b: 1, c: 42 }, false, null, 'child "c" fails because ["c" must be one of [0]]'],
[{ a: 2, b: 1, c: 42 }, false, null, 'child "c" fails because ["c" must be one of [0]]']
], done);
});

it('accepts context references as min value', (done) => {

const schema = Joi.object({ b: Joi.number().min(Joi.ref('$a')) });
Expand Down Expand Up @@ -859,6 +879,25 @@ describe('number', () => {
], done);
});

it('should handle references correctly within a when', (done) => {

const schema = Joi.object({
a: Joi.number().required(),
b: Joi.number().required(),
c: Joi.number().required().when('a', {
is: Joi.number().multiple(Joi.ref('b')), // a % b === 0
then: Joi.number().valid(0)
})
});

Helper.validate(schema, [
[{ a: 2, b: 3, c: 42 }, true],
[{ a: 2, b: 4, c: 42 }, true],
[{ a: 4, b: 2, c: 0 }, true],
[{ a: 4, b: 2, c: 42 }, false, null, 'child "c" fails because ["c" must be one of [0]]']
], done);
});

it('should handle non-number references correctly', (done) => {

const schema = Joi.object({ a: Joi.string(), b: Joi.number().multiple(Joi.ref('a')) });
Expand Down
18 changes: 18 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ describe('string', () => {
], done);
});

it('accepts references as min length within a when', (done) => {

const schema = Joi.object({
a: Joi.string().required(),
b: Joi.number().required(),
c: Joi.number().required().when('a', {
is: Joi.string().min(Joi.ref('b')), // a.length >= b
then: Joi.number().valid(0)
})
});

Helper.validate(schema, [
[{ a: 'abc', b: 4, c: 42 }, true],
[{ a: 'abc', b: 3, c: 0 }, true],
[{ a: 'abc', b: 3, c: 42 }, false, null, 'child "c" fails because ["c" must be one of [0]]']
], done);
});

it('accepts context references as min length', (done) => {

const schema = Joi.object({ b: Joi.string().min(Joi.ref('$a'), 'utf8') });
Expand Down

0 comments on commit 9f6fb73

Please sign in to comment.