json-schema-to-zod
Advanced tools
Comparing version 2.5.0 to 2.6.0
@@ -37,3 +37,3 @@ #!/usr/bin/env node | ||
type: { | ||
shortHand: "t", | ||
shorthand: "t", | ||
value: "string", | ||
@@ -43,5 +43,9 @@ description: "The name of the (optional) inferred type export." | ||
noImport: { | ||
shortHand: "ni", | ||
shorthand: "ni", | ||
description: "Removes the `import { z } from 'zod';` or equivalent from the output." | ||
}, | ||
withJsdocs: { | ||
shorthand: "wj", | ||
description: "Generate jsdocs off of the description property.", | ||
}, | ||
}; | ||
@@ -57,3 +61,4 @@ async function main() { | ||
noImport: args.noImport, | ||
type: args.type | ||
type: args.type, | ||
withJsdocs: args.withJsdocs, | ||
}); | ||
@@ -60,0 +65,0 @@ if (args.output) { |
@@ -37,2 +37,3 @@ "use strict"; | ||
__exportStar(require("./utils/half.js"), exports); | ||
__exportStar(require("./utils/jsdocs.js"), exports); | ||
__exportStar(require("./utils/omit.js"), exports); | ||
@@ -39,0 +40,0 @@ __exportStar(require("./utils/withMessage.js"), exports); |
@@ -5,2 +5,3 @@ "use strict"; | ||
const parseSchema_js_1 = require("./parsers/parseSchema.js"); | ||
const jsdocs_js_1 = require("./utils/jsdocs.js"); | ||
const jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest } = {}) => { | ||
@@ -17,7 +18,10 @@ if (type && (!name || module !== "esm")) { | ||
}); | ||
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description | ||
? (0, jsdocs_js_1.expandJsdocs)(schema.description) | ||
: ""; | ||
if (module === "cjs") { | ||
result = `module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result} | ||
result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result} | ||
`; | ||
if (!noImport) { | ||
result = `const { z } = require("zod") | ||
result = `${jsdocs}const { z } = require("zod") | ||
@@ -28,3 +32,3 @@ ${result}`; | ||
else if (module === "esm") { | ||
result = `export ${name ? `const ${name} =` : `default`} ${result} | ||
result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result} | ||
`; | ||
@@ -38,3 +42,3 @@ if (!noImport) { | ||
else if (name) { | ||
result = `const ${name} = ${result}`; | ||
result = `${jsdocs}const ${name} = ${result}`; | ||
} | ||
@@ -41,0 +45,0 @@ if (type && name) { |
@@ -8,2 +8,3 @@ "use strict"; | ||
const parseAllOf_js_1 = require("./parseAllOf.js"); | ||
const jsdocs_js_1 = require("../utils/jsdocs.js"); | ||
function parseObject(objectSchema, refs) { | ||
@@ -20,6 +21,9 @@ let properties = undefined; | ||
const propSchema = objectSchema.properties[key]; | ||
const result = `${JSON.stringify(key)}: ${(0, parseSchema_js_1.parseSchema)(propSchema, { | ||
let result = `${JSON.stringify(key)}: ${(0, parseSchema_js_1.parseSchema)(propSchema, { | ||
...refs, | ||
path: [...refs.path, "properties", key], | ||
})}`; | ||
if (refs.withJsdocs && typeof propSchema === "object") { | ||
result = (0, jsdocs_js_1.addJsdocs)(propSchema, result); | ||
} | ||
const hasDefault = typeof propSchema === "object" && propSchema.default !== undefined; | ||
@@ -26,0 +30,0 @@ const required = Array.isArray(objectSchema.required) |
@@ -35,3 +35,3 @@ #!/usr/bin/env node | ||
type: { | ||
shortHand: "t", | ||
shorthand: "t", | ||
value: "string", | ||
@@ -41,5 +41,9 @@ description: "The name of the (optional) inferred type export." | ||
noImport: { | ||
shortHand: "ni", | ||
shorthand: "ni", | ||
description: "Removes the `import { z } from 'zod';` or equivalent from the output." | ||
}, | ||
withJsdocs: { | ||
shorthand: "wj", | ||
description: "Generate jsdocs off of the description property.", | ||
}, | ||
}; | ||
@@ -55,3 +59,4 @@ async function main() { | ||
noImport: args.noImport, | ||
type: args.type | ||
type: args.type, | ||
withJsdocs: args.withJsdocs, | ||
}); | ||
@@ -58,0 +63,0 @@ if (args.output) { |
@@ -21,2 +21,3 @@ export * from "./Types.js"; | ||
export * from "./utils/half.js"; | ||
export * from "./utils/jsdocs.js"; | ||
export * from "./utils/omit.js"; | ||
@@ -23,0 +24,0 @@ export * from "./utils/withMessage.js"; |
import { parseSchema } from "./parsers/parseSchema.js"; | ||
import { expandJsdocs } from "./utils/jsdocs.js"; | ||
export const jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest } = {}) => { | ||
@@ -13,7 +14,10 @@ if (type && (!name || module !== "esm")) { | ||
}); | ||
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description | ||
? expandJsdocs(schema.description) | ||
: ""; | ||
if (module === "cjs") { | ||
result = `module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result} | ||
result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result} | ||
`; | ||
if (!noImport) { | ||
result = `const { z } = require("zod") | ||
result = `${jsdocs}const { z } = require("zod") | ||
@@ -24,3 +28,3 @@ ${result}`; | ||
else if (module === "esm") { | ||
result = `export ${name ? `const ${name} =` : `default`} ${result} | ||
result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result} | ||
`; | ||
@@ -34,3 +38,3 @@ if (!noImport) { | ||
else if (name) { | ||
result = `const ${name} = ${result}`; | ||
result = `${jsdocs}const ${name} = ${result}`; | ||
} | ||
@@ -37,0 +41,0 @@ if (type && name) { |
@@ -5,2 +5,3 @@ import { parseAnyOf } from "./parseAnyOf.js"; | ||
import { parseAllOf } from "./parseAllOf.js"; | ||
import { addJsdocs } from "../utils/jsdocs.js"; | ||
export function parseObject(objectSchema, refs) { | ||
@@ -17,6 +18,9 @@ let properties = undefined; | ||
const propSchema = objectSchema.properties[key]; | ||
const result = `${JSON.stringify(key)}: ${parseSchema(propSchema, { | ||
let result = `${JSON.stringify(key)}: ${parseSchema(propSchema, { | ||
...refs, | ||
path: [...refs.path, "properties", key], | ||
})}`; | ||
if (refs.withJsdocs && typeof propSchema === "object") { | ||
result = addJsdocs(propSchema, result); | ||
} | ||
const hasDefault = typeof propSchema === "object" && propSchema.default !== undefined; | ||
@@ -23,0 +27,0 @@ const required = Array.isArray(objectSchema.required) |
@@ -21,2 +21,3 @@ export * from "./Types.js"; | ||
export * from "./utils/half.js"; | ||
export * from "./utils/jsdocs.js"; | ||
export * from "./utils/omit.js"; | ||
@@ -23,0 +24,0 @@ export * from "./utils/withMessage.js"; |
@@ -54,2 +54,3 @@ export type Serializable = { | ||
withoutDescribes?: boolean; | ||
withJsdocs?: boolean; | ||
parserOverride?: ParserOverride; | ||
@@ -56,0 +57,0 @@ depth?: number; |
{ | ||
"name": "json-schema-to-zod", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Converts JSON schema objects or files into Zod schemas", | ||
@@ -59,3 +59,4 @@ "types": "./dist/types/index.d.ts", | ||
"Brett Zamir (https://github.com/brettz9)", | ||
"vForgeOne (https://github.com/vforgeone)" | ||
"vForgeOne (https://github.com/vforgeone)", | ||
"Adrian Ordonez (https://github.com/adrianord)" | ||
], | ||
@@ -62,0 +63,0 @@ "license": "ISC", |
@@ -46,11 +46,12 @@ # Json-Schema-to-Zod | ||
| 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. | | ||
| 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. | | ||
@@ -57,0 +58,0 @@ ### Programmatic |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15187840
123
2732
146