zod-to-json-schema

Converts Zod validation schemas into standard JSON Schema, ready for OpenAPI, OpenAI, and JSON Schema draft 7 consumers.

Library
npm
v3.25.2
1,251stars
ISC

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
46/100Fair
Development Activity8
Maintenance20
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture82
Code Quality88
Innovation72
Learning Curve85

zod-to-json-schema takes a Zod schema — the runtime validator most TypeScript projects already use for parsing input — and walks its internal definition tree to emit an equivalent JSON Schema document. That output can be handed to anything that expects standard JSON Schema: OpenAPI documentation generators, form builders, LLM function-calling tool definitions, or third-party validators.

Rather than reimplementing Zod’s type system, the library recursively parses each Zod type’s internal _def and matches it against a per-type parser (string, number, object, union, effects, etc.), threading a shared Refs context through the recursion to resolve $ref pointers for recursive and repeated schemas. It supports targeting JSON Schema draft 7, the 2019-09 draft, OpenAPI 3.0, and an OpenAI strict-mode profile, with dozens of configurable strategies for dates, emails, base64 strings, and additional-properties handling.

What You Get

  • A zodToJsonSchema() function that converts any Zod schema (object, union, array, effects, branded types, and more) into a JSON Schema object
  • Per-type parsers for the full Zod type surface — strings with pattern/format handling, numbers, enums, native enums, records, maps, sets, tuples, intersections, discriminated and plain unions, recursive types via $ref
  • Multiple output targets: JSON Schema draft 7 (default), JSON Schema 2019-09, OpenAPI 3.0, and an OpenAI strict-mode profile for structured tool-call schemas
  • Fine-grained strategy options for dates, emails, base64 strings, regex patterns, $ref resolution, and additional-properties behavior
  • An override and postProcess callback API for injecting custom JSON Schema output at specific nodes without forking the library

Common Use Cases

  • Generating OpenAPI request/response schemas from the same Zod schemas already used to validate API input, avoiding schema drift between validation and docs
  • Producing JSON Schema tool/function definitions for LLM function-calling APIs (including an OpenAI strict-mode target purpose-built for this)
  • Feeding form-generation or UI-schema libraries that consume standard JSON Schema instead of Zod’s TypeScript-only API
  • Publishing a public JSON Schema contract for a data shape defined internally with Zod, so non-TypeScript consumers can validate against it

Under The Hood

Architecture: The entry point zodToJsonSchema() (src/zodToJsonSchema.ts) builds a Refs context via getRefs() (src/Refs.ts) that carries the active options, a seen map for cycle detection, and the current JSON pointer path, then calls parseDef() (src/parseDef.ts) on the schema’s internal _def. parseDef dispatches to selectParser.ts, which pattern-matches the Zod type name against one of ~30 per-type modules in src/parsers/ (object.ts, union.ts, effects.ts, record.ts, and so on), each returning a JsonSchema7Type fragment. Recursive or repeated schemas are detected through the seen map and re-emitted as $ref pointers instead of being re-expanded, and the final pass assembles definitions/$defs and the $schema header based on the chosen target.

Tech Stack: Written entirely in TypeScript (99.6% of the codebase) with zod as a peer dependency supporting both v3 schemas and a v3-compatibility mode under Zod v4. The build pipeline uses tsc for type-only, CJS, and ESM output separately (tsconfig.cjs.json / tsconfig.esm.json / tsconfig.types.json) with small tsx post-processing scripts (postcjs.ts, postesm.ts) to fix up the emitted files. ajv, ajv-formats, and a local dist-test harness are used only in dev to validate generated schemas actually parse as JSON Schema.

Code Quality: Test coverage is unusually deep for a library this size — roughly 6,400 lines of tests against 2,400 lines of source, with a dedicated test file per parser under test/parsers/ plus cross-cutting suites (allParsers.test.ts, issues.test.ts, openApiMode.test.ts, openAiMode.test.ts) that exercise every target and strategy combination. Types are strict throughout, with the Options<Target> generic threading the selected output target through the whole call chain so target-specific fields stay type-checked at the call site.

API Design: The public surface is a single function with a well-documented options object (defaultOptions in Options.ts, mirrored by a large table in the README) rather than a class or builder, so a first call needs zero configuration — zodToJsonSchema(mySchema) works out of the box, with named strategy options only needed for edge cases like recursive refs or OpenAI-specific output. The one friction point is that the project is now in maintenance-off mode, with the README pointing new users toward Zod v4’s native z.toJSONSchema() instead.

Used by 29 apps in this directory

TypeScript
88%
Other

AFFiNE

Productivity · Project Management · Note Taking

71,668

Write, draw, and plan in one infinite canvas — the open-source alternative to Notion and Miro that keeps your data yours.

View details
91
Repo Health
87
Technical
68
Dependency
Built with
TypeScript88%
Updated today
TypeScript
84%
MIT

Amical

Note Taking · AI Assistants

1,495

Local-first AI dictation that understands your active app — private, offline, and built for speed.

View details
79
Repo Health
82
Technical
67
Dependency
Built with
TypeScript84%
Updated 3 weeks ago
JavaScript
100%
Other

Automatisch

Automation · No Code Platforms

13,936

Self-hosted, no-code workflow automation that keeps your data on your own servers—a privacy-first alternative to Zapier with 90+ integrations.

View details
49
Repo Health
78
Technical
64
Dependency
Built with
JavaScript100%
Updated 6 months ago
TypeScript
99%
Apache 2.0

Bubble Lab

Automation · AI Development

1,095

Open-core workflow engine for building AI-powered automations in TypeScript that run natively inside Slack — no context switching required.

View details
35
Repo Health
78
Technical
72
Dependency
Built with
TypeScript99%
Updated 3 months ago
TypeScript
99%
Other

byterover-cli

AI Agents · AI Code Assistants

4,941

A portable memory layer for AI coding agents — curate structured project knowledge into a version-controlled context tree that syncs across tools, machines, and teammates.

View details
77
Repo Health
83
Technical
71
Dependency
Built with
TypeScript99%
Updated 1 months ago
TypeScript
97%
Apache 2.0

Cline

AI Code Assistants

66,453

An open-source AI coding agent that lives in your editor and terminal — reads and edits your codebase, runs commands, browses the web, and requires human approval for every action by default.

View details
90
Repo Health
74
Technical
67
Dependency
Built with
TypeScript97%
Updated today
TypeScript
84%
Apache 2.0

Continue

Developer Tools · AI Development · AI Code Assistants

35,532

Open-source coding agent for VS Code, JetBrains, and CLI with support for 30+ LLM providers.

View details
87
Repo Health
88
Technical
65
Dependency
Built with
TypeScript84%
Updated today
TypeScript
45%
Other

Convex Backend

Developer Tools · Databases

12,394

Open-source reactive database that lets developers build live-updating apps with pure TypeScript, strong consistency, and real-time subscriptions—no separate API layer required.

View details
88
Repo Health
82
Technical
70
Dependency
Built with
TypeScript45%
Rust43%
Updated today
TypeScript
72%
AGPL 3.0

Firecrawl

AI Development · Developer Tools

169,311

Turn any website into clean, LLM-ready data with a single API call — no proxy headaches, no scraping complexity.

View details
89
Repo Health
83
Technical
66
Dependency
Built with
TypeScript72%
Python14%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search