Zod not returning undefined
I have this but it looks like zod has a problem returning undefined. I use prisma and I want it to do nothing which prisma does when value is undefined.
But get this error
11 Replies
I might be missing something because you only posted a small piece of code, but: Why do you need to validate Prisma's return value in the first place?
data is coming from a 3rd party api and going into prisma
It looks like you are missing the parse method.
created: z.string().nullish().transform((val) => {
return val ? new Date(val) : undefined;
}).parse(valFromThirdParty);
have it parsed like this: const parsedData = domainInput.parse({ task: data });
I did notice when I return null it works fine, only when its undefined do I get that type error
Oh, ok. Can you post a bit more of the code you currently have? And yes, I agree, you're propably just missing parse after transform.
Or safeParse with proper error handling
heres the code where the error is happening
And where are you using parse() or safeParse()?
beginning of the function
Pretty tough to see the mistake without knowing what the variables domainInput and p are. I could only guess.
Alternatively, you can try to replicate the issue in a small example that we can view if you don't want to share more of the code.
Alright I suppose it isnt that big of an issue atm since I can return null, thought it might be some kind of bug returning undefined in zod. Ill just keep it as it is, appreciate you trying to help.
Wow figured out what was happening just now:
Some reason undefined was being imported. After removing it works as it should.