
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
json-schema-to-zod
Advanced tools
The json-schema-to-zod package is a tool that converts JSON Schema definitions into Zod schemas. Zod is a TypeScript-first schema declaration and validation library, and this package helps in bridging the gap between JSON Schema and Zod by automating the conversion process.
Convert JSON Schema to Zod Schema
This feature allows users to convert a JSON Schema into a Zod schema. The code sample demonstrates how to use the `jsonSchemaToZod` function to transform a JSON Schema object into a Zod schema, which can then be used for validation in a TypeScript environment.
const { jsonSchemaToZod } = require('json-schema-to-zod');
const jsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
};
const zodSchema = jsonSchemaToZod(jsonSchema);
console.log(zodSchema.toString());
AJV is a JSON Schema validator that is highly performant and supports JSON Schema draft-07 and later. Unlike json-schema-to-zod, AJV focuses on validating data against JSON Schemas directly rather than converting them to another format like Zod.
Joi is a powerful schema description language and data validator for JavaScript. While it does not directly convert JSON Schema to its format, it serves a similar purpose in defining and validating data schemas, similar to what Zod does after conversion.
This package generates JSON Schema from TypeScript types. It serves the opposite purpose of json-schema-to-zod by converting TypeScript definitions into JSON Schema, rather than converting JSON Schema into another validation format.
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 | 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. |
import { jsonSchemaToZod } from "json-schema-to-zod";
const myObject = {
type: "object",
properties: {
hello: {
type: "string",
},
},
};
const module = jsonSchemaToZod(myObject, { module: "esm" });
const cjs = jsonSchemaToZod(myObject, { module: "cjs", name: "mySchema" });
const schema = jsonSchemaToZod(myObject);
module
import { z } from "zod";
export default z.object({ hello: z.string().optional() });
cjs
const { z } = require("zod");
module.exports = { mySchema: z.object({ hello: z.string().optional() }) };
schema
z.object({ hello: z.string().optional() });
import { z } from "zod"
import { resolveRefs } from "json-refs"
import { format } from "prettier"
import jsonSchemaToZod from "json-schema-to-zod"
async function example(jsonSchema: any) {
const { resolved }= await resolveRefs(jsonSchema)
const code = jsonSchemaToZod(resolved)
const formatted = await format(code, { parser: "typescript" })
return formatted
}
You can pass a function to the overrideParser
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.
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 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 112,843 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 1 open source maintainer 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
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.