How do you pass image files using React Hook Form and Zod to Server Action? I keep encountering this error:
Error: Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported.
I just can’t figure out what’s causing it. Here’s the Zod schema for the image:
image: z .instanceof(File) .refine( (file) => ACCEPTED_IMAGE_TYPES.includes(file.type), Only the following image types are allowed: ${ACCEPTED_IMAGE_TYPES.join(', ')}. ) .optional() .nullable(),
My reacthookform setup:
const imageFile = new File([compressedFile], compressedFile.name, { type: compressedFile.type || file.type, });
setValue('image', file);
You can see the structure of an image file after compression in the picture.
By default, the compressedFile is a Blob, which is why I’m converting it into a File.
Any ideas on what format I should prepare the file in before submitting, so it’s accepted? I’d be incredibly grateful. This issue has caused me so much frustration. I’d be incredibly grateful for a working solution with zod, react-hook-form.