zod-to-json-schema  by StefanTerdell

CLI tool for converting Zod schemas to JSON schemas

Created 4 years ago
1,181 stars

Top 32.9% on SourcePulse

GitHubView on GitHub
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

2 months ago

Responsiveness

1 week

Pull Requests (30d)
0
Issues (30d)
0
Star History
11 stars in the last 30 days

Explore Similar Projects

Starred by Marc Klingen Marc Klingen(Cofounder of Langfuse), John Resig John Resig(Author of jQuery; Chief Software Architect at Khan Academy), and
2 more.

zod-gpt by dzhng

0%
625
SDK for structured JSON output from LLMs, validated via Zod schemas
Created 2 years ago
Updated 1 year ago
Feedback? Help us improve.