Skip to content

Commit

Permalink
Alternatives respect subsequent "when"s if first has only "otherwise"
Browse files Browse the repository at this point in the history
  • Loading branch information
WesTyler committed Apr 14, 2017
1 parent 5450404 commit 19ef7ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ internals.Alternatives = class extends Any {
return baseType._validate(value, state, options);
}
}
else if (item.then === undefined && i < (il - 1)) {
continue;
}
else if (item.then || baseType) {
return (item.then || baseType)._validate(value, state, options);
}
Expand Down
18 changes: 18 additions & 0 deletions test/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ describe('alternatives', () => {
], done);
});

it('validates "then" when a preceding "when" has only "otherwise"', (done) => {

const schema = Joi.object({
a: Joi.number(),
b: Joi.number(),
c: Joi.number()
.when('a', { is: 1, otherwise: Joi.number().min(1) })
.when('b', { is: 1, then: Joi.number().min(1) })
});

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

it('validates when is is null', (done) => {

const schema = {
Expand Down

0 comments on commit 19ef7ce

Please sign in to comment.