npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-schema-to-zodex

v0.2.0

Published

Converts JSON schema objects or files into Zod schemas

Readme

Json-Schema-to-Zodex

NPM Version NPM Downloads

Summary

A runtime package and CLI tool to convert JSON schema (draft 4+) objects or files into Zodex schemas.

This is a fork of json-schema-to-zod which seeks to allow dynamic evaluation without the need for eval.

Note that with the ability to convert to Zodex JSON, some of the export options are no longer relevant and may be removed.

Looking for the opposite? Check out zod-to-json-schema

Usage

CLI

Simplest example

npm i -g json-schema-to-zodex
json-schema-to-zodex -i mySchema.json -o mySchema.ts

Example with $refs resolved and output formatted

npm i -g json-schema-to-zodex json-refs prettier
json-refs resolve mySchema.json | json-schema-to-zodex | prettier --parser typescript > mySchema.ts

Options

| Flag | Shorthand | Function | | -------------- | --------- | ---------------------------------------------------------------------------------------------- | | --input | -i | JSON or a source file path. Required if no data is piped. | | --output | -o | A file path to write to. If not supplied stdout will be used. | | --name | -n | The name of the schema in the output | | --depth | -d | Maximum depth of recursion in schema before falling back to z.any(). Defaults to 0. | | --module | -m | Module syntax; esm, cjs or none. Defaults to esm in the CLI and none programmaticly. | | --type | -t | Export a named type along with the schema. Requires name to be set and module to be esm. | | --withJsdocs | -wj | Generate jsdocs off of the description property. |

Programmatic

Simple example

import { jsonSchemaToZodex } from "json-schema-to-zodex";

const myObject = {
  type: "object",
  properties: {
    hello: {
      type: "string",
    },
  },
};

// `type` can be either a string or - outside of the CLI - a boolean. If its `true`, the name of the type will be the name of the schema with a capitalized first letter.

const justTheSchema = jsonSchemaToZodex(myObject);

Example with $refs resolved and output formatted

import { z } from "zod";
import { resolveRefs } from "json-refs";
import { format } from "prettier";
import jsonSchemaToZodex from "json-schema-to-zodex";

async function example(jsonSchema: Record<string, unknown>): Promise<string> {
  const { resolved } = await resolveRefs(jsonSchema);
  const code = jsonSchemaToZodex(resolved);
  const formatted = await format(code, { parser: "typescript" });

  return formatted;
}

Overriding a parser

You can pass a function to the parserOverride option, which represents a function that receives the current schema node and the reference object, and should return a string when it wants to replace a default output. If the default output should be used for the node just return void.

Schema factoring

Factored schemas (like object schemas with "oneOf" etc.) is only partially supported. Here be dragons.

Use at Runtime

JSON Schema and Zod does not overlap 100% and the scope of the parsers are purposefully limited in order to help the author avoid a permanent state of chaotic insanity. As this may cause some details of the original schema to be lost in translation, it is instead recommended to use tools such as Ajv to validate your runtime values directly against the original JSON Schema.