feat: add CORS origins string parsing with field validator
Add field_validator to Settings.cors_origins to support comma-separated string input in addition to list format, enabling flexible configuration through environment variables
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from pydantic import AnyHttpUrl, Field
|
||||
from pydantic import AnyHttpUrl, Field, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
@@ -20,6 +20,13 @@ class Settings(BaseSettings):
|
||||
demo_admin_password: str = "ChangeMe_UseEnvInstead"
|
||||
cors_origins: list[AnyHttpUrl] | list[str] = ["http://localhost:5173", "http://localhost:8080"]
|
||||
|
||||
@field_validator("cors_origins", mode="before")
|
||||
@classmethod
|
||||
def parse_cors_origins(cls, value: str | list[str]) -> list[str]:
|
||||
if isinstance(value, str):
|
||||
return [origin.strip() for origin in value.split(",") if origin.strip()]
|
||||
return value
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
|
||||
Reference in New Issue
Block a user