Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparing Alpha Branch #3698

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading