Skip to content

Commit

Permalink
Merge pull request #3694 from KBVE/patch-atomic-fudster-01-08-2025-17…
Browse files Browse the repository at this point in the history
…36385658

[CI] Merge patch-atomic-fudster-01-08-2025-1736385658 into dev
  • Loading branch information
Fudster authored Jan 9, 2025
2 parents 6e3755b + 471d83a commit 9b13da8
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions apps/pydiscordsh/pydiscordsh/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,29 @@ class DiscordServer(SQLModel, table=True):
class Config:
arbitrary_types_allowed = True
validate_assignment = True
@validator("lang", pre=True, always=True)
def validate_lang(cls, value):
if value:
if len(value) > 2:
raise ValueError("Language list cannot have more than two languages.")
valid_languages = {"en", "es", "zh", "hi", "fr", "ar", "de", "ja", "ru", "pt", "it", "ko", "tr", "vi", "pl"}
for lang in value:
if lang not in valid_languages:
raise ValueError(f"Invalid language code: {lang}. Must be one of {', '.join(cls.valid_languages)}.")
return value

@validator("invite", pre=True, always=True)
def validate_invite(cls, value):
if not value or not isinstance(value, str):
raise ValueError("Invite must be a valid string.")
discord_invite_pattern = r"(?:https?://(?:www\.)?discord(?:\.com)?/invite/|discord\.gg/)([a-zA-Z0-9_-]+)"
match = re.match(discord_invite_pattern, value)
if match:
return match.group(1)
if re.match(r"^[a-zA-Z0-9_-]{1,100}$", value):
return value
raise ValueError("Invalid invite link or invite code.")

@validator("categories", pre=True, always=True)
def validate_categories(cls, value):
if value and len(value) > 2:
Expand All @@ -49,13 +71,12 @@ def validate_categories(cls, value):

@validator("video", pre=True, always=True)
def validate_video(cls, value):
youtube_url_pattern = r"(https?://(?:www\.)?(?:youtube\.com/(?:[^/]+/)*[^/]+(?:\?v=|\/)([a-zA-Z0-9_-]{11}))|youtu\.be/([a-zA-Z0-9_-]{11}))"

youtube_url_pattern = r"(https?://(?:www\.)?(?:youtube\.com/(?:[^/]+/)*[^/]+(?:\?v=|\/)([a-zA-Z0-9_-]{1,50}))|youtu\.be/([a-zA-Z0-9_-]{1,50}))"
if value:
match = re.match(youtube_url_pattern, value)
if match:
return match.group(2) if match.group(2) else match.group(3)
if len(value) == 11 and re.match(r"^[a-zA-Z0-9_-]{11}$", value):
if len(value) < 50 and re.match(r"^[a-zA-Z0-9_-]{1,50}$", value):
return value
raise ValueError("Invalid YouTube video ID or URL.")

Expand Down

0 comments on commit 9b13da8

Please sign in to comment.