JSPM

  • Created
  • Published
  • Downloads 284925
  • Score
    100M100P100Q178071F
  • License ISC

Converts JSON schema objects or files into Zod schemas

Package Exports

  • json-schema-to-zod
  • json-schema-to-zod/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-schema-to-zod) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Json-Schema-to-Zod

NPM Version NPM Downloads

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

Summary

A runtime package and CLI tool to convert JSON schema (draft 4+) objects or files into Zod schemas in the form of JavaScript code. Uses Prettier for formatting, .

Usage

Online

Just paste your JSON schemas here!

CLI

Installation:

npm i -g json-schema-to-zod

Example:

json-schema-to-zod -s myJson.json -t mySchema.ts

Options

Flag Shorthand Function
--source -s Source file name (required)
--target -t Target file name
--name -n The name of the schema in the output
--deref -d Uses json-schema-ref-parser to dereference the schema
--without-defaults -wd Ignore default values in the schema

Programmatic

jsonSchemaToZod will output the full module code, including a Zod import. If you only need the Zod schema itself, try one of the parsers directly. If you need to deref your JSON schema, try awaiting jsonSchemaDereffed.

import {
  jsonSchemaToZod,
  jsonSchemaToZodDereffed,
  parseSchema,
} from "json-schema-to-zod";

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

const module = jsonSchemaToZod(myObject);

const dereffed = await jsonSchemaToZodDereffed(myObject);

const schema = parseSchema(myObject);

module/dereffed =

import { z } from "zod";

export default z.object({ hello: z.string().optional() });

schema =

z.object({ hello: z.string().optional() });