From 1653c478486227728942ebd8544bb068448cb814 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Wed, 19 Jun 2024 17:17:15 +0200 Subject: [PATCH] fix: correct function type in alternatives error (#3043) --- lib/types/alternatives.js | 9 ++++++--- test/types/alternatives.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) 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()', () => {