Skip to content

Pick the deepest error among the most relevant ones in each separate subschema - #1300

Merged
Julian merged 2 commits into
python-jsonschema:mainfrom
ilia1243:best_error_subschema_2
Sep 7, 2026
Merged

Julian merged 2 commits into
python-jsonschema:mainfrom
ilia1243:best_error_subschema_2

Conversation

@ilia1243

@ilia1243 ilia1243 commented Sep 15, 2024

Copy link
Copy Markdown
Contributor

Improves best_match in the presence of anyOf / oneOf. Calculate the most relevant error in each separate subschema and choose the deepest one.

In particular, for anyOf / oneOf keywords with the only subschema, the best error is resolved as if the subschema was not enclosed by these keywords.

To reproduce:

from jsonschema import Draft202012Validator as Validator, exceptions

for applicator in "anyOf", "oneOf":
    # Should match {"properties": {"foo": {"minProperties": 2}}
    schema = {
        applicator: [
            {
                "properties": {
                    "foo": {
                        "minProperties": 2,
                        "properties": {"bar": {"type": "object"}},
                    },
                },
            },
        ],
    }
    instance = {"foo": {"bar": []}}
    error = exceptions.best_match(Validator(schema).iter_errors(instance))
    print(error)

Revert main code changes in commit b20234e preserving the tests.

Closes: #1257


📚 Documentation preview 📚: https://python-jsonschema--1300.org.readthedocs.build/en/1300/

ilia1243 and others added 2 commits September 15, 2024 13:16
…subschema

Improves `best_match` in the presence of `anyOf` / `oneOf`. Calculate the most relevant error in each separate subschema and choose the deepest one.

In particular for `anyOf` / `oneOf` keywords with the only subschema, the best error is resolved as if the subschema was not enclosed by these keywords.

Revert main code changes in commit b20234e preserving the tests.
Julian added a commit that referenced this pull request Sep 7, 2026
…sition.

Follow-up to #1300, which finds the most relevant error within each
anyOf / oneOf subschema before picking the deepest of those.

This computes each context error's key exactly once rather than once
per comparison, and breaks exact ties by preferring the error earliest
in the instance, so that the result does not depend on the order in
which errors were provided (which dropping `error.path` from
`relevance` had otherwise reintroduced).

Closes: #1257
@Julian
Julian merged commit a677cbe into python-jsonschema:main Sep 7, 2026
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.

Mitigate undesired side effect of new best_match behaviour with alternative proposal

2 participants