feat(recipes): return fail(422) when all ingredients filter to empty

Prevents a silent 400 from the backend when the user submits a form
where every ingredient row has quantity <= 0 or blank name.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-10 09:36:41 +02:00
parent 56e6143fd2
commit ebaf42d83d
4 changed files with 63 additions and 18 deletions

View File

@@ -67,6 +67,13 @@ export const actions: Actions = {
return fail(400, { error: 'Ungültige Formulardaten' });
}
const filteredIngredients = (
parsedIngredients as { name: string; quantity: string; unit: string }[]
).filter((ing) => ing.name?.trim() && Number(ing.quantity) > 0);
if (!filteredIngredients.length)
return fail(422, { error: 'Mindestens eine gültige Zutat ist erforderlich' });
const api = apiClient(fetch);
const { error: apiError } = await api.PUT('/v1/recipes/{id}', {
params: { path: { id: params.id } },
@@ -76,14 +83,12 @@ export const actions: Actions = {
cookTimeMin: cookTimeMin ? Number(cookTimeMin) || null : null,
effort,
heroImageUrl,
ingredients: (parsedIngredients as { name: string; quantity: string; unit: string }[])
.filter((ing) => ing.name?.trim() && Number(ing.quantity) > 0)
.map((ing, i) => ({
newIngredientName: ing.name.trim(),
quantity: Number(ing.quantity),
unit: ing.unit || '',
sortOrder: i
})),
ingredients: filteredIngredients.map((ing, i) => ({
newIngredientName: ing.name.trim(),
quantity: Number(ing.quantity),
unit: ing.unit || '',
sortOrder: i
})),
steps: (parsedSteps as string[])
.filter((s) => s?.trim())
.map((s, i) => ({ stepNumber: i + 1, instruction: s.trim() })),