-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UInput with type='file', how to validate with Zod? #2462
Comments
Try to use Zod's I think in your situation you'd need to do something like this: z.object({
picture: z.union([z.instanceof(File), z.instanceof(FileList)])
.transform((val) => {
if (val instanceof File) return val;
if (val instanceof FileList) return val[0];
return null;
})
}) |
Hi @noook, thanks for your reply. When I try it, my app won't build and i get an 500 error:
When I use File alone and I select an image, i get:
z.object({
picture: z.instanceof(File)
.transform((val) => {
if (val instanceof File) return val;
if (val instanceof FileList) return val[0];
return null;
})
})
|
And when i make the component 'clientOnly' i get |
Can you try to make a reproducible example so that I can play with it ? |
@noook, sure, here you go! https://stackblitz.com/edit/nuxt-starter-mkx9qp?file=app.vue |
I tried with this example: https://stackblitz.com/edit/nuxt-starter-adyxmi?file=components%2FMyForm.client.vue Basically what I did:
However, I can't figure out why the transform is not working, it still outputs me a FileList on the Zod's parse output. |
For what version of Nuxt UI are you asking this question?
v2.x
Description
How can i use Zod to validate an UInput file field?
Let's say you have the following code:
(I found the example over here.)
As aspected the following onChange function logs a FileList
But when I log val in the transform function of Zod
I get a string like this:
Can someone please help me out, i'm looking for a few days now for a solution.
The text was updated successfully, but these errors were encountered: