Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce 'best deep match' heuristic #303

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/check_jsonschema/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,33 @@ def _show_validation_error(
if err.context:
best_match = jsonschema.exceptions.best_match(err.context)
self._echo("Underlying errors caused this.", indent=2)
self._echo("")
self._echo("Best Match:", indent=2)
self._echo(self._format_validation_error_message(best_match), indent=4)

best_deep_match = find_best_deep_match(err)
if best_deep_match != best_match:
self._echo("Best Deep Match:", indent=2)
self._echo(
self._format_validation_error_message(best_deep_match), indent=4
)

if self.verbosity > 1:
self._echo("All Errors:", indent=2)
for e in iter_validation_error(err):
self._echo(self._format_validation_error_message(e), indent=4)
else:
num_other_errors = len(list(iter_validation_error(err))) - 1
if best_deep_match != best_match:
num_other_errors -= 1
if num_other_errors > 0:
self._echo("")
self._echo(
f"{click.style(str(num_other_errors), fg='yellow')} other "
"errors were produced. "
"Use '--verbose' to see all errors.",
indent=2,
)

def _show_parse_error(self, filename: str, err: ParseError) -> None:
if self.verbosity < 2:
Expand Down Expand Up @@ -139,10 +160,17 @@ def _dump_error_map(
}
if err.context:
best_match = jsonschema.exceptions.best_match(err.context)
best_deep_match = find_best_deep_match(err)
item["best_match"] = {
"path": best_match.json_path,
"message": best_match.message,
}
item["best_deep_match"] = {
"path": best_deep_match.json_path,
"message": best_deep_match.message,
}
num_sub_errors = len(list(iter_validation_error(err))) - 1
item["num_sub_errors"] = num_sub_errors
if self.verbosity > 1:
item["sub_errors"] = [
{"path": suberr.json_path, "message": suberr.message}
Expand Down Expand Up @@ -176,3 +204,18 @@ def report_errors(self, result: CheckResult) -> None:
"text": TextReporter,
"json": JsonReporter,
}


def _deep_match_relevance(error: jsonschema.ValidationError) -> tuple[bool | int, ...]:
validator = error.validator
return (
validator not in ("anyOf", "oneOf"),
len(error.absolute_path),
-len(error.path),
)


def find_best_deep_match(
errors: jsonschema.ValidationError,
) -> jsonschema.ValidationError:
return max(iter_validation_error(errors), key=_deep_match_relevance)
6 changes: 6 additions & 0 deletions tests/unit/test_reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def test_text_print_validation_error_nested(capsys, verbosity):
assert "$.foo: {} is not of type 'string'" in captured.out
assert "$.bar: {'baz': 'buzz'} is not of type 'string'" in captured.out
assert "$.bar.baz: 'buzz' is not of type 'integer'" in captured.out
else:
assert (
"4 other errors were produced. Use '--verbose' to see all errors."
in captured.out
)


@pytest.mark.parametrize("pretty_json", (True, False))
Expand Down Expand Up @@ -187,6 +192,7 @@ def test_json_format_validation_error_nested(capsys, pretty_json, verbosity):
assert len(data["errors"]) == 1
assert "is not valid under any of the given schemas" in data["errors"][0]["message"]
assert data["errors"][0]["has_sub_errors"]
assert data["errors"][0]["num_sub_errors"] == 5

# stop here unless 'verbosity>=2'
if verbosity < 2:
Expand Down
Loading