zod-to-json-schema  by StefanTerdell

CLI tool for converting Zod schemas to JSON schemas

created 4 years ago
1,156 stars

Top 34.2% on sourcepulse

GitHubView on GitHub
1 Expert Loves This Project
Project Summary

This library converts Zod schemas into JSON schemas, targeting developers and teams using Zod for data validation and seeking to generate corresponding JSON schemas for API contracts, documentation, or interoperability. It offers extensive customization for various JSON Schema specifications and Zod features.

How It Works

The library parses Zod schema definitions and translates their validation rules, types, and metadata into the JSON Schema format. It handles complex structures like unions, recursive schemas, and custom validation patterns, mapping Zod's expressive API to JSON Schema's declarative syntax. Advanced options allow fine-grained control over reference resolution, error message inclusion, and target specification versions (e.g., OpenAPI 3.0, JSON Schema Draft 7).

Quick Start & Requirements

  • Install via npm: npm install zod-to-json-schema or yarn: yarn add zod-to-json-schema.
  • Usage: Import zodToJsonSchema from the library and pass your Zod schema.
  • Requires Node.js and the zod package.
  • Official documentation: https://github.com/StefanTerdell/zod-to-json-schema

Highlighted Details

  • Supports targeting JSON Schema Draft 7, 2019-09, OpenAPI 3.0, and OpenAI strict mode.
  • Handles recursive schemas with internal $refs and provides options for $ref resolution strategies.
  • Allows inclusion of custom error messages from Zod schemas for use with tools like ajv-errors.
  • Offers override and postProcess callbacks for deep customization of the generated JSON schema.

Maintenance & Community

Licensing & Compatibility

  • MIT License.
  • Compatible with commercial and closed-source projects.

Limitations & Caveats

The OpenAI target is experimental. .transform output types are not reflected, defaulting to the input schema; use effectStrategy: "any" as a workaround. Zod enums are supported for record keys since v3.11.3, but other non-string key types are ignored. Relative JSON pointers are not widely supported by resolvers. The library does not follow semantic versioning, aligning minor versions with Zod's feature parity.

Health Check
Last commit

1 month ago

Responsiveness

1 week

Pull Requests (30d)
0
Issues (30d)
2
Star History
78 stars in the last 90 days

Explore Similar Projects

Starred by John Resig John Resig(Author of jQuery; Chief Software Architect at Khan Academy), Travis Fischer Travis Fischer(Founder of Agentic), and
1 more.

instructor-js by 567-labs

0%
738
Typescript tool for structured extraction from LLMs
created 1 year ago
updated 6 months ago
Feedback? Help us improve.