You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to make file uploads type safe using zx.parseFormSafe but I'm not able to parse the NodeOnDiskFile class.
importtype{ActionArgs}from"@remix-run/node";import{json,NodeOnDiskFile,unstable_composeUploadHandlers,unstable_createFileUploadHandler,unstable_createMemoryUploadHandler,unstable_parseMultipartFormData,}from"@remix-run/node";importtype{ZodError}from"zod";import{z}from"zod";import{zx}from"zodix";functionerrorAtPath(error: ZodError,path: string){returnerror.issues.find((issue)=>issue.path[0]===path)?.message;}exportasyncfunctionaction({ request }: ActionArgs){constuploadHandler=unstable_composeUploadHandlers(unstable_createFileUploadHandler({maxPartSize: 5_000_000,file: ({ filename })=>filename,}),// parse everything else into memoryunstable_createMemoryUploadHandler());constformData=awaitunstable_parseMultipartFormData(request,uploadHandler);constresults=awaitzx.parseFormSafe(formData,{file: z.instanceof(NodeOnDiskFile),// And some more fields that work just fine 👌});if(results.success){returnawaitdoSomethingWithImage(results.data);}else{returnjson({success: results.success,error: errorAtPath(results.error,"image"),});}}
formData.get("image") instanceof NodeOnDiskFile actually evaluates to true.
I found a rather similar issue on the zod repo where running z.instanceof(File) on the server resulted in a Reference Error because the File class is not defined in Node, which is not the case of NodeOnDiskFile.
I tried another method to parse the data which would result in an any type inference but would at least throw for missing data :
constresults=awaitzx.parseFormSafe(formData,{image: z.any().refine((file: unknown)=>{console.log({ file });returnBoolean(file);},"Image is required."),});
Which logged { file: '[object File]' } suggesting the file is casted to a string before it reaches the refinement function.
I don't know if this issue comes from zod or from zodix but maybe someone encountered the issue before and found a workaround.
Thanks!
The text was updated successfully, but these errors were encountered:
If you can think of a simple way to add support, I'd be open to a PR, otherwise you may have to get the file from the FormData separately and handle validation directly with Zod.
Ok, thanks! I was wondering whether I was doing something wrong 😅
The URLSearchParams trick indeed explains the "[object File]" string in the console output as explained on the UrlSearchParams MDN page :
A literal sequence of name-value string pairs, or any object — such as a FormData object — with an iterator that produces a sequence of string pairs. Note that File entries will be serialized as [object File] rather than as their filename (as they would in an application/x-www-form-urlencoded form).
I will look into it and let you know if I find a way to make it work without adding too much overhead.
I'm trying to make file uploads type safe using
zx.parseFormSafe
but I'm not able to parse theNodeOnDiskFile
class.formData.get("image") instanceof NodeOnDiskFile
actually evaluates totrue
.I found a rather similar issue on the
zod
repo where runningz.instanceof(File)
on the server resulted in aReference Error
because theFile
class is not defined in Node, which is not the case ofNodeOnDiskFile
.I tried another method to parse the data which would result in an any type inference but would at least throw for missing data :
Which logged
{ file: '[object File]' }
suggesting the file is casted to a string before it reaches the refinement function.I don't know if this issue comes from
zod
or fromzodix
but maybe someone encountered the issue before and found a workaround.Thanks!
The text was updated successfully, but these errors were encountered: