GithubHelp home page GithubHelp logo

sendyputra / json-schema-to-zod Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stefanterdell/json-schema-to-zod

0.0 0.0 0.0 428 KB

License: ISC License

JavaScript 12.14% TypeScript 87.86%

json-schema-to-zod's Introduction

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
--recursionDepth -rd Maximum depth of recursion in schema before falling back to z.any(). Defaults to 0. `
--module -m Force module syntax ("esm" or "cjs")

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() });

At Runtime

The output of this package is not meant to be used 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)[https://ajv.js.org/] to validate your runtime values directly against the original JSON Schema.

That said, it's possible to use eval. Here's an example that you shouldn't use:

const zodSchema = eval(jsonSchemaToZod({ type: "string" }, { module: "cjs" }));

zodSchema.safeParse("Please just use Ajv instead");

json-schema-to-zod's People

Contributors

benmccann avatar dzakh avatar grimly avatar helgee avatar iamchandru6470 avatar lstrojny avatar mishrakrishnakant avatar navtoj avatar ncardoso-barracuda avatar stefanterdell avatar werifu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.