Skip to content

Commit

Permalink
feat: validate_schema for false bool error
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 25, 2024
1 parent 65c1e82 commit bdddbc1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ def _make_errors_readable(errors: ValidationError) -> list[str]:
readable_errors.append(_convert_unexpected_field_error_to_string(error))
elif error["type"] == "literal_error":
readable_errors.append(_convert_literal_error_to_string(error))
elif error["type"] in ["bool_type", "bool_parsing"]:
readable_errors.append(_convert_false_type_error_to_string(error, "bool"))
else:
raise ValueError

return readable_errors


def _build_error_path(loc: list[str]) -> str:
path = "$"
for part in loc:
path += f".{part}"
return path


def _convert_missing_error_to_string(error: dict) -> str:
return f"{_build_error_path(error["loc"][:-1])}: required field '{error["loc"][-1]}' is missing."

Expand All @@ -49,8 +58,8 @@ def _convert_literal_error_to_string(error: dict) -> str:
)


def _build_error_path(loc: list[str]) -> str:
path = "$"
for part in loc:
path += f".{part}"
return path
def _convert_false_type_error_to_string(error: dict, target_type: str) -> str:
return (
f"{_build_error_path(error["loc"][:-1])}: value '{error["input"]}' could not be interpreted "
f"as {target_type}."
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,41 @@ def test_unexpected_value():
assert "'1.0.0'" in actual[0]


def test_wrong_type_bool():
data = {
"openlabel": {
"metadata": {"schema_version": "1.0.0"},
"frames": {
"1": {
"objects": {
"113c2b35-0965-4c80-a212-08b262e94203": {
"object_data": {
"poly2d": [
{
"closed": "NOT A BOOLEAN",
"name": "not_important",
"val": [],
"mode": "MODE_POLY2D_ABSOLUTE",
"coordinate_system": "not_important",
}
]
}
}
}
}
},
}
}

actual = validate_schema(data)
assert len(actual) == 1
assert (
"$.openlabel.frames.1.objects.113c2b35-0965-4c80-a212-08b262e94203.object_data.poly2d.0"
in actual[0]
)
assert "bool" in actual[0]
assert "NOT A BOOLEAN" in actual[0]


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

0 comments on commit bdddbc1

Please sign in to comment.