Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
json-schema-to-zod
Advanced tools
Looking for the exact opposite? Check out zod-to-json-schema
A runtime package and CLI tool to convert JSON schema (draft 4+) objects or files into Zod schemas in the form of JavaScript code.
Before v2 it used prettier
for formatting and json-refs
to resolve schemas. To replicate the previous behaviour, please use their respective CLI tools.
Since v2 the CLI supports piped JSON.
Just paste your JSON schemas here!
npm i -g json-schema-to-zod
json-schema-to-zod -i mySchema.json -o mySchema.ts
npm i -g json-schema-to-zod json-refs prettier
json-refs resolve mySchema.json | json-schema-to-zod | prettier --parser typescript > mySchema.ts
Flag | Shorthand | Function |
---|---|---|
--input | -i | JSON or a source file path (required if no data is piped) |
--output | -t | Target file name |
--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 | Force module syntax ("esm" or "cjs" ) |
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 using json-refs
resolve
function before passing in the schema.
import { jsonSchemaToZod, parseSchema } from "json-schema-to-zod";
const myObject = {
type: "object",
properties: {
hello: {
type: "string",
},
},
} as const;
const module = jsonSchemaToZod(myObject);
const schema = parseSchema(myObject);
module
import { z } from "zod";
export default z.object({ hello: z.string().optional() });
schema
z.object({ hello: z.string().optional() });
You can pass a ParserOverride
to the overrideParser
option, which is 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 nothing.
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 in most cases 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");
FAQs
Converts JSON schema objects or files into Zod schemas
The npm package json-schema-to-zod receives a total of 49,260 weekly downloads. As such, json-schema-to-zod popularity was classified as popular.
We found that json-schema-to-zod demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.