Generate a JSON schema from a `zod.object`
See original GitHub issueLove zod
, so thanks for creating the library!
While working on an application, I want to share models and validators across repositories and components, the most common scenario being the backend application and the web frontend view. zod
is great on this.
However, it would also be great to be able to generate a JSON Schema (https://json-schema.org) via the model definition (i.e. zod.object
). These JSON objects can then be used to document API OPTIONS.
I believe that each of the zod field and option can be mapped to a JSON schema entity.
Few notes:
- If there’s no interest in implementing this feature, I am happy to jump on it with a fork & PR
- If you believe that the feature would be out of scope of
zod
I am also happy to create a complementary package, i.e.zod-json-schema
that exposes the utility function to convert the azod
object into a JSON Schema field.
Thanks again for the great library!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:7 (4 by maintainers)
Top Results From Across the Web
zod-to-json-schema - npm
Converts Zod schemas to Json Schemas. Latest version: 3.20.1, last published: 9 days ago. Start using zod-to-json-schema in your project by ...
Read more >StefanTerdell/zod-to-json-schema - GitHub
Converts Zod schemas to Json schemas. Contribute to StefanTerdell/zod-to-json-schema development by creating an account on GitHub.
Read more >Zod | Documentation
json -to-zod : Convert JSON objects into Zod schemas. Live demo. graphql-codegen-typescript-validation-schema : GraphQL Code Generator plugin to generate form ...
Read more >json-schema-to-zod - npm Package Health Analysis - Snyk
A runtime package and CLI tool to convert JSON schema (draft 4+) objects or files into Zod schemas in the form of JavaScript...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, I have created a very basic implementation in zod-endpoints. This function transforms a zod schema into openapi spec. The models are defined as json schema.
https://github.com/flock-community/zod-endpoints/blob/master/src/openapi.ts
I think you could safely start developing this against the latest v3 (
npm install zod@next
). Just publishedzod@3.0.0-alpha.7
which represents most of the major Zod 3 changes. I don’t expect the API to significantly change at this point despite the fact it’s technically still an alpha version. The changes will make it easier to implement something like JSON schema generation — for instance there’s a way to access theminLength
value ofz.array(z.string()).min(5)
(previously that wasn’t possible).