Skip to content

Commit

Permalink
Chore(pydiscordsh) adjust deprecated validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Fudster committed Jan 10, 2025
1 parent ee4c1aa commit c91c1ca
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions apps/pydiscordsh/pydiscordsh/api/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional, List
import os, re, html
from sqlmodel import Field, Session, SQLModel, create_engine, select, JSON, Column
from pydantic import validator, root_validator, field_validator
from pydantic import field_validator, model_validator
import logging
from pydiscordsh.api.utils import Utils

Expand All @@ -22,7 +22,7 @@ def _sanitize_string(value: str, user_id: Optional[str] = None, server_id: Optio
raise ValueError("Invalid content in input: Contains potentially harmful characters.")
return html.escape(sanitized)

@root_validator(pre=True)
@model_validator(mode="before")
def sanitize_all_fields(cls, values):
user_id = values.get('user_id', None) #TODO: Pass user ID into this somwhow once we get users done
server_id = values.get('server_id', None)
Expand Down Expand Up @@ -72,7 +72,7 @@ class DiscordServer(SanitizedBaseModel, table=True):
created_at: Optional[int] = Field(default=None, nullable=False) # UNIX timestamp for creation date
updated_at: Optional[int] = Field(default=None, nullable=True) # UNIX timestamp for update date

@validator("website", "logo", "banner", pre=True, always=True)
@field_validator("website", "logo", "banner", "url")
def validate_common_urls(cls, value):
try:
return Utils.validate_url(value)
Expand All @@ -93,7 +93,7 @@ def validate_lang(cls, value):
raise ValueError(f"Invalid language code: {lang}. Must be one of {', '.join(valid_languages)}.")
return value

@validator("invite", pre=True, always=True)
@field_validator("invite")
def validate_invite(cls, value):
if not value or not isinstance(value, str):
raise ValueError("Invite must be a valid string.")
Expand All @@ -106,13 +106,13 @@ def validate_invite(cls, value):
return value
raise ValueError(f"Invalid invite link or invite code. Got: {value}")

@validator("categories", pre=True, always=True)
@field_validator("categories")
def validate_categories(cls, value):
if value and len(value) > 2:
raise ValueError("Categories list cannot have more than 2 items.")
return value

@validator("video", pre=True, always=True)
@field_validator("video")
def validate_video(cls, value):
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:
Expand Down

0 comments on commit c91c1ca

Please sign in to comment.