Zod schema serialize/deserialize #2030
-
How can I serialise/deserialize zod schema? So that I can store it in database for example as a string and reuse later when needed. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 10 replies
-
Can you explain more about your use case? Perhaps a give a code example. |
Beta Was this translation helpful? Give feedback.
-
I don't think there is a built in way to serialize/deserialize a schema. However, you could definitely write something that would serialize/deserialize a schema. This would be fairly easy for most basic types (string, number, bool), but it might become difficult for more complex objects, records, or arrays. |
Beta Was this translation helpful? Give feedback.
-
This has come up a few times in json-schema-to-zod, which along with its counterpart probably is the closest you'll get to what you're looking for. There has been a couple of requests for a way to do essentially what you're describing here - turn a JSON schema into a Zod schema at runtime. My stance has always been that it doesn't make much sense to turn a perfectly good JSON schema into a Zod schema due to possibly getting weaker validation caused by translation errors or limitations. For instance, there is no (sane) way to deserialize a preprocess callback, and JSTZ even utilizes superRefine to bridge the gap of Zods missing equivalent of the Then again, the request keeps popping up, and as the ecosystem grows it may start to make more sense even with the limitations present. At some point I'd love to find enough time to split json-schema-to-zod into two modules: one to serialize an actual Zod schema and one to stringify it. In the meantime you could just eval the output though. |
Beta Was this translation helpful? Give feedback.
-
I started working on a serialization library for zod that also retains type information. It can be found overe here: https://github.com/commonbaseapp/zodex It's still a WIP, and there are still some battles with TypeScript I'll have to fight (if there's some wizards reading this, reach out if you want to help), but I'm hoping to stabilize soonish! |
Beta Was this translation helpful? Give feedback.
-
Zod, IMO is not made for serializing. If you use it heavily with refinements or processing functions, there is no way to serialize it other than stringifying code. I would suggest to refrain from using this approach. I would suggest sharing your Zod schemas. I have the following.
Both app and server tsconfigs and bundlers include the shared into their projects. if you do not have a mono repo, you might still try using linking for example. Another approach is to publish your models to a private npm. |
Beta Was this translation helpful? Give feedback.
This has come up a few times in json-schema-to-zod, which along with its counterpart probably is the closest you'll get to what you're looking for. There has been a couple of requests for a way to do essentially what you're describing here - turn a JSON schema into a Zod schema at runtime. My stance has always been that it doesn't make much sense to turn a perfectly good JSON schema into a Zod schema due to possibly getting weaker validation caused by translation errors or limitations. For instance, there is no (sane) way to deserialize a preprocess callback, and JSTZ even utilizes superRefine to bridge the gap of Zods missing equivalent of the
oneOf
-keyword! 🤪 Since type inference wouldn…