Gathering detailed insights and metrics for json-schema-to-zod-with-defaults
Gathering detailed insights and metrics for json-schema-to-zod-with-defaults
Gathering detailed insights and metrics for json-schema-to-zod-with-defaults
Gathering detailed insights and metrics for json-schema-to-zod-with-defaults
npm install json-schema-to-zod-with-defaults
Typescript
Module System
Node Version
NPM Version
69.1
Supply Chain
85.5
Quality
74.7
Maintenance
100
Vulnerability
99.3
License
TypeScript (96.29%)
JavaScript (3.71%)
Total Downloads
1,876
Last Day
2
Last Week
4
Last Month
11
Last Year
126
386 Stars
231 Commits
52 Forks
3 Watching
1 Branches
18 Contributors
Minified
Minified + Gzipped
Latest Version
0.2.1
Package Id
json-schema-to-zod-with-defaults@0.2.1
Unpacked Size
25.09 kB
Size
6.61 kB
File Count
45
NPM Version
8.19.2
Node Version
18.12.1
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
33.3%
4
Compared to previous week
Last month
-15.4%
11
Compared to previous month
Last year
70.3%
126
Compared to previous year
3
6
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!
1npm i -g json-schema-to-zod
1json-schema-to-zod -i mySchema.json -o mySchema.ts
$refs
resolved and output formatted1npm i -g json-schema-to-zod json-refs prettier
1json-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 | -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 . |
--noImport | -ni | Removes the import { z } from 'zod'; or equivalent from the output. |
--withJsdocs | -wj | Generate jsdocs off of the description property. |
1import { jsonSchemaToZod } from "json-schema-to-zod"; 2 3const myObject = { 4 type: "object", 5 properties: { 6 hello: { 7 type: "string", 8 }, 9 }, 10}; 11 12const module = jsonSchemaToZod(myObject, { module: "esm" }); 13 14// `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. 15const moduleWithType = jsonSchemaToZod(myObject, { name: "mySchema", module: "esm", type: true }); 16 17const cjs = jsonSchemaToZod(myObject, { module: "cjs", name: "mySchema" }); 18 19const justTheSchema = jsonSchemaToZod(myObject);
module
1import { z } from "zod"; 2 3export default z.object({ hello: z.string().optional() });
moduleWithType
1import { z } from "zod"; 2 3export const mySchema = z.object({ hello: z.string().optional() }); 4export type MySchema = z.infer<typeof mySchema>;
cjs
1const { z } = require("zod"); 2 3module.exports = { mySchema: z.object({ hello: z.string().optional() }) };
justTheSchema
1z.object({ hello: z.string().optional() });
$refs
resolved and output formatted1import { z } from "zod"; 2import { resolveRefs } from "json-refs"; 3import { format } from "prettier"; 4import jsonSchemaToZod from "json-schema-to-zod"; 5 6async function example(jsonSchema: Record<string, unknown>): Promise<string>{ 7 const { resolved } = await resolveRefs(jsonSchema); 8 const code = jsonSchemaToZod(resolved); 9 const formatted = await format(code, { parser: "typescript" }); 10 11 return formatted; 12}
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:
1const zodSchema = eval(jsonSchemaToZod({ type: "string" }, { module: "cjs" })); 2 3zodSchema.safeParse("Please just use Ajv instead");
No vulnerabilities found.
No security vulnerabilities found.