Closed Cmacu closed 1 year ago
Hey @Cmacu thanks for the hint. Would it be possible to use customValdators for this use case?
model MyModel {
id Int @id @default(autoincrement())
custom String? /// @zod.custom.use(z.coerce.string())
}
And what do you mean by "issue with parsing json dates". Is this something zod related or something that we can fix in the generator?
@chrishoermann thanks for your response. I will try the custom validator. There are many dates in our schema (transaction stuff), but we can make it work.
In regards to parsing them, it's a zod issue (I think). JSON doesn't support Date values and is casting them to iso strings which than require additional transform to be parsed by Zod, because the default z.date()
expects a valid Date object. Hence the recent z.coerce.date()
feature. There are other ways to do that too, but all require some kind of transform prior to the validation, which is not ideal for API endpoints.
I might be also missing something, though. How do you handle this on your end?
In the current form I just create a z.date()
for prisma DateTime
fields. But I'll investigate if there is a simple solution for this problem that can be implemented directly in the generator like using z.corece.date()
by default for date values? Other options that quickly come to my mind would require a transform which, as you mentioned, is not ideal in your situation.
@chrishoermann FYI, tested your suggestion with z.custom.use
and it worked. This is an improvement and I appreciate it. I would still prefer if the generator is somehow setup to default dates to z.coerce.date()
since it's more inclusive (although maybe it can also cause false positives?). I can look into it deeper when I have more time and maybe submit a PR?
@Cmacu
I just released version 2.0.0-beta.0
that supports z.coerce.date()
by default. Please give it a try.
The beta docs can be found here.
npm:
npm install zod-prisma-types@2.0.0-beta.0
yarn:
yarn add zod-prisma-types@2.0.0-beta.0
pnpm:
pnpm add zod-prisma-types@2.0.0-beta.0
closing since version 2.0.1 is now released and z.coerce.date()
is implemented as standard behaviour
https://zod.dev/?id=coercion-for-primitives
This is in relation to issues with parsing json dates, but I think it can be useful in other scenarios too.