json-schema-to-zod
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -15,3 +15,6 @@ "use strict"; | ||
? defaults | ||
: Object.assign(Object.assign({}, defaults), help); | ||
: { | ||
...defaults, | ||
...help, | ||
}; | ||
let index = args.indexOf("--" + param.name); | ||
@@ -66,5 +69,4 @@ if (index === -1) { | ||
function parseOrReadJSON(jsonOrPath) { | ||
var _a; | ||
jsonOrPath = jsonOrPath.trim(); | ||
if ((_a = (0, fs_1.statSync)(jsonOrPath, { throwIfNoEntry: false })) === null || _a === void 0 ? void 0 : _a.isFile()) { | ||
if ((0, fs_1.statSync)(jsonOrPath, { throwIfNoEntry: false })?.isFile()) { | ||
jsonOrPath = (0, fs_1.readFileSync)(jsonOrPath, "utf-8"); | ||
@@ -92,3 +94,2 @@ } | ||
function printParams(params) { | ||
var _a; | ||
const longest = params.reduce((n, p) => (p.name.length > n ? p.name.length : n), 5); | ||
@@ -98,3 +99,3 @@ const header = "Name " + " ".repeat(longest - 2) + "Short Description"; | ||
for (const param of params) { | ||
console.log((_a = "--" + | ||
console.log("--" + | ||
param.name + | ||
@@ -105,5 +106,5 @@ " ".repeat(longest - param.name.length) + | ||
" " + | ||
param.description) !== null && _a !== void 0 ? _a : ""); | ||
param.description ?? ""); | ||
} | ||
} | ||
exports.printParams = printParams; |
#!/usr/bin/env node | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -51,22 +42,19 @@ const jsonSchemaToZod_js_1 = require("./jsonSchemaToZod.js"); | ||
]; | ||
function main() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const args = (0, args_js_1.parseArgs)(params, process.argv, {}); | ||
const input = (_a = args.input) !== null && _a !== void 0 ? _a : (yield (0, args_js_1.readPipe)()); | ||
const jsonSchema = (0, args_js_1.parseOrReadJSON)(input); | ||
const zodSchema = (0, jsonSchemaToZod_js_1.jsonSchemaToZod)(jsonSchema, { | ||
module: args.module, | ||
name: args.name, | ||
recursionDepth: args["recursion-depth"], | ||
}); | ||
if (args.output) { | ||
(0, fs_1.mkdirSync)((0, path_1.dirname)(args.output), { recursive: true }); | ||
(0, fs_1.writeFileSync)(args.output, zodSchema); | ||
} | ||
else { | ||
console.log(zodSchema); | ||
} | ||
async function main() { | ||
const args = (0, args_js_1.parseArgs)(params, process.argv, {}); | ||
const input = args.input ?? (await (0, args_js_1.readPipe)()); | ||
const jsonSchema = (0, args_js_1.parseOrReadJSON)(input); | ||
const zodSchema = (0, jsonSchemaToZod_js_1.jsonSchemaToZod)(jsonSchema, { | ||
module: args.module, | ||
name: args.name, | ||
recursionDepth: args["recursion-depth"], | ||
}); | ||
if (args.output) { | ||
(0, fs_1.mkdirSync)((0, path_1.dirname)(args.output), { recursive: true }); | ||
(0, fs_1.writeFileSync)(args.output, zodSchema); | ||
} | ||
else { | ||
console.log(zodSchema); | ||
} | ||
} | ||
void main(); |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.jsonSchemaToZod = void 0; | ||
const parseSchema_js_1 = require("./parsers/parseSchema.js"); | ||
const jsonSchemaToZod = (schema, _a = {}) => { | ||
var { module = true, name } = _a, rest = __rest(_a, ["module", "name"]); | ||
let result = (0, parseSchema_js_1.parseSchema)(schema, Object.assign({ module, | ||
name, path: [], seen: new Map() }, rest)); | ||
const jsonSchemaToZod = (schema, { module = true, name, ...rest } = {}) => { | ||
let result = (0, parseSchema_js_1.parseSchema)(schema, { | ||
module, | ||
name, | ||
path: [], | ||
seen: new Map(), | ||
...rest, | ||
}); | ||
if (module) { | ||
@@ -21,0 +14,0 @@ if (module === "cjs") { |
@@ -18,3 +18,3 @@ "use strict"; | ||
else { | ||
newArr.push(Object.assign(Object.assign({}, item), { [originalIndex]: i })); | ||
newArr.push({ ...item, [originalIndex]: i }); | ||
} | ||
@@ -30,3 +30,6 @@ } | ||
const item = schema.allOf[0]; | ||
return (0, parseSchema_js_1.parseSchema)(item, Object.assign(Object.assign({}, refs), { path: [...refs.path, "allOf", item[originalIndex]] })); | ||
return (0, parseSchema_js_1.parseSchema)(item, { | ||
...refs, | ||
path: [...refs.path, "allOf", item[originalIndex]], | ||
}); | ||
} | ||
@@ -33,0 +36,0 @@ else { |
@@ -8,5 +8,8 @@ "use strict"; | ||
? schema.anyOf.length === 1 | ||
? (0, parseSchema_js_1.parseSchema)(schema.anyOf[0], Object.assign(Object.assign({}, refs), { path: [...refs.path, "anyOf", 0] })) | ||
? (0, parseSchema_js_1.parseSchema)(schema.anyOf[0], { | ||
...refs, | ||
path: [...refs.path, "anyOf", 0], | ||
}) | ||
: `z.union([${schema.anyOf | ||
.map((schema, i) => (0, parseSchema_js_1.parseSchema)(schema, Object.assign(Object.assign({}, refs), { path: [...refs.path, "anyOf", i] }))) | ||
.map((schema, i) => (0, parseSchema_js_1.parseSchema)(schema, { ...refs, path: [...refs.path, "anyOf", i] })) | ||
.join(", ")}])` | ||
@@ -13,0 +16,0 @@ : `z.any()`; |
@@ -8,7 +8,10 @@ "use strict"; | ||
if (Array.isArray(schema.items)) { | ||
return `z.tuple([${schema.items.map((v, i) => (0, parseSchema_js_1.parseSchema)(v, Object.assign(Object.assign({}, refs), { path: [...refs.path, "items", i] })))}])`; | ||
return `z.tuple([${schema.items.map((v, i) => (0, parseSchema_js_1.parseSchema)(v, { ...refs, path: [...refs.path, "items", i] }))}])`; | ||
} | ||
let r = !schema.items | ||
? "z.array(z.any())" | ||
: `z.array(${(0, parseSchema_js_1.parseSchema)(schema.items, Object.assign(Object.assign({}, refs), { path: [...refs.path, "items"] }))})`; | ||
: `z.array(${(0, parseSchema_js_1.parseSchema)(schema.items, { | ||
...refs, | ||
path: [...refs.path, "items"], | ||
})})`; | ||
r += (0, withMessage_js_1.withMessage)(schema, "minItems", ({ json }) => [ | ||
@@ -15,0 +18,0 @@ `.min(${json}`, |
@@ -6,5 +6,11 @@ "use strict"; | ||
const parseIfThenElse = (schema, refs) => { | ||
const $if = (0, parseSchema_js_1.parseSchema)(schema.if, Object.assign(Object.assign({}, refs), { path: [...refs.path, "if"] })); | ||
const $then = (0, parseSchema_js_1.parseSchema)(schema.then, Object.assign(Object.assign({}, refs), { path: [...refs.path, "then"] })); | ||
const $else = (0, parseSchema_js_1.parseSchema)(schema.else, Object.assign(Object.assign({}, refs), { path: [...refs.path, "else"] })); | ||
const $if = (0, parseSchema_js_1.parseSchema)(schema.if, { ...refs, path: [...refs.path, "if"] }); | ||
const $then = (0, parseSchema_js_1.parseSchema)(schema.then, { | ||
...refs, | ||
path: [...refs.path, "then"], | ||
}); | ||
const $else = (0, parseSchema_js_1.parseSchema)(schema.else, { | ||
...refs, | ||
path: [...refs.path, "else"], | ||
}); | ||
return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => { | ||
@@ -11,0 +17,0 @@ const result = ${$if}.safeParse(value).success |
@@ -7,5 +7,5 @@ "use strict"; | ||
return `z.union([${schema.type | ||
.map((type) => (0, parseSchema_js_1.parseSchema)(Object.assign(Object.assign({}, schema), { type }), refs)) | ||
.map((type) => (0, parseSchema_js_1.parseSchema)({ ...schema, type }, refs)) | ||
.join(", ")}])`; | ||
}; | ||
exports.parseMultipleType = parseMultipleType; |
@@ -6,4 +6,7 @@ "use strict"; | ||
const parseNot = (schema, refs) => { | ||
return `z.any().refine((value) => !${(0, parseSchema_js_1.parseSchema)(schema.not, Object.assign(Object.assign({}, refs), { path: [...refs.path, "not"] }))}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`; | ||
return `z.any().refine((value) => !${(0, parseSchema_js_1.parseSchema)(schema.not, { | ||
...refs, | ||
path: [...refs.path, "not"], | ||
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`; | ||
}; | ||
exports.parseNot = parseNot; |
@@ -19,3 +19,6 @@ "use strict"; | ||
const propSchema = objectSchema.properties[key]; | ||
const result = `${JSON.stringify(key)}: ${(0, parseSchema_js_1.parseSchema)(propSchema, Object.assign(Object.assign({}, refs), { path: [...refs.path, "properties", key] }))}`; | ||
const result = `${JSON.stringify(key)}: ${(0, parseSchema_js_1.parseSchema)(propSchema, { | ||
...refs, | ||
path: [...refs.path, "properties", key], | ||
})}`; | ||
const hasDefault = (typeof propSchema === "object" && | ||
@@ -37,3 +40,6 @@ propSchema.default !== undefined) || | ||
const additionalProperties = objectSchema.additionalProperties !== undefined | ||
? (0, parseSchema_js_1.parseSchema)(objectSchema.additionalProperties, Object.assign(Object.assign({}, refs), { path: [...refs.path, "additionalProperties"] })) | ||
? (0, parseSchema_js_1.parseSchema)(objectSchema.additionalProperties, { | ||
...refs, | ||
path: [...refs.path, "additionalProperties"], | ||
}) | ||
: undefined; | ||
@@ -45,3 +51,6 @@ let patternProperties = undefined; | ||
key, | ||
(0, parseSchema_js_1.parseSchema)(value, Object.assign(Object.assign({}, refs), { path: [...refs.path, "patternProperties", key] })), | ||
(0, parseSchema_js_1.parseSchema)(value, { | ||
...refs, | ||
path: [...refs.path, "patternProperties", key], | ||
}), | ||
]; | ||
@@ -145,18 +154,30 @@ }, {})); | ||
if (parseSchema_js_1.its.an.anyOf(objectSchema)) { | ||
output += `.and(${(0, parseAnyOf_js_1.parseAnyOf)(Object.assign(Object.assign({}, objectSchema), { anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && | ||
output += `.and(${(0, parseAnyOf_js_1.parseAnyOf)({ | ||
...objectSchema, | ||
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && | ||
!x.type && | ||
(x.properties || x.additionalProperties || x.patternProperties) | ||
? Object.assign(Object.assign({}, x), { type: "object" }) : x) }), refs)})`; | ||
? { ...x, type: "object" } | ||
: x), | ||
}, refs)})`; | ||
} | ||
if (parseSchema_js_1.its.a.oneOf(objectSchema)) { | ||
output += `.and(${(0, parseOneOf_js_1.parseOneOf)(Object.assign(Object.assign({}, objectSchema), { oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && | ||
output += `.and(${(0, parseOneOf_js_1.parseOneOf)({ | ||
...objectSchema, | ||
oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && | ||
!x.type && | ||
(x.properties || x.additionalProperties || x.patternProperties) | ||
? Object.assign(Object.assign({}, x), { type: "object" }) : x) }), refs)})`; | ||
? { ...x, type: "object" } | ||
: x), | ||
}, refs)})`; | ||
} | ||
if (parseSchema_js_1.its.an.allOf(objectSchema)) { | ||
output += `.and(${(0, parseAllOf_js_1.parseAllOf)(Object.assign(Object.assign({}, objectSchema), { allOf: objectSchema.allOf.map((x) => typeof x === "object" && | ||
output += `.and(${(0, parseAllOf_js_1.parseAllOf)({ | ||
...objectSchema, | ||
allOf: objectSchema.allOf.map((x) => typeof x === "object" && | ||
!x.type && | ||
(x.properties || x.additionalProperties || x.patternProperties) | ||
? Object.assign(Object.assign({}, x), { type: "object" }) : x) }), refs)})`; | ||
? { ...x, type: "object" } | ||
: x), | ||
}, refs)})`; | ||
} | ||
@@ -163,0 +184,0 @@ return output; |
@@ -8,6 +8,12 @@ "use strict"; | ||
? schema.oneOf.length === 1 | ||
? (0, parseSchema_js_1.parseSchema)(schema.oneOf[0], Object.assign(Object.assign({}, refs), { path: [...refs.path, "oneOf", 0] })) | ||
? (0, parseSchema_js_1.parseSchema)(schema.oneOf[0], { | ||
...refs, | ||
path: [...refs.path, "oneOf", 0], | ||
}) | ||
: `z.any().superRefine((x, ctx) => { | ||
const schemas = [${schema.oneOf | ||
.map((schema, i) => (0, parseSchema_js_1.parseSchema)(schema, Object.assign(Object.assign({}, refs), { path: [...refs.path, "oneOf", i] }))) | ||
.map((schema, i) => (0, parseSchema_js_1.parseSchema)(schema, { | ||
...refs, | ||
path: [...refs.path, "oneOf", i], | ||
})) | ||
.join(", ")}]; | ||
@@ -14,0 +20,0 @@ const errors = schemas.reduce( |
@@ -5,3 +5,2 @@ "use strict"; | ||
function withMessage(schema, key, get) { | ||
var _a; | ||
const value = schema[key]; | ||
@@ -16,3 +15,3 @@ let r = ""; | ||
r += opener; | ||
if (((_a = schema.errorMessage) === null || _a === void 0 ? void 0 : _a[key]) !== undefined) { | ||
if (schema.errorMessage?.[key] !== undefined) { | ||
r += prefix + JSON.stringify(schema.errorMessage[key]); | ||
@@ -19,0 +18,0 @@ } |
@@ -12,3 +12,6 @@ import { statSync, readFileSync } from "fs"; | ||
? defaults | ||
: Object.assign(Object.assign({}, defaults), help); | ||
: { | ||
...defaults, | ||
...help, | ||
}; | ||
let index = args.indexOf("--" + param.name); | ||
@@ -62,5 +65,4 @@ if (index === -1) { | ||
export function parseOrReadJSON(jsonOrPath) { | ||
var _a; | ||
jsonOrPath = jsonOrPath.trim(); | ||
if ((_a = statSync(jsonOrPath, { throwIfNoEntry: false })) === null || _a === void 0 ? void 0 : _a.isFile()) { | ||
if (statSync(jsonOrPath, { throwIfNoEntry: false })?.isFile()) { | ||
jsonOrPath = readFileSync(jsonOrPath, "utf-8"); | ||
@@ -86,3 +88,2 @@ } | ||
export function printParams(params) { | ||
var _a; | ||
const longest = params.reduce((n, p) => (p.name.length > n ? p.name.length : n), 5); | ||
@@ -92,3 +93,3 @@ const header = "Name " + " ".repeat(longest - 2) + "Short Description"; | ||
for (const param of params) { | ||
console.log((_a = "--" + | ||
console.log("--" + | ||
param.name + | ||
@@ -99,4 +100,4 @@ " ".repeat(longest - param.name.length) + | ||
" " + | ||
param.description) !== null && _a !== void 0 ? _a : ""); | ||
param.description ?? ""); | ||
} | ||
} |
#!/usr/bin/env node | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { jsonSchemaToZod } from "./jsonSchemaToZod.js"; | ||
@@ -49,22 +40,19 @@ import { writeFileSync, mkdirSync } from "fs"; | ||
]; | ||
function main() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const args = parseArgs(params, process.argv, {}); | ||
const input = (_a = args.input) !== null && _a !== void 0 ? _a : (yield readPipe()); | ||
const jsonSchema = parseOrReadJSON(input); | ||
const zodSchema = jsonSchemaToZod(jsonSchema, { | ||
module: args.module, | ||
name: args.name, | ||
recursionDepth: args["recursion-depth"], | ||
}); | ||
if (args.output) { | ||
mkdirSync(dirname(args.output), { recursive: true }); | ||
writeFileSync(args.output, zodSchema); | ||
} | ||
else { | ||
console.log(zodSchema); | ||
} | ||
async function main() { | ||
const args = parseArgs(params, process.argv, {}); | ||
const input = args.input ?? (await readPipe()); | ||
const jsonSchema = parseOrReadJSON(input); | ||
const zodSchema = jsonSchemaToZod(jsonSchema, { | ||
module: args.module, | ||
name: args.name, | ||
recursionDepth: args["recursion-depth"], | ||
}); | ||
if (args.output) { | ||
mkdirSync(dirname(args.output), { recursive: true }); | ||
writeFileSync(args.output, zodSchema); | ||
} | ||
else { | ||
console.log(zodSchema); | ||
} | ||
} | ||
void main(); |
@@ -1,17 +0,10 @@ | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
import { parseSchema } from "./parsers/parseSchema.js"; | ||
export const jsonSchemaToZod = (schema, _a = {}) => { | ||
var { module = true, name } = _a, rest = __rest(_a, ["module", "name"]); | ||
let result = parseSchema(schema, Object.assign({ module, | ||
name, path: [], seen: new Map() }, rest)); | ||
export const jsonSchemaToZod = (schema, { module = true, name, ...rest } = {}) => { | ||
let result = parseSchema(schema, { | ||
module, | ||
name, | ||
path: [], | ||
seen: new Map(), | ||
...rest, | ||
}); | ||
if (module) { | ||
@@ -18,0 +11,0 @@ if (module === "cjs") { |
@@ -15,3 +15,3 @@ import { parseSchema } from "./parseSchema.js"; | ||
else { | ||
newArr.push(Object.assign(Object.assign({}, item), { [originalIndex]: i })); | ||
newArr.push({ ...item, [originalIndex]: i }); | ||
} | ||
@@ -27,3 +27,6 @@ } | ||
const item = schema.allOf[0]; | ||
return parseSchema(item, Object.assign(Object.assign({}, refs), { path: [...refs.path, "allOf", item[originalIndex]] })); | ||
return parseSchema(item, { | ||
...refs, | ||
path: [...refs.path, "allOf", item[originalIndex]], | ||
}); | ||
} | ||
@@ -30,0 +33,0 @@ else { |
@@ -5,7 +5,10 @@ import { parseSchema } from "./parseSchema.js"; | ||
? schema.anyOf.length === 1 | ||
? parseSchema(schema.anyOf[0], Object.assign(Object.assign({}, refs), { path: [...refs.path, "anyOf", 0] })) | ||
? parseSchema(schema.anyOf[0], { | ||
...refs, | ||
path: [...refs.path, "anyOf", 0], | ||
}) | ||
: `z.union([${schema.anyOf | ||
.map((schema, i) => parseSchema(schema, Object.assign(Object.assign({}, refs), { path: [...refs.path, "anyOf", i] }))) | ||
.map((schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] })) | ||
.join(", ")}])` | ||
: `z.any()`; | ||
}; |
@@ -5,7 +5,10 @@ import { withMessage } from "../utils/withMessage.js"; | ||
if (Array.isArray(schema.items)) { | ||
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, Object.assign(Object.assign({}, refs), { path: [...refs.path, "items", i] })))}])`; | ||
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`; | ||
} | ||
let r = !schema.items | ||
? "z.array(z.any())" | ||
: `z.array(${parseSchema(schema.items, Object.assign(Object.assign({}, refs), { path: [...refs.path, "items"] }))})`; | ||
: `z.array(${parseSchema(schema.items, { | ||
...refs, | ||
path: [...refs.path, "items"], | ||
})})`; | ||
r += withMessage(schema, "minItems", ({ json }) => [ | ||
@@ -12,0 +15,0 @@ `.min(${json}`, |
import { parseSchema } from "./parseSchema.js"; | ||
export const parseIfThenElse = (schema, refs) => { | ||
const $if = parseSchema(schema.if, Object.assign(Object.assign({}, refs), { path: [...refs.path, "if"] })); | ||
const $then = parseSchema(schema.then, Object.assign(Object.assign({}, refs), { path: [...refs.path, "then"] })); | ||
const $else = parseSchema(schema.else, Object.assign(Object.assign({}, refs), { path: [...refs.path, "else"] })); | ||
const $if = parseSchema(schema.if, { ...refs, path: [...refs.path, "if"] }); | ||
const $then = parseSchema(schema.then, { | ||
...refs, | ||
path: [...refs.path, "then"], | ||
}); | ||
const $else = parseSchema(schema.else, { | ||
...refs, | ||
path: [...refs.path, "else"], | ||
}); | ||
return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => { | ||
@@ -7,0 +13,0 @@ const result = ${$if}.safeParse(value).success |
import { parseSchema } from "./parseSchema.js"; | ||
export const parseMultipleType = (schema, refs) => { | ||
return `z.union([${schema.type | ||
.map((type) => parseSchema(Object.assign(Object.assign({}, schema), { type }), refs)) | ||
.map((type) => parseSchema({ ...schema, type }, refs)) | ||
.join(", ")}])`; | ||
}; |
import { parseSchema } from "./parseSchema.js"; | ||
export const parseNot = (schema, refs) => { | ||
return `z.any().refine((value) => !${parseSchema(schema.not, Object.assign(Object.assign({}, refs), { path: [...refs.path, "not"] }))}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`; | ||
return `z.any().refine((value) => !${parseSchema(schema.not, { | ||
...refs, | ||
path: [...refs.path, "not"], | ||
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`; | ||
}; |
@@ -16,3 +16,6 @@ import { parseAnyOf } from "./parseAnyOf.js"; | ||
const propSchema = objectSchema.properties[key]; | ||
const result = `${JSON.stringify(key)}: ${parseSchema(propSchema, Object.assign(Object.assign({}, refs), { path: [...refs.path, "properties", key] }))}`; | ||
const result = `${JSON.stringify(key)}: ${parseSchema(propSchema, { | ||
...refs, | ||
path: [...refs.path, "properties", key], | ||
})}`; | ||
const hasDefault = (typeof propSchema === "object" && | ||
@@ -34,3 +37,6 @@ propSchema.default !== undefined) || | ||
const additionalProperties = objectSchema.additionalProperties !== undefined | ||
? parseSchema(objectSchema.additionalProperties, Object.assign(Object.assign({}, refs), { path: [...refs.path, "additionalProperties"] })) | ||
? parseSchema(objectSchema.additionalProperties, { | ||
...refs, | ||
path: [...refs.path, "additionalProperties"], | ||
}) | ||
: undefined; | ||
@@ -42,3 +48,6 @@ let patternProperties = undefined; | ||
key, | ||
parseSchema(value, Object.assign(Object.assign({}, refs), { path: [...refs.path, "patternProperties", key] })), | ||
parseSchema(value, { | ||
...refs, | ||
path: [...refs.path, "patternProperties", key], | ||
}), | ||
]; | ||
@@ -142,20 +151,32 @@ }, {})); | ||
if (its.an.anyOf(objectSchema)) { | ||
output += `.and(${parseAnyOf(Object.assign(Object.assign({}, objectSchema), { anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && | ||
output += `.and(${parseAnyOf({ | ||
...objectSchema, | ||
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && | ||
!x.type && | ||
(x.properties || x.additionalProperties || x.patternProperties) | ||
? Object.assign(Object.assign({}, x), { type: "object" }) : x) }), refs)})`; | ||
? { ...x, type: "object" } | ||
: x), | ||
}, refs)})`; | ||
} | ||
if (its.a.oneOf(objectSchema)) { | ||
output += `.and(${parseOneOf(Object.assign(Object.assign({}, objectSchema), { oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && | ||
output += `.and(${parseOneOf({ | ||
...objectSchema, | ||
oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && | ||
!x.type && | ||
(x.properties || x.additionalProperties || x.patternProperties) | ||
? Object.assign(Object.assign({}, x), { type: "object" }) : x) }), refs)})`; | ||
? { ...x, type: "object" } | ||
: x), | ||
}, refs)})`; | ||
} | ||
if (its.an.allOf(objectSchema)) { | ||
output += `.and(${parseAllOf(Object.assign(Object.assign({}, objectSchema), { allOf: objectSchema.allOf.map((x) => typeof x === "object" && | ||
output += `.and(${parseAllOf({ | ||
...objectSchema, | ||
allOf: objectSchema.allOf.map((x) => typeof x === "object" && | ||
!x.type && | ||
(x.properties || x.additionalProperties || x.patternProperties) | ||
? Object.assign(Object.assign({}, x), { type: "object" }) : x) }), refs)})`; | ||
? { ...x, type: "object" } | ||
: x), | ||
}, refs)})`; | ||
} | ||
return output; | ||
} |
@@ -5,6 +5,12 @@ import { parseSchema } from "./parseSchema.js"; | ||
? schema.oneOf.length === 1 | ||
? parseSchema(schema.oneOf[0], Object.assign(Object.assign({}, refs), { path: [...refs.path, "oneOf", 0] })) | ||
? parseSchema(schema.oneOf[0], { | ||
...refs, | ||
path: [...refs.path, "oneOf", 0], | ||
}) | ||
: `z.any().superRefine((x, ctx) => { | ||
const schemas = [${schema.oneOf | ||
.map((schema, i) => parseSchema(schema, Object.assign(Object.assign({}, refs), { path: [...refs.path, "oneOf", i] }))) | ||
.map((schema, i) => parseSchema(schema, { | ||
...refs, | ||
path: [...refs.path, "oneOf", i], | ||
})) | ||
.join(", ")}]; | ||
@@ -11,0 +17,0 @@ const errors = schemas.reduce( |
export function withMessage(schema, key, get) { | ||
var _a; | ||
const value = schema[key]; | ||
@@ -12,3 +11,3 @@ let r = ""; | ||
r += opener; | ||
if (((_a = schema.errorMessage) === null || _a === void 0 ? void 0 : _a[key]) !== undefined) { | ||
if (schema.errorMessage?.[key] !== undefined) { | ||
r += prefix + JSON.stringify(schema.errorMessage[key]); | ||
@@ -15,0 +14,0 @@ } |
{ | ||
"name": "json-schema-to-zod", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Converts JSON schema objects or files into Zod schemas", | ||
@@ -5,0 +5,0 @@ "types": "dist/types/index.d.ts", |
2407
94590