@gabrielbryk/json-schema-to-zod 2.8.0 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/cjs/core/analyzeSchema.js +62 -0
- package/dist/cjs/core/emitZod.js +141 -0
- package/dist/cjs/generators/generateBundle.js +117 -63
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/jsonSchemaToZod.js +5 -167
- package/dist/cjs/parsers/parseAllOf.js +12 -6
- package/dist/cjs/parsers/parseBoolean.js +1 -3
- package/dist/cjs/parsers/parseIfThenElse.js +1 -1
- package/dist/cjs/parsers/parseNull.js +1 -3
- package/dist/cjs/parsers/parseObject.js +8 -3
- package/dist/cjs/parsers/parseSchema.js +130 -26
- package/dist/cjs/parsers/parseString.js +1 -1
- package/dist/cjs/utils/buildRefRegistry.js +56 -0
- package/dist/cjs/utils/omit.js +3 -2
- package/dist/cjs/utils/resolveUri.js +16 -0
- package/dist/esm/Types.js +1 -2
- package/dist/esm/cli.js +10 -12
- package/dist/esm/core/analyzeSchema.js +58 -0
- package/dist/esm/core/emitZod.js +137 -0
- package/dist/esm/generators/generateBundle.js +118 -68
- package/dist/esm/index.js +34 -46
- package/dist/esm/jsonSchemaToZod.js +5 -171
- package/dist/esm/parsers/parseAllOf.js +17 -14
- package/dist/esm/parsers/parseAnyOf.js +6 -10
- package/dist/esm/parsers/parseArray.js +11 -15
- package/dist/esm/parsers/parseBoolean.js +1 -7
- package/dist/esm/parsers/parseConst.js +1 -5
- package/dist/esm/parsers/parseDefault.js +3 -7
- package/dist/esm/parsers/parseEnum.js +1 -5
- package/dist/esm/parsers/parseIfThenElse.js +6 -10
- package/dist/esm/parsers/parseMultipleType.js +3 -7
- package/dist/esm/parsers/parseNot.js +4 -8
- package/dist/esm/parsers/parseNull.js +1 -7
- package/dist/esm/parsers/parseNullable.js +4 -8
- package/dist/esm/parsers/parseNumber.js +11 -15
- package/dist/esm/parsers/parseObject.js +33 -31
- package/dist/esm/parsers/parseOneOf.js +6 -10
- package/dist/esm/parsers/parseSchema.js +187 -87
- package/dist/esm/parsers/parseSimpleDiscriminatedOneOf.js +6 -10
- package/dist/esm/parsers/parseString.js +12 -16
- package/dist/esm/utils/anyOrUnknown.js +1 -5
- package/dist/esm/utils/buildRefRegistry.js +52 -0
- package/dist/esm/utils/cliTools.js +7 -13
- package/dist/esm/utils/cycles.js +3 -9
- package/dist/esm/utils/half.js +1 -5
- package/dist/esm/utils/jsdocs.js +3 -8
- package/dist/esm/utils/omit.js +4 -7
- package/dist/esm/utils/resolveUri.js +12 -0
- package/dist/esm/utils/withMessage.js +1 -4
- package/dist/esm/zodToJsonSchema.js +1 -4
- package/dist/types/Types.d.ts +34 -3
- package/dist/types/core/analyzeSchema.d.ts +24 -0
- package/dist/types/core/emitZod.d.ts +2 -0
- package/dist/types/generators/generateBundle.d.ts +5 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/jsonSchemaToZod.d.ts +1 -1
- package/dist/types/parsers/parseBoolean.d.ts +1 -4
- package/dist/types/parsers/parseNull.d.ts +1 -4
- package/dist/types/parsers/parseSchema.d.ts +2 -1
- package/dist/types/utils/buildRefRegistry.d.ts +12 -0
- package/dist/types/utils/resolveUri.d.ts +1 -0
- package/docs/proposals/bundle-refactor.md +43 -0
- package/docs/proposals/ref-anchor-support.md +65 -0
- package/eslint.config.js +26 -0
- package/package.json +10 -4
- /package/{jest.config.js → jest.config.cjs} +0 -0
- /package/{postcjs.js → postcjs.cjs} +0 -0
- /package/{postesm.js → postesm.cjs} +0 -0
|
@@ -1,172 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const cycles_js_1 = require("./utils/cycles.js");
|
|
7
|
-
const jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest } = {}) => {
|
|
8
|
-
if (type && (!name || module !== "esm")) {
|
|
9
|
-
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
|
|
10
|
-
}
|
|
11
|
-
const refNameByPointer = new Map();
|
|
12
|
-
const usedNames = new Set();
|
|
13
|
-
const exportRefs = rest.exportRefs ?? true;
|
|
14
|
-
const withMeta = rest.withMeta ?? true;
|
|
15
|
-
if (name)
|
|
16
|
-
usedNames.add(name);
|
|
17
|
-
// Pass 1: collect declarations/dependencies to detect cycles before emitting
|
|
18
|
-
const pass1 = {
|
|
19
|
-
module,
|
|
20
|
-
name,
|
|
21
|
-
path: [],
|
|
22
|
-
seen: new Map(),
|
|
23
|
-
declarations: new Map(),
|
|
24
|
-
dependencies: new Map(),
|
|
25
|
-
inProgress: new Set(),
|
|
26
|
-
refNameByPointer,
|
|
27
|
-
usedNames,
|
|
28
|
-
root: schema,
|
|
29
|
-
currentSchemaName: name,
|
|
30
|
-
...rest,
|
|
31
|
-
withMeta,
|
|
32
|
-
};
|
|
33
|
-
(0, parseSchema_js_1.parseSchema)(schema, pass1);
|
|
34
|
-
const names = Array.from(pass1.declarations.keys());
|
|
35
|
-
const cycleRefNames = (0, cycles_js_1.detectCycles)(names, pass1.dependencies);
|
|
36
|
-
const { componentByName } = (0, cycles_js_1.computeScc)(names, pass1.dependencies);
|
|
37
|
-
// Pass 2: generate with cycle awareness
|
|
38
|
-
const declarations = new Map();
|
|
39
|
-
const dependencies = new Map();
|
|
40
|
-
const parsedSchema = (0, parseSchema_js_1.parseSchema)(schema, {
|
|
41
|
-
module,
|
|
42
|
-
name,
|
|
43
|
-
path: [],
|
|
44
|
-
seen: new Map(),
|
|
45
|
-
declarations,
|
|
46
|
-
dependencies,
|
|
47
|
-
inProgress: new Set(),
|
|
48
|
-
refNameByPointer,
|
|
49
|
-
usedNames,
|
|
50
|
-
root: schema,
|
|
51
|
-
currentSchemaName: name,
|
|
52
|
-
cycleRefNames,
|
|
53
|
-
cycleComponentByName: componentByName,
|
|
54
|
-
...rest,
|
|
55
|
-
withMeta,
|
|
56
|
-
});
|
|
57
|
-
const declarationBlock = declarations.size
|
|
58
|
-
? orderDeclarations(Array.from(declarations.entries()), dependencies)
|
|
59
|
-
.map(([refName, value]) => {
|
|
60
|
-
const shouldExport = exportRefs && module === "esm";
|
|
61
|
-
const decl = `${shouldExport ? "export " : ""}const ${refName} = ${value}`;
|
|
62
|
-
return decl;
|
|
63
|
-
})
|
|
64
|
-
.join("\n")
|
|
65
|
-
: "";
|
|
66
|
-
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description
|
|
67
|
-
? (0, jsdocs_js_1.expandJsdocs)(schema.description)
|
|
68
|
-
: "";
|
|
69
|
-
const lines = [];
|
|
70
|
-
if (module === "cjs" && !noImport) {
|
|
71
|
-
lines.push(`const { z } = require("zod")`);
|
|
72
|
-
}
|
|
73
|
-
if (module === "esm" && !noImport) {
|
|
74
|
-
lines.push(`import { z } from "zod"`);
|
|
75
|
-
}
|
|
76
|
-
if (declarationBlock) {
|
|
77
|
-
lines.push(declarationBlock);
|
|
78
|
-
}
|
|
79
|
-
if (module === "cjs") {
|
|
80
|
-
const payload = name ? `{ ${JSON.stringify(name)}: ${parsedSchema} }` : parsedSchema;
|
|
81
|
-
lines.push(`${jsdocs}module.exports = ${payload}`);
|
|
82
|
-
}
|
|
83
|
-
else if (module === "esm") {
|
|
84
|
-
lines.push(`${jsdocs}export ${name ? `const ${name} =` : `default`} ${parsedSchema}`);
|
|
85
|
-
}
|
|
86
|
-
else if (name) {
|
|
87
|
-
lines.push(`${jsdocs}const ${name} = ${parsedSchema}`);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
lines.push(`${jsdocs}${parsedSchema}`);
|
|
91
|
-
}
|
|
92
|
-
let typeLine;
|
|
93
|
-
if (type && name) {
|
|
94
|
-
let typeName = typeof type === "string"
|
|
95
|
-
? type
|
|
96
|
-
: `${name[0].toUpperCase()}${name.substring(1)}`;
|
|
97
|
-
typeLine = `export type ${typeName} = z.infer<typeof ${name}>`;
|
|
98
|
-
}
|
|
99
|
-
const joined = lines.filter(Boolean).join("\n\n");
|
|
100
|
-
const combined = typeLine ? `${joined}\n${typeLine}` : joined;
|
|
101
|
-
const shouldEndWithNewline = module === "esm" || module === "cjs";
|
|
102
|
-
return `${combined}${shouldEndWithNewline ? "\n" : ""}`;
|
|
1
|
+
import { analyzeSchema } from "./core/analyzeSchema.js";
|
|
2
|
+
import { emitZod } from "./core/emitZod.js";
|
|
3
|
+
export const jsonSchemaToZod = (schema, options = {}) => {
|
|
4
|
+
const analysis = analyzeSchema(schema, options);
|
|
5
|
+
return emitZod(analysis);
|
|
103
6
|
};
|
|
104
|
-
exports.jsonSchemaToZod = jsonSchemaToZod;
|
|
105
|
-
function orderDeclarations(entries, dependencies) {
|
|
106
|
-
const valueByName = new Map(entries);
|
|
107
|
-
const depGraph = new Map();
|
|
108
|
-
// Seed with explicit dependencies recorded during parsing
|
|
109
|
-
for (const [from, set] of dependencies.entries()) {
|
|
110
|
-
const onlyKnown = new Set();
|
|
111
|
-
for (const dep of set) {
|
|
112
|
-
if (valueByName.has(dep) && dep !== from) {
|
|
113
|
-
onlyKnown.add(dep);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (onlyKnown.size)
|
|
117
|
-
depGraph.set(from, onlyKnown);
|
|
118
|
-
}
|
|
119
|
-
// Also infer deps by scanning declaration bodies for referenced names
|
|
120
|
-
const names = Array.from(valueByName.keys());
|
|
121
|
-
for (const [name, value] of entries) {
|
|
122
|
-
const deps = depGraph.get(name) ?? new Set();
|
|
123
|
-
for (const candidate of names) {
|
|
124
|
-
if (candidate === name)
|
|
125
|
-
continue;
|
|
126
|
-
const matcher = new RegExp(`\\b${candidate}\\b`);
|
|
127
|
-
if (matcher.test(value)) {
|
|
128
|
-
deps.add(candidate);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
if (deps.size)
|
|
132
|
-
depGraph.set(name, deps);
|
|
133
|
-
}
|
|
134
|
-
const ordered = [];
|
|
135
|
-
const perm = new Set();
|
|
136
|
-
const temp = new Set();
|
|
137
|
-
const visit = (name) => {
|
|
138
|
-
if (perm.has(name))
|
|
139
|
-
return;
|
|
140
|
-
if (temp.has(name)) {
|
|
141
|
-
// Cycle detected; break it but still include the node.
|
|
142
|
-
temp.delete(name);
|
|
143
|
-
perm.add(name);
|
|
144
|
-
ordered.push(name);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
temp.add(name);
|
|
148
|
-
const deps = depGraph.get(name);
|
|
149
|
-
if (deps) {
|
|
150
|
-
for (const dep of deps) {
|
|
151
|
-
if (valueByName.has(dep)) {
|
|
152
|
-
visit(dep);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
temp.delete(name);
|
|
157
|
-
perm.add(name);
|
|
158
|
-
ordered.push(name);
|
|
159
|
-
};
|
|
160
|
-
for (const name of valueByName.keys()) {
|
|
161
|
-
visit(name);
|
|
162
|
-
}
|
|
163
|
-
const unique = [];
|
|
164
|
-
const seen = new Set();
|
|
165
|
-
for (const name of ordered) {
|
|
166
|
-
if (!seen.has(name)) {
|
|
167
|
-
seen.add(name);
|
|
168
|
-
unique.push(name);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return unique.map((name) => [name, valueByName.get(name)]);
|
|
172
|
-
}
|
|
@@ -1,38 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const half_js_1 = require("../utils/half.js");
|
|
6
|
-
const originalIndex = Symbol("Original index");
|
|
1
|
+
import { parseSchema } from "./parseSchema.js";
|
|
2
|
+
import { half } from "../utils/half.js";
|
|
3
|
+
const originalIndexKey = "__originalIndex";
|
|
7
4
|
const ensureOriginalIndex = (arr) => {
|
|
8
|
-
|
|
5
|
+
const newArr = [];
|
|
9
6
|
for (let i = 0; i < arr.length; i++) {
|
|
10
7
|
const item = arr[i];
|
|
11
8
|
if (typeof item === "boolean") {
|
|
12
|
-
newArr.push(item ? { [
|
|
9
|
+
newArr.push(item ? { [originalIndexKey]: i } : { [originalIndexKey]: i, not: {} });
|
|
13
10
|
}
|
|
14
|
-
else if (
|
|
11
|
+
else if (typeof item === "object" &&
|
|
12
|
+
item !== null &&
|
|
13
|
+
originalIndexKey in item) {
|
|
15
14
|
return arr;
|
|
16
15
|
}
|
|
17
16
|
else {
|
|
18
|
-
newArr.push({ ...item, [
|
|
17
|
+
newArr.push({ ...item, [originalIndexKey]: i });
|
|
19
18
|
}
|
|
20
19
|
}
|
|
21
20
|
return newArr;
|
|
22
21
|
};
|
|
23
|
-
function parseAllOf(schema, refs) {
|
|
22
|
+
export function parseAllOf(schema, refs) {
|
|
24
23
|
if (schema.allOf.length === 0) {
|
|
25
24
|
return "z.never()";
|
|
26
25
|
}
|
|
27
26
|
else if (schema.allOf.length === 1) {
|
|
28
27
|
const item = schema.allOf[0];
|
|
29
|
-
return
|
|
28
|
+
return parseSchema(item, {
|
|
30
29
|
...refs,
|
|
31
|
-
path: [
|
|
30
|
+
path: [
|
|
31
|
+
...refs.path,
|
|
32
|
+
"allOf",
|
|
33
|
+
item[originalIndexKey] ?? 0,
|
|
34
|
+
],
|
|
32
35
|
});
|
|
33
36
|
}
|
|
34
37
|
else {
|
|
35
|
-
const [left, right] =
|
|
38
|
+
const [left, right] = half(ensureOriginalIndex(schema.allOf));
|
|
36
39
|
return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
|
|
37
40
|
allOf: right,
|
|
38
41
|
}, refs)})`;
|
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
6
|
-
const parseAnyOf = (schema, refs) => {
|
|
1
|
+
import { parseSchema } from "./parseSchema.js";
|
|
2
|
+
import { anyOrUnknown } from "../utils/anyOrUnknown.js";
|
|
3
|
+
export const parseAnyOf = (schema, refs) => {
|
|
7
4
|
return schema.anyOf.length
|
|
8
5
|
? schema.anyOf.length === 1
|
|
9
|
-
?
|
|
6
|
+
? parseSchema(schema.anyOf[0], {
|
|
10
7
|
...refs,
|
|
11
8
|
path: [...refs.path, "anyOf", 0],
|
|
12
9
|
})
|
|
13
10
|
: `z.union([${schema.anyOf
|
|
14
|
-
.map((schema, i) =>
|
|
11
|
+
.map((schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] }))
|
|
15
12
|
.join(", ")}])`
|
|
16
|
-
:
|
|
13
|
+
: anyOrUnknown(refs);
|
|
17
14
|
};
|
|
18
|
-
exports.parseAnyOf = parseAnyOf;
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
6
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
7
|
-
const parseArray = (schema, refs) => {
|
|
1
|
+
import { withMessage } from "../utils/withMessage.js";
|
|
2
|
+
import { parseSchema } from "./parseSchema.js";
|
|
3
|
+
import { anyOrUnknown } from "../utils/anyOrUnknown.js";
|
|
4
|
+
export const parseArray = (schema, refs) => {
|
|
8
5
|
if (Array.isArray(schema.items)) {
|
|
9
|
-
let tuple = `z.tuple([${schema.items.map((v, i) =>
|
|
6
|
+
let tuple = `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
10
7
|
if (schema.contains) {
|
|
11
|
-
const containsSchema =
|
|
8
|
+
const containsSchema = parseSchema(schema.contains, {
|
|
12
9
|
...refs,
|
|
13
10
|
path: [...refs.path, "contains"],
|
|
14
11
|
});
|
|
@@ -27,18 +24,18 @@ const parseArray = (schema, refs) => {
|
|
|
27
24
|
return tuple;
|
|
28
25
|
}
|
|
29
26
|
let r = !schema.items
|
|
30
|
-
? `z.array(${
|
|
31
|
-
: `z.array(${
|
|
27
|
+
? `z.array(${anyOrUnknown(refs)})`
|
|
28
|
+
: `z.array(${parseSchema(schema.items, {
|
|
32
29
|
...refs,
|
|
33
30
|
path: [...refs.path, "items"],
|
|
34
31
|
})})`;
|
|
35
|
-
r +=
|
|
32
|
+
r += withMessage(schema, "minItems", ({ json }) => ({
|
|
36
33
|
opener: `.min(${json}`,
|
|
37
34
|
closer: ")",
|
|
38
35
|
messagePrefix: ", { error: ",
|
|
39
36
|
messageCloser: " })",
|
|
40
37
|
}));
|
|
41
|
-
r +=
|
|
38
|
+
r += withMessage(schema, "maxItems", ({ json }) => ({
|
|
42
39
|
opener: `.max(${json}`,
|
|
43
40
|
closer: ")",
|
|
44
41
|
messagePrefix: ", { error: ",
|
|
@@ -69,7 +66,7 @@ const parseArray = (schema, refs) => {
|
|
|
69
66
|
})`;
|
|
70
67
|
}
|
|
71
68
|
if (schema.contains) {
|
|
72
|
-
const containsSchema =
|
|
69
|
+
const containsSchema = parseSchema(schema.contains, {
|
|
73
70
|
...refs,
|
|
74
71
|
path: [...refs.path, "contains"],
|
|
75
72
|
});
|
|
@@ -87,4 +84,3 @@ const parseArray = (schema, refs) => {
|
|
|
87
84
|
}
|
|
88
85
|
return r;
|
|
89
86
|
};
|
|
90
|
-
exports.parseArray = parseArray;
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseBoolean = void 0;
|
|
4
|
-
const parseBoolean = (_schema) => {
|
|
5
|
-
return "z.boolean()";
|
|
6
|
-
};
|
|
7
|
-
exports.parseBoolean = parseBoolean;
|
|
1
|
+
export const parseBoolean = () => "z.boolean()";
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseConst = void 0;
|
|
4
|
-
const parseConst = (schema) => {
|
|
1
|
+
export const parseConst = (schema) => {
|
|
5
2
|
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
6
3
|
};
|
|
7
|
-
exports.parseConst = parseConst;
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
5
|
-
const parseDefault = (_schema, refs) => {
|
|
6
|
-
return (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
1
|
+
import { anyOrUnknown } from "../utils/anyOrUnknown.js";
|
|
2
|
+
export const parseDefault = (_schema, refs) => {
|
|
3
|
+
return anyOrUnknown(refs);
|
|
7
4
|
};
|
|
8
|
-
exports.parseDefault = parseDefault;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseEnum = void 0;
|
|
4
|
-
const parseEnum = (schema) => {
|
|
1
|
+
export const parseEnum = (schema) => {
|
|
5
2
|
if (schema.enum.length === 0) {
|
|
6
3
|
return "z.never()";
|
|
7
4
|
}
|
|
@@ -18,4 +15,3 @@ const parseEnum = (schema) => {
|
|
|
18
15
|
.join(", ")}])`;
|
|
19
16
|
}
|
|
20
17
|
};
|
|
21
|
-
exports.parseEnum = parseEnum;
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const parseIfThenElse = (schema, refs) => {
|
|
6
|
-
const $if = (0, parseSchema_js_1.parseSchema)(schema.if, { ...refs, path: [...refs.path, "if"] });
|
|
7
|
-
const $then = (0, parseSchema_js_1.parseSchema)(schema.then, {
|
|
1
|
+
import { parseSchema } from "./parseSchema.js";
|
|
2
|
+
export const parseIfThenElse = (schema, refs) => {
|
|
3
|
+
const $if = parseSchema(schema.if, { ...refs, path: [...refs.path, "if"] });
|
|
4
|
+
const $then = parseSchema(schema.then, {
|
|
8
5
|
...refs,
|
|
9
6
|
path: [...refs.path, "then"],
|
|
10
7
|
});
|
|
11
|
-
const $else =
|
|
8
|
+
const $else = parseSchema(schema.else, {
|
|
12
9
|
...refs,
|
|
13
10
|
path: [...refs.path, "else"],
|
|
14
11
|
});
|
|
@@ -17,7 +14,7 @@ const parseIfThenElse = (schema, refs) => {
|
|
|
17
14
|
? ${$then}.safeParse(value)
|
|
18
15
|
: ${$else}.safeParse(value);
|
|
19
16
|
if (!result.success) {
|
|
20
|
-
const issues = result.error.issues
|
|
17
|
+
const issues = result.error.issues;
|
|
21
18
|
issues.forEach((issue) => ctx.addIssue(issue))
|
|
22
19
|
}
|
|
23
20
|
})`;
|
|
@@ -32,4 +29,3 @@ const parseIfThenElse = (schema, refs) => {
|
|
|
32
29
|
}
|
|
33
30
|
return result;
|
|
34
31
|
};
|
|
35
|
-
exports.parseIfThenElse = parseIfThenElse;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.parseMultipleType = void 0;
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const parseMultipleType = (schema, refs) => {
|
|
1
|
+
import { parseSchema } from "./parseSchema.js";
|
|
2
|
+
export const parseMultipleType = (schema, refs) => {
|
|
6
3
|
return `z.union([${schema.type
|
|
7
|
-
.map((type) =>
|
|
4
|
+
.map((type) => parseSchema({ ...schema, type }, { ...refs, withoutDefaults: true }))
|
|
8
5
|
.join(", ")}])`;
|
|
9
6
|
};
|
|
10
|
-
exports.parseMultipleType = parseMultipleType;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
6
|
-
const parseNot = (schema, refs) => {
|
|
7
|
-
return `${(0, anyOrUnknown_js_1.anyOrUnknown)(refs)}.refine((value) => !${(0, parseSchema_js_1.parseSchema)(schema.not, {
|
|
1
|
+
import { parseSchema } from "./parseSchema.js";
|
|
2
|
+
import { anyOrUnknown } from "../utils/anyOrUnknown.js";
|
|
3
|
+
export const parseNot = (schema, refs) => {
|
|
4
|
+
return `${anyOrUnknown(refs)}.refine((value) => !${parseSchema(schema.not, {
|
|
8
5
|
...refs,
|
|
9
6
|
path: [...refs.path, "not"],
|
|
10
7
|
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
|
|
11
8
|
};
|
|
12
|
-
exports.parseNot = parseNot;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.parseNullable = void 0;
|
|
4
|
-
const omit_js_1 = require("../utils/omit.js");
|
|
5
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
1
|
+
import { omit } from "../utils/omit.js";
|
|
2
|
+
import { parseSchema } from "./parseSchema.js";
|
|
6
3
|
/**
|
|
7
4
|
* For compatibility with open api 3.0 nullable
|
|
8
5
|
*/
|
|
9
|
-
const parseNullable = (schema, refs) => {
|
|
10
|
-
return `${
|
|
6
|
+
export const parseNullable = (schema, refs) => {
|
|
7
|
+
return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
|
|
11
8
|
};
|
|
12
|
-
exports.parseNullable = parseNullable;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.parseNumber = void 0;
|
|
4
|
-
const withMessage_js_1 = require("../utils/withMessage.js");
|
|
5
|
-
const parseNumber = (schema) => {
|
|
1
|
+
import { withMessage } from "../utils/withMessage.js";
|
|
2
|
+
export const parseNumber = (schema) => {
|
|
6
3
|
const formatError = schema.errorMessage?.format;
|
|
7
4
|
const numericFormatMap = {
|
|
8
5
|
int32: "z.int32",
|
|
@@ -18,7 +15,7 @@ const parseNumber = (schema) => {
|
|
|
18
15
|
let r = mappedFormat ? `${mappedFormat}(${formatParams})` : "z.number()";
|
|
19
16
|
if (schema.type === "integer") {
|
|
20
17
|
if (!mappedFormat) {
|
|
21
|
-
r +=
|
|
18
|
+
r += withMessage(schema, "type", () => ({
|
|
22
19
|
opener: ".int(",
|
|
23
20
|
closer: ")",
|
|
24
21
|
messagePrefix: "{ error: ",
|
|
@@ -28,7 +25,7 @@ const parseNumber = (schema) => {
|
|
|
28
25
|
}
|
|
29
26
|
else {
|
|
30
27
|
if (!mappedFormat) {
|
|
31
|
-
r +=
|
|
28
|
+
r += withMessage(schema, "format", ({ value }) => {
|
|
32
29
|
if (value === "int64") {
|
|
33
30
|
return {
|
|
34
31
|
opener: ".int(",
|
|
@@ -40,7 +37,7 @@ const parseNumber = (schema) => {
|
|
|
40
37
|
});
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
|
-
r +=
|
|
40
|
+
r += withMessage(schema, "multipleOf", ({ value, json }) => {
|
|
44
41
|
if (value === 1) {
|
|
45
42
|
if (r.startsWith("z.number().int(")) {
|
|
46
43
|
return;
|
|
@@ -61,7 +58,7 @@ const parseNumber = (schema) => {
|
|
|
61
58
|
});
|
|
62
59
|
if (typeof schema.minimum === "number") {
|
|
63
60
|
if (schema.exclusiveMinimum === true) {
|
|
64
|
-
r +=
|
|
61
|
+
r += withMessage(schema, "minimum", ({ json }) => ({
|
|
65
62
|
opener: `.gt(${json}`,
|
|
66
63
|
closer: ")",
|
|
67
64
|
messagePrefix: ", { error: ",
|
|
@@ -69,7 +66,7 @@ const parseNumber = (schema) => {
|
|
|
69
66
|
}));
|
|
70
67
|
}
|
|
71
68
|
else {
|
|
72
|
-
r +=
|
|
69
|
+
r += withMessage(schema, "minimum", ({ json }) => ({
|
|
73
70
|
opener: `.gte(${json}`,
|
|
74
71
|
closer: ")",
|
|
75
72
|
messagePrefix: ", { error: ",
|
|
@@ -78,7 +75,7 @@ const parseNumber = (schema) => {
|
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
77
|
else if (typeof schema.exclusiveMinimum === "number") {
|
|
81
|
-
r +=
|
|
78
|
+
r += withMessage(schema, "exclusiveMinimum", ({ json }) => ({
|
|
82
79
|
opener: `.gt(${json}`,
|
|
83
80
|
closer: ")",
|
|
84
81
|
messagePrefix: ", { error: ",
|
|
@@ -87,7 +84,7 @@ const parseNumber = (schema) => {
|
|
|
87
84
|
}
|
|
88
85
|
if (typeof schema.maximum === "number") {
|
|
89
86
|
if (schema.exclusiveMaximum === true) {
|
|
90
|
-
r +=
|
|
87
|
+
r += withMessage(schema, "maximum", ({ json }) => ({
|
|
91
88
|
opener: `.lt(${json}`,
|
|
92
89
|
closer: ")",
|
|
93
90
|
messagePrefix: ", { error: ",
|
|
@@ -95,7 +92,7 @@ const parseNumber = (schema) => {
|
|
|
95
92
|
}));
|
|
96
93
|
}
|
|
97
94
|
else {
|
|
98
|
-
r +=
|
|
95
|
+
r += withMessage(schema, "maximum", ({ json }) => ({
|
|
99
96
|
opener: `.lte(${json}`,
|
|
100
97
|
closer: ")",
|
|
101
98
|
messagePrefix: ", { error: ",
|
|
@@ -104,7 +101,7 @@ const parseNumber = (schema) => {
|
|
|
104
101
|
}
|
|
105
102
|
}
|
|
106
103
|
else if (typeof schema.exclusiveMaximum === "number") {
|
|
107
|
-
r +=
|
|
104
|
+
r += withMessage(schema, "exclusiveMaximum", ({ json }) => ({
|
|
108
105
|
opener: `.lt(${json}`,
|
|
109
106
|
closer: ")",
|
|
110
107
|
messagePrefix: ", { error: ",
|
|
@@ -113,4 +110,3 @@ const parseNumber = (schema) => {
|
|
|
113
110
|
}
|
|
114
111
|
return r;
|
|
115
112
|
};
|
|
116
|
-
exports.parseNumber = parseNumber;
|