Skip to content

Fix match('one') reporting any when exclusive object alternatives overlap - #3155

Closed
dyk1454683243-sudo wants to merge 3 commits into
hapijs:masterfrom
dyk1454683243-sudo:cursor/fix-alternatives-match-one-error-92fa
Closed

dyk1454683243-sudo wants to merge 3 commits into
hapijs:masterfrom
dyk1454683243-sudo:cursor/fix-alternatives-match-one-error-92fa

Conversation

@dyk1454683243-sudo

@dyk1454683243-sudo dyk1454683243-sudo commented Sep 20, 2026 •

Copy link
Copy Markdown

Fixes #3058.

alternatives.match('one') already reports alternatives.one when more than one alternative validates successfully. Exclusive object alternatives such as { a } vs { b } plus shared keys do not take that path: each object fails only because the other alternative's keys are unknown, so Joi previously reported alternatives.any ("does not match any of the allowed types").

When failed alternatives only differ by keys declared on other alternatives, those sibling keys are stripped and each candidate is revalidated. If more than one then matches, report alternatives.one instead of alternatives.any.

Revalidation is required because abortEarly can hide later object rules (for example .and()) behind an object.unknown error.

Single-alternative matches, shared-only values, unrelated unknown keys, and real field errors are unchanged.

cursoragent and others added 3 commits September 20, 2026 09:54
When every failed alternative only rejected keys declared on the other
alternatives, report alternatives.one instead of alternatives.any.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
Unknown-key failures can hide later object rules when abortEarly is
true. Strip sibling keys and revalidate so overlap detection does not
depend on that preference.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
@dyk1454683243-sudo

Copy link
Copy Markdown
Author

Withdrawing this PR while I clean up a high-volume open-PR backlog. Sorry for the noise — happy to come back later with a focused change if useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

alternatives().match("one") incorrectly reports that there's no match, when the error is that all schemas match

2 participants