diff options
author | 2020-12-21 20:31:16 +0200 | |
---|---|---|
committer | 2020-12-21 20:31:16 +0200 | |
commit | faeb656e2ababed373cfc3e793d4c2025d4c6207 (patch) | |
tree | d3a34905473889022bf632514edc068ea03fc056 /backend | |
parent | Merge pull request #40 from python-discord/ks123/form-patch (diff) |
Convert user IDs to string to avoid JS bad behaviours
Diffstat (limited to 'backend')
-rw-r--r-- | backend/models/discord_user.py | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/backend/models/discord_user.py b/backend/models/discord_user.py index e835176..e61c7b3 100644 --- a/backend/models/discord_user.py +++ b/backend/models/discord_user.py @@ -1,14 +1,16 @@ import typing as t -from pydantic import BaseModel +from pydantic import BaseModel, validator class DiscordUser(BaseModel): """Schema model of Discord user for form response.""" - # Discord default fields - id: int # This is actually snowflake, but we simplify it here + # Discord default fields. username: str + # Store ID as str not as int because JavaScript + # doesn't work correctly with big ints. + id: str discriminator: str avatar: t.Optional[str] bot: t.Optional[bool] @@ -22,3 +24,11 @@ class DiscordUser(BaseModel): # Custom fields admin: bool + + @validator("id", pre=True) + def validate_id(cls, value: t.Any) -> t.Any: + """When ID is integer, convert it to string.""" + if isinstance(value, int): + return str(value) + + return value |