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.
created: z.string().nullish().transform((val) => {
return val ? new Date(val) : undefined;
}),
created: z.string().nullish().transform((val) => {
return val ? new Date(val) : undefined;
}),
But get this error
[TypeScript] Types of property 'creationDate' are incompatible.
[TypeScript] Type '(Date | ((params?: RawCreateParams) => ZodUndefined)) & (Date | ((params?: RawCreateParams) => ZodUndefined) | undefined)' is not assignable to type 'string | Date | NullableDateTimeFieldUpdateOperationsInput | null | undefined'.
[TypeScript] Type '(params?: RawCreateParams) => ZodUndefined' is not assignable to type 'string | Date | NullableDateTimeFieldUpdateOperationsInput | null | undefined'.
[TypeScript]
[TypeScript] 308 update: whois
[TypeScript] Types of property 'creationDate' are incompatible.
[TypeScript] Type '(Date | ((params?: RawCreateParams) => ZodUndefined)) & (Date | ((params?: RawCreateParams) => ZodUndefined) | undefined)' is not assignable to type 'string | Date | NullableDateTimeFieldUpdateOperationsInput | null | undefined'.
[TypeScript] Type '(params?: RawCreateParams) => ZodUndefined' is not assignable to type 'string | Date | NullableDateTimeFieldUpdateOperationsInput | null | undefined'.
[TypeScript]
[TypeScript] 308 update: whois
11 Replies
Christoph
Christoph17mo ago
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?
DivMode
DivModeOP17mo ago
data is coming from a 3rd party api and going into prisma
Alan Ibarra-2310
Alan Ibarra-231017mo ago
It looks like you are missing the parse method. created: z.string().nullish().transform((val) => { return val ? new Date(val) : undefined; }).parse(valFromThirdParty);
DivMode
DivModeOP17mo ago
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
Christoph
Christoph17mo ago
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
DivMode
DivModeOP17mo ago
heres the code where the error is happening
No description
Christoph
Christoph17mo ago
And where are you using parse() or safeParse()?
DivMode
DivModeOP17mo ago
beginning of the function
DivMode
DivModeOP17mo ago
No description
Christoph
Christoph17mo ago
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.
DivMode
DivModeOP16mo ago
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:
import { undefined, z } from "zod";
import { undefined, z } from "zod";
Some reason undefined was being imported. After removing it works as it should.
Want results from more Discord servers?
Add your server