StefanTerdell / zod-to-json-schema

Converts Zod schemas to Json schemas
ISC License
886 stars 71 forks source link

`z.Date()` resolves to a `string` type with a `date-time` check #130

Closed Adeniyii closed 2 months ago

Adeniyii commented 2 months ago

When converting back to a zod object, this then gets registered as a ZodString type instead of a ZodDate.

StefanTerdell commented 2 months ago

This is intended behavior. Based on this and the other issue you logged I'm guessing you're using this package and json-schema-to-zod to de/serialize your Zod schemas? It can be useful sometimes, but since JSON schema cannot represent all of Zod and vice versa there will always be some issues. For instance, a JS Date object is not serializable in JSON at all and thus has no JSON schema specification, so the closest you can get it is what you see here.

I've discussed this a bit with the creator of Zod earlier this year, specifically about adding a flag to force the schema to only use serializable components (meaning no transformations without the output defined by a schema, no Date etc.) but I'm not aware of the status of that idea in practice.

Adeniyii commented 2 months ago

Thanks for the detailed response, this all makes sense.

I'm guessing you're using this package and json-schema-to-zod to de/serialize your Zod schemas?

Yes I am, and having to do magic to make it work for my use-cases. So far, I've found work arounds to this issue and the other one I posted, but I imagine there's a lot more quirks around the corner.

Thanks for building this 🚀