mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-09 03:04:54 -04:00
added failsafe for bad input data
This commit is contained in:
parent
05e13e6078
commit
21d57735c9
@ -1,6 +1,6 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Annotated
|
from typing import Annotated, Any
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from pydantic import UUID4, ConfigDict, Field, StringConstraints, field_validator
|
from pydantic import UUID4, ConfigDict, Field, StringConstraints, field_validator
|
||||||
@ -119,16 +119,22 @@ class UserOut(UserBase):
|
|||||||
return [joinedload(User.group), joinedload(User.favorite_recipes), joinedload(User.tokens)]
|
return [joinedload(User.group), joinedload(User.favorite_recipes), joinedload(User.tokens)]
|
||||||
|
|
||||||
@field_validator("favorite_recipes", mode="before")
|
@field_validator("favorite_recipes", mode="before")
|
||||||
def convert_favorite_recipes_to_slugs(cls, v: list[str | RecipeSummary] | None):
|
def convert_favorite_recipes_to_slugs(cls, v: Any):
|
||||||
if not v:
|
if not v:
|
||||||
return []
|
return []
|
||||||
|
if not isinstance(v, list):
|
||||||
|
return v
|
||||||
|
|
||||||
slugs: list[str] = []
|
slugs: list[str] = []
|
||||||
for recipe in v:
|
for recipe in v:
|
||||||
if isinstance(recipe, str):
|
if isinstance(recipe, str):
|
||||||
slugs.append(recipe)
|
slugs.append(recipe)
|
||||||
else:
|
else:
|
||||||
slugs.append(recipe.slug)
|
try:
|
||||||
|
slugs.append(recipe.slug)
|
||||||
|
except AttributeError:
|
||||||
|
# this isn't a list of recipes, so we quit early and let Pydantic's typical validation handle it
|
||||||
|
return v
|
||||||
|
|
||||||
return slugs
|
return slugs
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user