fix parse_json_fields

This commit is contained in:
jxxghp
2024-08-16 17:53:12 +08:00
parent 5d3809b8f5
commit 65ff01b713
2 changed files with 13 additions and 9 deletions

View File

@@ -69,11 +69,13 @@ class Subscribe(BaseModel):
@validator('sites', pre=True)
def parse_json_fields(cls, value):
if value:
try:
return json.loads(value)
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON string: {value}")
return {}
if isinstance(value, str):
try:
return json.loads(value)
except json.JSONDecodeError:
return []
return value
return []
class Config:
orm_mode = True

View File

@@ -26,10 +26,12 @@ class UserBase(BaseModel):
@validator('permissions', 'settings', pre=True)
def parse_json_fields(cls, value):
if value:
try:
return json.loads(value)
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON string: {value}")
if isinstance(value, str):
try:
return json.loads(value)
except json.JSONDecodeError:
return {}
return value
return {}
class Config: