Skip to content

Commit

Permalink
fix: correct function type in alternatives error
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Jun 19, 2024
1 parent 7373136 commit ba21526
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,13 @@ internals.errors = function (failures, { error, state }) {
const [type, code] = report.code.split('.');
if (code !== 'base') {
complex.push({ type: schema.type, report });
continue;
}

valids.add(type);
else if (report.code === 'object.base') {
valids.add(report.local.type);
}
else {
valids.add(type);
}
}

// All errors are base types or valids
Expand Down
12 changes: 12 additions & 0 deletions test/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,18 @@ describe('alternatives', () => {
[{ p: 'a' }, false, 'oops']
]);
});

it('validates alternatives with the correct type', () => {

const schema = Joi.alternatives([
Joi.boolean(),
Joi.function()
]);

Helper.validate(schema, [
['wrong', false, '"value" must be one of [boolean, function]']
]);
});
});

describe('when()', () => {
Expand Down

0 comments on commit ba21526

Please sign in to comment.