json-schema-to-zod
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -67,3 +67,3 @@ #!/usr/bin/env node | ||
let deref = process.argv.indexOf("--deref") !== -1 || process.argv.indexOf("-d") !== -1; | ||
let withoutDefaults = process.argv.indexOf("--without-defaults") !== -1; | ||
let withoutDefaults = process.argv.indexOf("--without-defaults") !== -1 && process.argv.indexOf("-wd") !== -1; | ||
if (targetFilePath) { | ||
@@ -70,0 +70,0 @@ if (deref) { |
{ | ||
"name": "json-schema-to-zod", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Converts JSON schema objects or files into Zod schemas", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
import { JSONSchema7, JSONSchema7Definition } from "json-schema"; | ||
export declare function parseAllOf(schema: JSONSchema7 & { | ||
allOf: JSONSchema7Definition[]; | ||
}, withoutDefaults: boolean): string; | ||
}, withoutDefaults?: boolean): string; |
import { JSONSchema7, JSONSchema7Definition } from "json-schema"; | ||
export declare const parseAnyOf: (schema: JSONSchema7 & { | ||
anyOf: JSONSchema7Definition[]; | ||
}, withoutDefaults: boolean) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
import { JSONSchema7 } from "json-schema"; | ||
export declare const parseArray: (schema: JSONSchema7 & { | ||
type: "array"; | ||
}, withoutDefaults: boolean) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
@@ -9,3 +9,3 @@ "use strict"; | ||
: Array.isArray(schema.items) | ||
? `z.tuple([${schema.items.map(v => (0, parseSchema_1.parseSchema)(v, withoutDefaults))}])` | ||
? `z.tuple([${schema.items.map((v) => (0, parseSchema_1.parseSchema)(v, withoutDefaults))}])` | ||
: `z.array(${(0, parseSchema_1.parseSchema)(schema.items, withoutDefaults)})`; | ||
@@ -12,0 +12,0 @@ if (typeof schema.minItems === "number") |
@@ -6,6 +6,7 @@ "use strict"; | ||
if (Array.isArray(schema.enum)) { | ||
if (schema.enum.every((x) => typeof x === 'string')) { | ||
if (schema.enum.every((x) => typeof x === "string")) { | ||
return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`; | ||
} | ||
else if (schema.enum.length === 1) { // union does not work when there is only one element | ||
else if (schema.enum.length === 1) { | ||
// union does not work when there is only one element | ||
return `z.literal(${JSON.stringify(schema.enum[0])})`; | ||
@@ -12,0 +13,0 @@ } |
@@ -6,2 +6,2 @@ import { JSONSchema7, JSONSchema7Definition } from "json-schema"; | ||
else: JSONSchema7Definition; | ||
}, withoutDefaults: boolean) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
import { JSONSchema7, JSONSchema7TypeName } from "json-schema"; | ||
export declare const parseMultipleType: (schema: JSONSchema7 & { | ||
type: JSONSchema7TypeName[]; | ||
}, withoutDefaults: boolean) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
import { JSONSchema7, JSONSchema7Definition } from "json-schema"; | ||
export declare const parseNot: (schema: JSONSchema7 & { | ||
not: JSONSchema7Definition; | ||
}) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseNot = void 0; | ||
const parseNot = (schema) => { | ||
return "z.undefined()"; | ||
const parseSchema_1 = require("./parseSchema"); | ||
const parseNot = (schema, withoutDefaults) => { | ||
return `z.any().refine((value) => !${(0, parseSchema_1.parseSchema)(schema.not, withoutDefaults)}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`; | ||
}; | ||
exports.parseNot = parseNot; |
@@ -7,2 +7,2 @@ import { JSONSchema7 } from "json-schema"; | ||
nullable: true; | ||
}, includeDefaults: boolean) => string; | ||
}, includeDefaults?: boolean | undefined) => string; |
import { JSONSchema7 } from "json-schema"; | ||
export declare const parseObject: (schema: JSONSchema7 & { | ||
type: "object"; | ||
}, withoutDefaults: boolean) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
@@ -19,3 +19,6 @@ "use strict"; | ||
var _a; | ||
return `${JSON.stringify(k)}:${(0, parseSchema_1.parseSchema)(v, withoutDefaults)}${((_a = schema.required) === null || _a === void 0 ? void 0 : _a.includes(k)) || (!withoutDefaults && v.hasOwnProperty('default')) ? requiredFlag : ".optional()"}`; | ||
return `${JSON.stringify(k)}:${(0, parseSchema_1.parseSchema)(v, withoutDefaults)}${((_a = schema.required) === null || _a === void 0 ? void 0 : _a.includes(k)) || | ||
(!withoutDefaults && v.hasOwnProperty("default")) | ||
? requiredFlag | ||
: ".optional()"}`; | ||
})}})${schema.additionalProperties === true | ||
@@ -22,0 +25,0 @@ ? ".catchall(z.any())" |
import { JSONSchema7, JSONSchema7Definition } from "json-schema"; | ||
export declare const parseOneOf: (schema: JSONSchema7 & { | ||
oneOf: JSONSchema7Definition[]; | ||
}, withoutDefaults: boolean) => string; | ||
}, withoutDefaults?: boolean | undefined) => string; |
@@ -10,3 +10,3 @@ "use strict"; | ||
: `z.any().superRefine((x, ctx) => { | ||
const schemas = [${schema.oneOf.map(schema => (0, parseSchema_1.parseSchema)(schema, withoutDefaults))}]; | ||
const schemas = [${schema.oneOf.map((schema) => (0, parseSchema_1.parseSchema)(schema, withoutDefaults))}]; | ||
const errors = schemas.reduce( | ||
@@ -13,0 +13,0 @@ (errors: z.ZodError[], schema) => |
import { JSONSchema7, JSONSchema7Definition, JSONSchema7Type, JSONSchema7TypeName } from "json-schema"; | ||
export declare const parseSchema: (schema: JSONSchema7 | boolean, withoutDefaults: boolean) => string; | ||
export declare const parseSchema: (schema: JSONSchema7 | boolean, withoutDefaults?: boolean | undefined) => string; | ||
export declare const its: { | ||
@@ -4,0 +4,0 @@ an: { |
@@ -37,3 +37,3 @@ "use strict"; | ||
const addDefaults = (schema, parsed) => { | ||
if (schema.default) { | ||
if (schema.default !== undefined) { | ||
parsed += `.default(${JSON.stringify(schema.default)} )`; | ||
@@ -63,3 +63,3 @@ } | ||
else if (exports.its.a.not(schema)) { | ||
return (0, parseNot_1.parseNot)(schema); | ||
return (0, parseNot_1.parseNot)(schema, withoutDefaults); | ||
} | ||
@@ -66,0 +66,0 @@ else if (exports.its.an.enum(schema)) { |
# Json-Schema-to-Zod | ||
[![NPM Version](https://img.shields.io/npm/v/json-schema-to-zod.svg)](https://npmjs.org/package/json-schema-to-zod) | ||
[![NPM Downloads](https://img.shields.io/npm/dw/json-schema-to-zod.svg)](https://npmjs.org/package/json-schema-to-zod) | ||
_Looking for the exact opposite? Check out [zod-to-json-schema](https://npmjs.org/package/zod-to-json-schema)_ | ||
## Summary | ||
A very simple CLI tool to convert JSON schema (draft 4+) objects or files into Zod schemas. Uses Prettier for formatting for now. | ||
A runtime package and CLI tool to convert JSON schema (draft 4+) objects or files into Zod schemas in the form of JavaScript code. Uses Prettier for formatting, . | ||
@@ -13,20 +16,34 @@ ## Usage | ||
[Paste your schemas here](https://stefanterdell.github.io/json-schema-to-zod-react/) | ||
[Just paste your JSON schemas here!](https://stefanterdell.github.io/json-schema-to-zod-react/) | ||
### CLI | ||
`json-schema-to-zod -s myJson.json -t mySchema.ts` | ||
Installation: | ||
Options: | ||
> `npm i -G json-schema-to-zod` | ||
- --source/-s [source file name] | ||
- --target/-t [(optional) target file name] | ||
- --name/-n [(optional) schema name in output] | ||
- --deref/-d [(optional) deref schemas before parsing] | ||
- --without-defaults [(optional) exclude default values] | ||
Example: | ||
> `json-schema-to-zod -s myJson.json -t mySchema.ts` | ||
#### Options | ||
| Flag | Shorthand | Function | | ||
| -------------------- | --------- | ------------------------------------------------------- | | ||
| `--source` | `-s` | Source file name (required) | | ||
| `--target` | `-t` | Target file name | | ||
| `--name` | `-n` | The name of the schema in the output | | ||
| `--deref` | `-d` | Uses `json-schema-ref-parser` to dereference the schema | | ||
| `--without-defaults` | `-wd` | Ignore default values in the schema | | ||
### Programmatic | ||
`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 awaiting `jsonSchemaDereffed`. | ||
```typescript | ||
import { jsonSchemaToZod, parseSchema } from "json-schema-to-zod"; | ||
import { | ||
jsonSchemaToZod, | ||
jsonSchemaToZodDereffed, | ||
parseSchema, | ||
} from "json-schema-to-zod"; | ||
@@ -42,12 +59,12 @@ const myObject = { | ||
const result = jsonSchemaToZod(myObject); | ||
console.log(result); | ||
const module = jsonSchemaToZod(myObject); | ||
const zodSchema = parseSchema(myObject, false); | ||
console.log(zodSchema); | ||
const dereffed = await jsonSchemaToZodDereffed(myObject); | ||
const schema = parseSchema(myObject); | ||
``` | ||
### Expected output: | ||
`module`/`dereffed` = | ||
``` | ||
```typescript | ||
import { z } from "zod"; | ||
@@ -58,6 +75,6 @@ | ||
and | ||
`schema` = | ||
```typescript | ||
z.object({ hello: z.string().optional() }); | ||
``` | ||
z.object({hello: z.string().optional()}) | ||
``` |
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
32737
691
78