Skip to content

Commit

Permalink
feat: validate_schema for unexpected field
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 25, 2024
1 parent 4635a9f commit 216139f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def _make_errors_readable(errors: ValidationError) -> list[str]:
for error in json.loads(errors.json()):
if error["type"] == "missing":
readable_errors.append(_convert_missing_error_to_string(error))
elif error["type"] == "extra_forbidden":
readable_errors.append(_convert_unexpected_field_error_to_string(error))
else:
raise ValueError

Expand All @@ -34,6 +36,10 @@ def _convert_missing_error_to_string(error: dict) -> str:
return f"{_build_error_path(error["loc"][:-1])}: required field '{error["loc"][-1]}' is missing."


def _convert_unexpected_field_error_to_string(error: dict) -> str:
return f"{_build_error_path(error["loc"][:-1])}: found unexpected field '{error["loc"][-1]}'."


def _build_error_path(loc: list[str]) -> str:
path = "$"
for part in loc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@ def test_required_field_missing():
assert "missing" in actual[0]


def test_unsupported_field():
data = {"openlabel": {"metadata": {"schema_version": "1.0.0"}, "UNSUPPORTED_FIELD": {}}}

actual = validate_schema(data)
assert len(actual) == 1
assert "$.openlabel" in actual[0]
assert "unexpected" in actual[0]
assert "UNSUPPORTED_FIELD" in actual[0]


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit 216139f

Please sign in to comment.