Skip to content

Commit

Permalink
refactor: replace if-else tree with match
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 25, 2024
1 parent edc5f8e commit 3fd8e0b
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions raillabel_providerkit/validation/validate_schema/validate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,39 @@ def validate_schema(data: dict) -> list[str]:
def _make_errors_readable(errors: ValidationError) -> list[str]: # noqa: C901
readable_errors = []
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))
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"))
elif error["type"] in ["int_type", "int_parsing", "int_from_float"]:
readable_errors.append(_convert_false_type_error_to_string(error, "int"))
elif error["type"] in ["decimal_type", "decimal_parsing"]:
readable_errors.append(_convert_false_type_error_to_string(error, "Decimal"))
elif error["type"] in ["string_type", "string_parsing"]:
readable_errors.append(_convert_false_type_error_to_string(error, "str"))
elif error["type"] in ["float_type", "float_parsing"]:
readable_errors.append(_convert_false_type_error_to_string(error, "float"))
elif error["type"] in ["uuid_type", "uuid_parsing"]:
readable_errors.append(_convert_false_type_error_to_string(error, "UUID"))
elif error["type"] == "too_long":
readable_errors.append(_convert_too_long_error_to_string(error))
else:
readable_errors.append(str(error))
match error["type"]:
case "missing":
readable_errors.append(_convert_missing_error_to_string(error))

case "extra_forbidden":
readable_errors.append(_convert_unexpected_field_error_to_string(error))

case "literal_error":
readable_errors.append(_convert_literal_error_to_string(error))

case "bool_type" | "bool_parsing":
readable_errors.append(_convert_false_type_error_to_string(error, "bool"))

case "int_type" | "int_parsing" | "int_from_float":
readable_errors.append(_convert_false_type_error_to_string(error, "int"))

case "decimal_type" | "decimal_parsing":
readable_errors.append(_convert_false_type_error_to_string(error, "Decimal"))

case "string_type" | "string_parsing":
readable_errors.append(_convert_false_type_error_to_string(error, "str"))

case "float_type" | "float_parsing":
readable_errors.append(_convert_false_type_error_to_string(error, "float"))

case "uuid_type" | "uuid_parsing":
readable_errors.append(_convert_false_type_error_to_string(error, "UUID"))

case "too_long":
readable_errors.append(_convert_too_long_error_to_string(error))

case _:
readable_errors.append(str(error))

return readable_errors

Expand Down

0 comments on commit 3fd8e0b

Please sign in to comment.