diff --git a/lib/types/alternatives.js b/lib/types/alternatives.js index d59e7256..15a706c6 100755 --- a/lib/types/alternatives.js +++ b/lib/types/alternatives.js @@ -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 diff --git a/test/types/alternatives.js b/test/types/alternatives.js index 1a0d15e1..602e8e72 100755 --- a/test/types/alternatives.js +++ b/test/types/alternatives.js @@ -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()', () => {