Skip to content

Commit

Permalink
added failsafe for bad input data
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-genson committed Mar 11, 2024
1 parent 05e13e6 commit 48fb200
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mealie/schema/user/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
from pathlib import Path
from typing import Annotated
from typing import Annotated, Any
from uuid import UUID

from pydantic import UUID4, ConfigDict, Field, StringConstraints, field_validator
Expand Down Expand Up @@ -119,16 +119,22 @@ def loader_options(cls) -> list[LoaderOption]:
return [joinedload(User.group), joinedload(User.favorite_recipes), joinedload(User.tokens)]

@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: list | None):
if not v:
return []
if not isinstance(v, list):
return v

slugs: list[str] = []
for recipe in v:
if isinstance(recipe, str):
slugs.append(recipe)
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

Expand Down

0 comments on commit 48fb200

Please sign in to comment.