diff --git a/raillabel_providerkit/validation/validate_schema/validate_schema.py b/raillabel_providerkit/validation/validate_schema/validate_schema.py index c9f8570..c3320a4 100644 --- a/raillabel_providerkit/validation/validate_schema/validate_schema.py +++ b/raillabel_providerkit/validation/validate_schema/validate_schema.py @@ -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 @@ -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: diff --git a/tests/test_raillabel_providerkit/validation/validate_schema/test_validate_schema.py b/tests/test_raillabel_providerkit/validation/validate_schema/test_validate_schema.py index 10a113f..ab65ccb 100644 --- a/tests/test_raillabel_providerkit/validation/validate_schema/test_validate_schema.py +++ b/tests/test_raillabel_providerkit/validation/validate_schema/test_validate_schema.py @@ -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"])