Skip to content

Commit

Permalink
fix: validate rut on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
kovaxis committed Dec 6, 2023
1 parent 56ce3c5 commit 48a53dd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions backend/app/user/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

class Rut(str):
"""
A code for a major or a minor.
A RUT, like 12345678-K. No dots, no leading zeroes, uppercase K.
"""

_pattern = re.compile(r"^(\d{1,16})-([0-9K])$")

def __new__(cls: type[Self], value: str) -> Self:
return super().__new__(cls, value)
return super().__new__(cls, cls.validate_str(value))

@classmethod
def __get_validators__(
Expand All @@ -25,13 +25,17 @@ def __get_validators__(

@classmethod
def validate(cls: type[Self], value: str, field: ModelField) -> Self:
return cls(cls.validate_str(value))

@classmethod
def validate_str(cls: type[Self], value: str) -> str:
if not isinstance(value, str): # type: ignore
raise TypeError("string required")
value = value.replace(".", "").strip().lstrip("0").upper()
m = cls._pattern.fullmatch(value)
if m is None:
raise ValueError(f"Invalid RUT {value}")
return cls(value)
return value

@classmethod
def __modify_schema__(cls: type[Self], field_schema: dict[str, Any]) -> None:
Expand Down

0 comments on commit 48a53dd

Please sign in to comment.