Skip to content

Commit

Permalink
fix: add validation to create recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed May 17, 2024
1 parent 508e8e9 commit ac8bef4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion frontend/src/components/InstructionsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { useFormContext } from "react-hook-form";
import { Button } from "./ui/button";
import { PlusIcon } from "lucide-react";
import { Textarea } from "./ui/textarea";
import { FormField, FormMessage } from "./ui/form";

export default function InstructionsInput() {
const [count, setCount] = useState(1);
const { register, setValue, getValues } = useFormContext();
const { register, setValue, getValues, control } = useFormContext();

const deleteIndex = (index: number) => {
setCount(count - 1);
Expand Down Expand Up @@ -36,6 +37,11 @@ export default function InstructionsInput() {
</div>
</div>
))}
<FormField
name="instructions.root"
control={control}
render={() => <FormMessage />}
/>
<Button
onClick={(e) => {
e.preventDefault();
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/routes/create-recipe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ import useAuthStore from "@/services/auth";

const newRecipeSchema = z.object({
name: z.string().min(1),
description: z.string().min(1),
description: z.string().min(1).max(5000),
ingredients: z.array(
z.object({ name: z.string().min(1), amount: z.string() }),
),
instructions: z.array(z.string().min(1)),
instructions: z
.array(z.string().min(1))
.refine(
(val) => val.reduce((acc, curr) => acc + curr.length + 4, 0) < 5000,
"Total length must be less than 5000 characters.",
),
images: z.array(z.string().min(1)),
prepTime: z.coerce.number().min(1),
cookTime: z.coerce.number().min(1),
Expand Down

0 comments on commit ac8bef4

Please sign in to comment.