Skip to content

Commit

Permalink
Fox pydantic validation errors
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Jul 23, 2024
1 parent 04e809d commit 8b98bb1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions app/core/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, Dict, List, Optional, Union
from typing import Any, List, Optional, Union
from urllib.parse import quote_plus

from cryptography.fernet import Fernet
from pydantic import field_validator
from pydantic_core.core_schema import ValidationInfo
from pydantic_settings import BaseSettings


Expand Down Expand Up @@ -31,10 +32,12 @@ class Settings(BaseSettings):
SQLALCHEMY_DATABASE_URI: Optional[str] = None
DEBUG: bool = False

@field_validator("SQLALCHEMY_DATABASE_URI", pre=True)
def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
@field_validator("SQLALCHEMY_DATABASE_URI", mode="before")
def assemble_db_connection(cls, v: Optional[str], values: ValidationInfo) -> Any:
if isinstance(v, str):
return v

values = values.data
return (
f"postgresql://{quote_plus(values.get('POSTGRES_USER'))}:"
f"{quote_plus(values.get('POSTGRES_PASSWORD'))}@"
Expand All @@ -44,7 +47,7 @@ def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any

SENTRY_DSN: Optional[str] = ""

@field_validator("SENTRY_DSN", pre=True)
@field_validator("SENTRY_DSN", mode="before")
def sentry_dsn_can_be_blank(cls, v: str = "") -> Optional[str]:
if len(v) == 0:
return None
Expand All @@ -57,10 +60,12 @@ def sentry_dsn_can_be_blank(cls, v: str = "") -> Optional[str]:
REDIS_USERNAME: Optional[str] = None
REDIS_URL: Optional[str] = None

@field_validator("REDIS_URL", pre=True)
def assemble_redis_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
@field_validator("REDIS_URL", mode="before")
def assemble_redis_connection(cls, v: Optional[str], values: ValidationInfo) -> Any:
if isinstance(v, str):
return v

values = values.data
user_info = (
f"{quote_plus(values.get('REDIS_USERNAME'))}:{quote_plus(values.get('REDIS_PASSWORD'))}@"
if values.get("REDIS_USERNAME") and values.get("REDIS_PASSWORD")
Expand All @@ -85,7 +90,7 @@ def assemble_redis_connection(cls, v: Optional[str], values: Dict[str, Any]) ->
S3_REGION: str = "eu-west-1"
S3_BUCKET: str = "duva"

@field_validator("CORS_ALLOWED_ORIGINS", pre=True)
@field_validator("CORS_ALLOWED_ORIGINS", mode="before")
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
if isinstance(v, str) and not v.startswith("["):
return [i.strip() for i in v.split(",")]
Expand Down
2 changes: 1 addition & 1 deletion app/core/onadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def get_form(self, form_id: int) -> dict:
self.refresh_access_token()
return self.get_form(form_id)
elif resp.status_code != 200:
logger.error(
logger.debug(
f"{self.unique_id} - Failed to get form {resp.status_code} - "
f"Reason {resp.text}"
)
Expand Down

0 comments on commit 8b98bb1

Please sign in to comment.