json-schema-to-zod
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -36,2 +36,6 @@ #!/usr/bin/env node | ||
}, | ||
type: { | ||
shortHand: "t", | ||
value: "string", | ||
}, | ||
}; | ||
@@ -38,0 +42,0 @@ async function main() { |
@@ -5,3 +5,6 @@ "use strict"; | ||
const parseSchema_js_1 = require("./parsers/parseSchema.js"); | ||
const jsonSchemaToZod = (schema, { module, name, ...rest } = {}) => { | ||
const jsonSchemaToZod = (schema, { module, name, type, ...rest } = {}) => { | ||
if (type && (!name || module !== "esm")) { | ||
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`"); | ||
} | ||
let result = (0, parseSchema_js_1.parseSchema)(schema, { | ||
@@ -29,4 +32,11 @@ module, | ||
} | ||
if (type && name) { | ||
let typeName = typeof type === "string" | ||
? type | ||
: `${name[0].toUpperCase()}${name.substring(1)}`; | ||
result += `export type ${typeName} = z.infer<typeof ${name}> | ||
`; | ||
} | ||
return result; | ||
}; | ||
exports.jsonSchemaToZod = jsonSchemaToZod; |
@@ -34,2 +34,6 @@ #!/usr/bin/env node | ||
}, | ||
type: { | ||
shortHand: "t", | ||
value: "string", | ||
}, | ||
}; | ||
@@ -36,0 +40,0 @@ async function main() { |
import { parseSchema } from "./parsers/parseSchema.js"; | ||
export const jsonSchemaToZod = (schema, { module, name, ...rest } = {}) => { | ||
export const jsonSchemaToZod = (schema, { module, name, type, ...rest } = {}) => { | ||
if (type && (!name || module !== "esm")) { | ||
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`"); | ||
} | ||
let result = parseSchema(schema, { | ||
@@ -25,3 +28,10 @@ module, | ||
} | ||
if (type && name) { | ||
let typeName = typeof type === "string" | ||
? type | ||
: `${name[0].toUpperCase()}${name.substring(1)}`; | ||
result += `export type ${typeName} = z.infer<typeof ${name}> | ||
`; | ||
} | ||
return result; | ||
}; |
import { Options, JsonSchema } from "./Types.js"; | ||
export declare const jsonSchemaToZod: (schema: JsonSchema, { module, name, ...rest }?: Options) => string; | ||
export declare const jsonSchemaToZod: (schema: JsonSchema, { module, name, type, ...rest }?: Options) => string; |
@@ -55,2 +55,3 @@ export type Serializable = { | ||
depth?: number; | ||
type?: boolean | string; | ||
}; | ||
@@ -57,0 +58,0 @@ export type Refs = Options & { |
{ | ||
"name": "json-schema-to-zod", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Converts JSON schema objects or files into Zod schemas", | ||
@@ -62,2 +62,2 @@ "types": "./dist/types/index.d.ts", | ||
} | ||
} | ||
} |
@@ -38,9 +38,10 @@ # Json-Schema-to-Zod | ||
| 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. | | ||
| 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. | | ||
| `--type` | `-t` | Export a named type along with the schema. Requires `name` to be set and `module` to be `esm`. | | ||
@@ -65,5 +66,8 @@ ### Programmatic | ||
// `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. | ||
const moduleWithType = jsonSchemaToZod(myObject, { name: "mySchema", module: "esm", type: true }); | ||
const cjs = jsonSchemaToZod(myObject, { module: "cjs", name: "mySchema" }); | ||
const schema = jsonSchemaToZod(myObject); | ||
const justTheSchema = jsonSchemaToZod(myObject); | ||
``` | ||
@@ -79,2 +83,12 @@ | ||
##### `moduleWithType` | ||
```typescript | ||
import { z } from "zod"; | ||
export const mySchema = z.object({ hello: z.string().optional() }); | ||
export type MySchema = z.infer<typeof mySchema>; | ||
``` | ||
##### `cjs` | ||
@@ -88,3 +102,3 @@ | ||
##### `schema` | ||
##### `justTheSchema` | ||
@@ -91,0 +105,0 @@ ```typescript |
102295
2574
137