Skip to content

Commit

Permalink
Default is. Closes #2078
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 27, 2019
1 parent f684b15 commit 86ad470
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
4 changes: 2 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ Adds conditions that are evaluated during validation and modify the schema befor
- `switch` - an array of `{ is, then }` conditions that are evaluated against the `condition`. The last item in the array may also contain `otherwise`.

If `condition` is a reference:
- one of `is` or `switch` is required.
- if both `is` and `switch` are missing, `is` defaults to `Joi.invalid(null, false, 0, '').required()` (value must be a truthy).
- one of `then`, `otherwise`, or `switch` is required.
- cannot use `is` or `then` with `switch`.
- cannot specify `otherwise` both inside the last `switch` statement and outside.
Expand Down Expand Up @@ -1507,7 +1507,7 @@ Adds a conditional alternative schema type, either based on another key value, o
- `switch` - an array of `{ is, then }` conditions that are evaluated against the `condition`. The last item in the array may also contain `otherwise`.

If `condition` is a reference:
- one of `is` or `switch` is required.
- if both `is` and `switch` are missing, `is` defaults to `Joi.invalid(null, false, 0, '').required()` (value must be a truthy).
- one of `then`, `otherwise`, or `switch` is required.
- cannot use `is` or `then` with `switch`.
- cannot specify `otherwise` both inside the last `switch` statement and outside.
Expand Down
8 changes: 4 additions & 4 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ exports.when = function (schema, condition, options) {
// Single condition

Assert(Ref.isRef(condition) || typeof condition === 'string', 'Invalid condition:', condition);
Assert(options.is !== undefined || options.switch !== undefined, 'Missing "is" or "switch" option');

if (options.is !== undefined) {
if (options.switch === undefined) {
let is = options.is !== undefined ? schema.$_compile(options.is) : schema.$_root.invalid(null, false, 0, '').required();
Assert(options.then !== undefined || options.otherwise !== undefined, 'options must have at least one of "then", "otherwise", or "switch"');

let is = schema.$_compile(options.is);
if (!Ref.isRef(options.is) &&
if (options.is !== undefined &&
!Ref.isRef(options.is) &&
!Common.isSchema(options.is)) {

is = is.required(); // Only apply required if this wasn't already a schema or a ref
Expand Down
64 changes: 63 additions & 1 deletion test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,10 @@ describe('any', () => {
it('validates required values', () => {

Helper.validate(Joi.exist(), [
[4, true],
[0, true],
[null, true],
['', true],
[false, true],
[undefined, false, null, {
message: '"value" is required',
details: [{
Expand Down Expand Up @@ -3080,6 +3083,65 @@ describe('any', () => {
]);
});

it('defaults is to truthy', () => {

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

Helper.validate(schema, [
[{ b: 2 }, true],
[{ a: 1, b: 1 }, true],
[{ b: 1 }, false, null, {
message: '"b" must be one of [2]',
details: [{
message: '"b" must be one of [2]',
path: ['b'],
type: 'any.only',
context: { value: 1, label: 'b', key: 'b', valids: [2] }
}]
}],
[{ a: 0, b: 1 }, false, null, {
message: '"b" must be one of [2]',
details: [{
message: '"b" must be one of [2]',
path: ['b'],
type: 'any.only',
context: { value: 1, label: 'b', key: 'b', valids: [2] }
}]
}],
[{ a: '', b: 1 }, false, null, {
message: '"b" must be one of [2]',
details: [{
message: '"b" must be one of [2]',
path: ['b'],
type: 'any.only',
context: { value: 1, label: 'b', key: 'b', valids: [2] }
}]
}],
[{ a: false, b: 1 }, false, null, {
message: '"b" must be one of [2]',
details: [{
message: '"b" must be one of [2]',
path: ['b'],
type: 'any.only',
context: { value: 1, label: 'b', key: 'b', valids: [2] }
}]
}],
[{ a: null, b: 1 }, false, null, {
message: '"b" must be one of [2]',
details: [{
message: '"b" must be one of [2]',
path: ['b'],
type: 'any.only',
context: { value: 1, label: 'b', key: 'b', valids: [2] }
}]
}]
]);
});

it('sets type whens', () => {

const schema = Joi.object({
Expand Down

0 comments on commit 86ad470

Please sign in to comment.