Replies: 1 comment
-
Same here. I guess it is caused by the one of the latest TypeScript features. Avoiding non-null inference may help you. const personSchema = z.object({
avatar: z
.instanceof(File)
.nullable()
.refine((file) => (file !== null ? true : false), 'The avatar is required'),
}); const personSchema = z
.object({
avatar: z.instanceof(File).nullable(),
})
.superRefine((data, ctx) => {
if (data.avatar === null) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'The avatar is required',
path: ['avatar'],
});
}
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to validate a schema with Zod that has an avatar property of type File. I want to set it to null by default so I can easily validate whether there is an avatar or not. My schema looks like this:
However, the problem is that even though I use .nullable(), the .refine() method changes the avatar type to File instead of File | null. This prevents me from setting null as a default value:
is there any way to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions