Repository navigation
Fix match('one') reporting any when exclusive object alternatives overlap - #3159
Open
SahinurDEV wants to merge 1 commit into
Open
SahinurDEV wants to merge 1 commit into
SahinurDEV wants to merge 1 commit into
Conversation
…rlap
When alternatives().match('one') uses exclusive object schemas (e.g. { a }
vs { b } plus shared keys), each alternative fails only because the other
alternative's keys are unknown. That left matched.length at 0, so Joi
reported alternatives.any instead of alternatives.one.
When failed alternatives only differ by keys declared on sibling
alternatives, strip those sibling keys and revalidate. If more than one
then matches, report alternatives.one. Revalidation is required because
abortEarly can hide later object rules (for example .and()) behind an
object.unknown error.
Fixes hapijs#3058
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3058.
Problem
alternatives().match('one')already reportsalternatives.onewhen 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 reportedalternatives.any("does not match any of the allowed types").That is the opposite of what happened — both schemas would match if the sibling exclusive keys were ignored — so callers cannot tell a true "no match" from a "more than one matched" exclusivity violation.
Fix
When
match('one')finds zero successful matches, check whether the failures are explained only by keys declared on sibling alternatives. Strip those sibling keys and revalidate each candidate. If more than one then matches, reportalternatives.oneinstead ofalternatives.any.Revalidation is required because
abortEarlycan hide later object rules (for example.and()) behind anobject.unknownerror.Single-alternative matches, shared-only values that already take the multi-match path, unrelated unknown keys, and real field errors (invalid email, etc.) are unchanged.
Tests
Added regression coverage for:
alternatives.any.and()interactions where stripping alone would be insufficient without revalidationnpm test— 1841 tests, 100% coverage, lint and types clean.