question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate a JSON schema from a `zod.object`

See original GitHub issue

Love 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:

  1. If there’s no interest in implementing this feature, I am happy to jump on it with a fork & PR
  2. 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 a zod object into a JSON Schema field.

Thanks again for the great library!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wilmveelcommented, May 8, 2021

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

1reaction
colinhackscommented, Mar 18, 2021

I think you could safely start developing this against the latest v3 (npm install zod@next). Just published zod@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 the minLength value of z.array(z.string()).min(5) (previously that wasn’t possible).

Read more comments on GitHub >

github_iconTop 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 >
JSON to Zod Schema - Transform Tools
An online playground to convert JSON to Zod Schema.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found