@gabrielbryk/json-schema-to-zod 2.7.3 → 2.8.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/.github/workflows/release.yml +0 -5
- package/CHANGELOG.md +17 -0
- package/dist/cjs/generators/generateBundle.js +311 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/jsonSchemaToZod.js +96 -2
- package/dist/cjs/parsers/parseArray.js +34 -15
- package/dist/cjs/parsers/parseIfThenElse.js +2 -1
- package/dist/cjs/parsers/parseNumber.js +81 -39
- package/dist/cjs/parsers/parseObject.js +32 -9
- package/dist/cjs/parsers/parseSchema.js +23 -1
- package/dist/cjs/parsers/parseString.js +294 -54
- package/dist/cjs/utils/cycles.js +113 -0
- package/dist/cjs/utils/withMessage.js +4 -5
- package/dist/esm/Types.js +2 -1
- package/dist/esm/cli.js +12 -10
- package/dist/esm/generators/generateBundle.js +311 -0
- package/dist/esm/index.js +46 -28
- package/dist/esm/jsonSchemaToZod.js +105 -7
- package/dist/esm/parsers/parseAllOf.js +8 -5
- package/dist/esm/parsers/parseAnyOf.js +10 -6
- package/dist/esm/parsers/parseArray.js +47 -24
- package/dist/esm/parsers/parseBoolean.js +5 -1
- package/dist/esm/parsers/parseConst.js +5 -1
- package/dist/esm/parsers/parseDefault.js +7 -3
- package/dist/esm/parsers/parseEnum.js +5 -1
- package/dist/esm/parsers/parseIfThenElse.js +11 -6
- package/dist/esm/parsers/parseMultipleType.js +7 -3
- package/dist/esm/parsers/parseNot.js +8 -4
- package/dist/esm/parsers/parseNull.js +5 -1
- package/dist/esm/parsers/parseNullable.js +8 -4
- package/dist/esm/parsers/parseNumber.js +88 -42
- package/dist/esm/parsers/parseObject.js +59 -33
- package/dist/esm/parsers/parseOneOf.js +10 -6
- package/dist/esm/parsers/parseSchema.js +85 -59
- package/dist/esm/parsers/parseSimpleDiscriminatedOneOf.js +10 -6
- package/dist/esm/parsers/parseString.js +303 -59
- package/dist/esm/utils/anyOrUnknown.js +5 -1
- package/dist/esm/utils/cliTools.js +13 -7
- package/dist/esm/utils/cycles.js +113 -0
- package/dist/esm/utils/half.js +5 -1
- package/dist/esm/utils/jsdocs.js +8 -3
- package/dist/esm/utils/omit.js +5 -1
- package/dist/esm/utils/withMessage.js +8 -6
- package/dist/esm/zodToJsonSchema.js +4 -1
- package/dist/types/Types.d.ts +8 -0
- package/dist/types/generators/generateBundle.d.ts +57 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/parsers/parseString.d.ts +2 -2
- package/dist/types/utils/cycles.d.ts +7 -0
- package/dist/types/utils/jsdocs.d.ts +1 -1
- package/dist/types/utils/withMessage.d.ts +6 -1
- package/package.json +1 -1
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseObject = parseObject;
|
|
4
|
+
const parseAnyOf_js_1 = require("./parseAnyOf.js");
|
|
5
|
+
const parseOneOf_js_1 = require("./parseOneOf.js");
|
|
6
|
+
const parseSchema_js_1 = require("./parseSchema.js");
|
|
7
|
+
const parseAllOf_js_1 = require("./parseAllOf.js");
|
|
8
|
+
const parseIfThenElse_js_1 = require("./parseIfThenElse.js");
|
|
9
|
+
const jsdocs_js_1 = require("../utils/jsdocs.js");
|
|
10
|
+
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
11
|
+
function parseObject(objectSchema, refs) {
|
|
9
12
|
let properties = undefined;
|
|
10
13
|
if (objectSchema.properties) {
|
|
11
14
|
if (!Object.keys(objectSchema.properties).length) {
|
|
@@ -16,7 +19,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
16
19
|
properties += Object.keys(objectSchema.properties)
|
|
17
20
|
.map((key) => {
|
|
18
21
|
const propSchema = objectSchema.properties[key];
|
|
19
|
-
const parsedProp = parseSchema(propSchema, {
|
|
22
|
+
const parsedProp = (0, parseSchema_js_1.parseSchema)(propSchema, {
|
|
20
23
|
...refs,
|
|
21
24
|
path: [...refs.path, "properties", key],
|
|
22
25
|
});
|
|
@@ -32,7 +35,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
32
35
|
? `get ${JSON.stringify(key)}(){ return ${valueWithOptional} }`
|
|
33
36
|
: `${JSON.stringify(key)}: ${valueWithOptional}`;
|
|
34
37
|
if (refs.withJsdocs && typeof propSchema === "object") {
|
|
35
|
-
result = addJsdocs(propSchema, result);
|
|
38
|
+
result = (0, jsdocs_js_1.addJsdocs)(propSchema, result);
|
|
36
39
|
}
|
|
37
40
|
return result;
|
|
38
41
|
})
|
|
@@ -41,7 +44,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
41
44
|
}
|
|
42
45
|
}
|
|
43
46
|
const additionalProperties = objectSchema.additionalProperties !== undefined
|
|
44
|
-
? parseSchema(objectSchema.additionalProperties, {
|
|
47
|
+
? (0, parseSchema_js_1.parseSchema)(objectSchema.additionalProperties, {
|
|
45
48
|
...refs,
|
|
46
49
|
path: [...refs.path, "additionalProperties"],
|
|
47
50
|
})
|
|
@@ -52,7 +55,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
52
55
|
const parsedPatternProperties = Object.fromEntries(Object.entries(objectSchema.patternProperties).map(([key, value]) => {
|
|
53
56
|
return [
|
|
54
57
|
key,
|
|
55
|
-
parseSchema(value, {
|
|
58
|
+
(0, parseSchema_js_1.parseSchema)(value, {
|
|
56
59
|
...refs,
|
|
57
60
|
path: [...refs.path, "patternProperties", key],
|
|
58
61
|
}),
|
|
@@ -75,16 +78,16 @@ export function parseObject(objectSchema, refs) {
|
|
|
75
78
|
}
|
|
76
79
|
else {
|
|
77
80
|
if (additionalProperties) {
|
|
78
|
-
patternProperties += `z.record(z.union([${[
|
|
81
|
+
patternProperties += `z.record(z.string(), z.union([${[
|
|
79
82
|
...Object.values(parsedPatternProperties),
|
|
80
83
|
additionalProperties,
|
|
81
84
|
].join(", ")}]))`;
|
|
82
85
|
}
|
|
83
86
|
else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
84
|
-
patternProperties += `z.record(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
87
|
+
patternProperties += `z.record(z.string(), z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
85
88
|
}
|
|
86
89
|
else {
|
|
87
|
-
patternProperties += `z.record(${Object.values(parsedPatternProperties)})`;
|
|
90
|
+
patternProperties += `z.record(z.string(), ${Object.values(parsedPatternProperties)})`;
|
|
88
91
|
}
|
|
89
92
|
}
|
|
90
93
|
patternProperties += ".superRefine((value, ctx) => {\n";
|
|
@@ -100,9 +103,8 @@ export function parseObject(objectSchema, refs) {
|
|
|
100
103
|
}
|
|
101
104
|
}
|
|
102
105
|
for (const key in objectSchema.patternProperties) {
|
|
103
|
-
const escapedPattern = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
104
106
|
patternProperties +=
|
|
105
|
-
"if (key.match(new RegExp(" + JSON.stringify(
|
|
107
|
+
"if (key.match(new RegExp(" + JSON.stringify(key) + "))) {\n";
|
|
106
108
|
if (additionalProperties) {
|
|
107
109
|
patternProperties += "evaluated = true\n";
|
|
108
110
|
}
|
|
@@ -112,7 +114,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
112
114
|
".safeParse(value[key])\n";
|
|
113
115
|
patternProperties += "if (!result.success) {\n";
|
|
114
116
|
patternProperties += `ctx.addIssue({
|
|
115
|
-
path: [...ctx.path, key],
|
|
117
|
+
path: [...(ctx.path ?? []), key],
|
|
116
118
|
code: 'custom',
|
|
117
119
|
message: \`Invalid input: Key matching regex /\${key}/ must match schema\`,
|
|
118
120
|
params: {
|
|
@@ -128,7 +130,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
128
130
|
"const result = " + additionalProperties + ".safeParse(value[key])\n";
|
|
129
131
|
patternProperties += "if (!result.success) {\n";
|
|
130
132
|
patternProperties += `ctx.addIssue({
|
|
131
|
-
path: [...ctx.path, key],
|
|
133
|
+
path: [...(ctx.path ?? []), key],
|
|
132
134
|
code: 'custom',
|
|
133
135
|
message: \`Invalid input: must match catchall schema\`,
|
|
134
136
|
params: {
|
|
@@ -152,7 +154,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
152
154
|
// Check if there will be an .and() call that adds properties from oneOf/anyOf/allOf/if-then-else
|
|
153
155
|
// In that case, we should NOT use .strict() because it will reject the additional keys
|
|
154
156
|
// before the union gets a chance to validate them.
|
|
155
|
-
const hasCompositionKeywords = its.an.anyOf(objectSchema) || its.a.oneOf(objectSchema) || its.an.allOf(objectSchema) || its.a.conditional(objectSchema);
|
|
157
|
+
const hasCompositionKeywords = parseSchema_js_1.its.an.anyOf(objectSchema) || parseSchema_js_1.its.a.oneOf(objectSchema) || parseSchema_js_1.its.an.allOf(objectSchema) || parseSchema_js_1.its.a.conditional(objectSchema);
|
|
156
158
|
let output = properties
|
|
157
159
|
? patternProperties
|
|
158
160
|
? properties + patternProperties
|
|
@@ -167,13 +169,13 @@ export function parseObject(objectSchema, refs) {
|
|
|
167
169
|
: patternProperties
|
|
168
170
|
? patternProperties
|
|
169
171
|
: additionalProperties
|
|
170
|
-
? `z.record(${additionalProperties})`
|
|
171
|
-
: `z.record(${anyOrUnknown(refs)})`;
|
|
172
|
+
? `z.record(z.string(), ${additionalProperties})`
|
|
173
|
+
: `z.record(z.string(), ${(0, anyOrUnknown_js_1.anyOrUnknown)(refs)})`;
|
|
172
174
|
if (unevaluated === false && properties && !hasCompositionKeywords) {
|
|
173
175
|
output += ".strict()";
|
|
174
176
|
}
|
|
175
177
|
else if (unevaluated && typeof unevaluated !== 'boolean') {
|
|
176
|
-
const unevaluatedSchema = parseSchema(unevaluated, {
|
|
178
|
+
const unevaluatedSchema = (0, parseSchema_js_1.parseSchema)(unevaluated, {
|
|
177
179
|
...refs,
|
|
178
180
|
path: [...refs.path, "unevaluatedProperties"],
|
|
179
181
|
});
|
|
@@ -194,8 +196,8 @@ export function parseObject(objectSchema, refs) {
|
|
|
194
196
|
}
|
|
195
197
|
})`;
|
|
196
198
|
}
|
|
197
|
-
if (its.an.anyOf(objectSchema)) {
|
|
198
|
-
output += `.and(${parseAnyOf({
|
|
199
|
+
if (parseSchema_js_1.its.an.anyOf(objectSchema)) {
|
|
200
|
+
output += `.and(${(0, parseAnyOf_js_1.parseAnyOf)({
|
|
199
201
|
...objectSchema,
|
|
200
202
|
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" &&
|
|
201
203
|
!x.type &&
|
|
@@ -204,8 +206,8 @@ export function parseObject(objectSchema, refs) {
|
|
|
204
206
|
: x),
|
|
205
207
|
}, refs)})`;
|
|
206
208
|
}
|
|
207
|
-
if (its.a.oneOf(objectSchema)) {
|
|
208
|
-
output += `.and(${parseOneOf({
|
|
209
|
+
if (parseSchema_js_1.its.a.oneOf(objectSchema)) {
|
|
210
|
+
output += `.and(${(0, parseOneOf_js_1.parseOneOf)({
|
|
209
211
|
...objectSchema,
|
|
210
212
|
oneOf: objectSchema.oneOf.map((x) => typeof x === "object" &&
|
|
211
213
|
!x.type &&
|
|
@@ -214,8 +216,8 @@ export function parseObject(objectSchema, refs) {
|
|
|
214
216
|
: x),
|
|
215
217
|
}, refs)})`;
|
|
216
218
|
}
|
|
217
|
-
if (its.an.allOf(objectSchema)) {
|
|
218
|
-
output += `.and(${parseAllOf({
|
|
219
|
+
if (parseSchema_js_1.its.an.allOf(objectSchema)) {
|
|
220
|
+
output += `.and(${(0, parseAllOf_js_1.parseAllOf)({
|
|
219
221
|
...objectSchema,
|
|
220
222
|
allOf: objectSchema.allOf.map((x) => typeof x === "object" &&
|
|
221
223
|
!x.type &&
|
|
@@ -225,8 +227,8 @@ export function parseObject(objectSchema, refs) {
|
|
|
225
227
|
}, refs)})`;
|
|
226
228
|
}
|
|
227
229
|
// Handle if/then/else conditionals on object schemas
|
|
228
|
-
if (its.a.conditional(objectSchema)) {
|
|
229
|
-
output += `.and(${parseIfThenElse(objectSchema, refs)})`;
|
|
230
|
+
if (parseSchema_js_1.its.a.conditional(objectSchema)) {
|
|
231
|
+
output += `.and(${(0, parseIfThenElse_js_1.parseIfThenElse)(objectSchema, refs)})`;
|
|
230
232
|
}
|
|
231
233
|
// propertyNames
|
|
232
234
|
if (objectSchema.propertyNames) {
|
|
@@ -235,7 +237,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
235
237
|
objectSchema.propertyNames.pattern
|
|
236
238
|
? { ...objectSchema.propertyNames, type: "string" }
|
|
237
239
|
: objectSchema.propertyNames;
|
|
238
|
-
const propNameSchema = parseSchema(normalizedPropNames, {
|
|
240
|
+
const propNameSchema = (0, parseSchema_js_1.parseSchema)(normalizedPropNames, {
|
|
239
241
|
...refs,
|
|
240
242
|
path: [...refs.path, "propertyNames"],
|
|
241
243
|
});
|
|
@@ -260,7 +262,7 @@ export function parseObject(objectSchema, refs) {
|
|
|
260
262
|
output += `.superRefine((obj, ctx) => {
|
|
261
263
|
${entries
|
|
262
264
|
.map(([key, schema], idx) => {
|
|
263
|
-
const parsed = parseSchema(schema, { ...refs, path: [...refs.path, "dependentSchemas", key] });
|
|
265
|
+
const parsed = (0, parseSchema_js_1.parseSchema)(schema, { ...refs, path: [...refs.path, "dependentSchemas", key] });
|
|
264
266
|
return `if (Object.prototype.hasOwnProperty.call(obj, ${JSON.stringify(key)})) {
|
|
265
267
|
const result = ${parsed}.safeParse(obj);
|
|
266
268
|
if (!result.success) {
|
|
@@ -269,6 +271,30 @@ export function parseObject(objectSchema, refs) {
|
|
|
269
271
|
}`;
|
|
270
272
|
})
|
|
271
273
|
.join("\n ")}
|
|
274
|
+
})`;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// dependentRequired
|
|
278
|
+
if (objectSchema.dependentRequired && typeof objectSchema.dependentRequired === "object") {
|
|
279
|
+
const entries = Object.entries(objectSchema.dependentRequired);
|
|
280
|
+
if (entries.length) {
|
|
281
|
+
const depRequiredMessage = objectSchema.errorMessage?.dependentRequired ?? "Dependent required properties missing";
|
|
282
|
+
output += `.superRefine((obj, ctx) => {
|
|
283
|
+
${entries
|
|
284
|
+
.map(([prop, deps]) => {
|
|
285
|
+
const arr = Array.isArray(deps) ? deps : [];
|
|
286
|
+
if (!arr.length)
|
|
287
|
+
return "";
|
|
288
|
+
const jsonDeps = JSON.stringify(arr);
|
|
289
|
+
return `if (Object.prototype.hasOwnProperty.call(obj, ${JSON.stringify(prop)})) {
|
|
290
|
+
const missing = ${jsonDeps}.filter((d) => !Object.prototype.hasOwnProperty.call(obj, d));
|
|
291
|
+
if (missing.length) {
|
|
292
|
+
ctx.addIssue({ code: "custom", message: ${JSON.stringify(depRequiredMessage)}, path: [], params: { missing } });
|
|
293
|
+
}
|
|
294
|
+
}`;
|
|
295
|
+
})
|
|
296
|
+
.filter(Boolean)
|
|
297
|
+
.join("\n ")}
|
|
272
298
|
})`;
|
|
273
299
|
}
|
|
274
300
|
}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseOneOf = void 0;
|
|
4
|
+
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
+
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
6
|
+
const parseOneOf = (schema, refs) => {
|
|
4
7
|
if (!schema.oneOf.length) {
|
|
5
|
-
return anyOrUnknown(refs);
|
|
8
|
+
return (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
6
9
|
}
|
|
7
10
|
if (schema.oneOf.length === 1) {
|
|
8
|
-
return parseSchema(schema.oneOf[0], {
|
|
11
|
+
return (0, parseSchema_js_1.parseSchema)(schema.oneOf[0], {
|
|
9
12
|
...refs,
|
|
10
13
|
path: [...refs.path, "oneOf", 0],
|
|
11
14
|
});
|
|
12
15
|
}
|
|
13
16
|
// Generate parsed schemas for each oneOf option
|
|
14
|
-
const parsedSchemas = schema.oneOf.map((s, i) => parseSchema(s, {
|
|
17
|
+
const parsedSchemas = schema.oneOf.map((s, i) => (0, parseSchema_js_1.parseSchema)(s, {
|
|
15
18
|
...refs,
|
|
16
19
|
path: [...refs.path, "oneOf", i],
|
|
17
20
|
}));
|
|
@@ -47,3 +50,4 @@ export const parseOneOf = (schema, refs) => {
|
|
|
47
50
|
// Default: use simple z.union() (at least one must match)
|
|
48
51
|
return `z.union([${parsedSchemas.join(", ")}])`;
|
|
49
52
|
};
|
|
53
|
+
exports.parseOneOf = parseOneOf;
|
|
@@ -1,30 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.its = exports.parseSchema = void 0;
|
|
4
|
+
const parseAnyOf_js_1 = require("./parseAnyOf.js");
|
|
5
|
+
const parseBoolean_js_1 = require("./parseBoolean.js");
|
|
6
|
+
const parseDefault_js_1 = require("./parseDefault.js");
|
|
7
|
+
const parseMultipleType_js_1 = require("./parseMultipleType.js");
|
|
8
|
+
const parseNot_js_1 = require("./parseNot.js");
|
|
9
|
+
const parseNull_js_1 = require("./parseNull.js");
|
|
10
|
+
const parseAllOf_js_1 = require("./parseAllOf.js");
|
|
11
|
+
const parseArray_js_1 = require("./parseArray.js");
|
|
12
|
+
const parseConst_js_1 = require("./parseConst.js");
|
|
13
|
+
const parseEnum_js_1 = require("./parseEnum.js");
|
|
14
|
+
const parseIfThenElse_js_1 = require("./parseIfThenElse.js");
|
|
15
|
+
const parseNumber_js_1 = require("./parseNumber.js");
|
|
16
|
+
const parseObject_js_1 = require("./parseObject.js");
|
|
17
|
+
const parseString_js_1 = require("./parseString.js");
|
|
18
|
+
const parseOneOf_js_1 = require("./parseOneOf.js");
|
|
19
|
+
const parseSimpleDiscriminatedOneOf_js_1 = require("./parseSimpleDiscriminatedOneOf.js");
|
|
20
|
+
const parseNullable_js_1 = require("./parseNullable.js");
|
|
21
|
+
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
22
|
+
const parseSchema = (schema, refs = { seen: new Map(), path: [] }, blockMeta) => {
|
|
20
23
|
// Ensure ref bookkeeping exists so $ref declarations and getter-based recursion work
|
|
21
24
|
refs.root = refs.root ?? schema;
|
|
22
25
|
refs.declarations = refs.declarations ?? new Map();
|
|
26
|
+
refs.dependencies = refs.dependencies ?? new Map();
|
|
23
27
|
refs.inProgress = refs.inProgress ?? new Set();
|
|
24
28
|
refs.refNameByPointer = refs.refNameByPointer ?? new Map();
|
|
25
29
|
refs.usedNames = refs.usedNames ?? new Set();
|
|
26
30
|
if (typeof schema !== "object")
|
|
27
|
-
return schema ? anyOrUnknown(refs) : "z.never()";
|
|
31
|
+
return schema ? (0, anyOrUnknown_js_1.anyOrUnknown)(refs) : "z.never()";
|
|
28
32
|
if (refs.parserOverride) {
|
|
29
33
|
const custom = refs.parserOverride(schema, refs);
|
|
30
34
|
if (typeof custom === "string") {
|
|
@@ -37,7 +41,7 @@ export const parseSchema = (schema, refs = { seen: new Map(), path: [] }, blockM
|
|
|
37
41
|
return seen.r;
|
|
38
42
|
}
|
|
39
43
|
if (refs.depth === undefined || seen.n >= refs.depth) {
|
|
40
|
-
return anyOrUnknown(refs);
|
|
44
|
+
return (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
41
45
|
}
|
|
42
46
|
seen.n += 1;
|
|
43
47
|
}
|
|
@@ -45,7 +49,7 @@ export const parseSchema = (schema, refs = { seen: new Map(), path: [] }, blockM
|
|
|
45
49
|
seen = { r: undefined, n: 0 };
|
|
46
50
|
refs.seen.set(schema, seen);
|
|
47
51
|
}
|
|
48
|
-
if (its.a.ref(schema)) {
|
|
52
|
+
if (exports.its.a.ref(schema)) {
|
|
49
53
|
const parsedRef = parseRef(schema, refs);
|
|
50
54
|
seen.r = parsedRef;
|
|
51
55
|
return parsedRef;
|
|
@@ -63,16 +67,17 @@ export const parseSchema = (schema, refs = { seen: new Map(), path: [] }, blockM
|
|
|
63
67
|
seen.r = parsed;
|
|
64
68
|
return parsed;
|
|
65
69
|
};
|
|
70
|
+
exports.parseSchema = parseSchema;
|
|
66
71
|
const parseRef = (schema, refs) => {
|
|
67
72
|
const resolved = resolveRef(refs.root, schema.$ref);
|
|
68
73
|
if (!resolved) {
|
|
69
|
-
return anyOrUnknown(refs);
|
|
74
|
+
return (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
70
75
|
}
|
|
71
76
|
const { schema: target, path } = resolved;
|
|
72
77
|
const refName = getOrCreateRefName(schema.$ref, path, refs);
|
|
73
78
|
if (!refs.declarations.has(refName) && !refs.inProgress.has(refName)) {
|
|
74
79
|
refs.inProgress.add(refName);
|
|
75
|
-
const declaration = parseSchema(target, {
|
|
80
|
+
const declaration = (0, exports.parseSchema)(target, {
|
|
76
81
|
...refs,
|
|
77
82
|
path,
|
|
78
83
|
currentSchemaName: refName,
|
|
@@ -81,6 +86,27 @@ const parseRef = (schema, refs) => {
|
|
|
81
86
|
refs.inProgress.delete(refName);
|
|
82
87
|
refs.declarations.set(refName, declaration);
|
|
83
88
|
}
|
|
89
|
+
const current = refs.currentSchemaName;
|
|
90
|
+
if (current) {
|
|
91
|
+
const deps = refs.dependencies;
|
|
92
|
+
const set = deps.get(current) ?? new Set();
|
|
93
|
+
set.add(refName);
|
|
94
|
+
deps.set(current, set);
|
|
95
|
+
}
|
|
96
|
+
const currentComponent = refs.currentSchemaName
|
|
97
|
+
? refs.cycleComponentByName?.get(refs.currentSchemaName)
|
|
98
|
+
: undefined;
|
|
99
|
+
const targetComponent = refs.cycleComponentByName?.get(refName);
|
|
100
|
+
const isSameCycle = currentComponent !== undefined &&
|
|
101
|
+
targetComponent !== undefined &&
|
|
102
|
+
currentComponent === targetComponent &&
|
|
103
|
+
refs.cycleRefNames?.has(refName);
|
|
104
|
+
// Only lazy if the ref stays inside the current strongly-connected component
|
|
105
|
+
// (or is currently being resolved). This avoids TDZ on true cycles while
|
|
106
|
+
// letting ordered, acyclic refs stay direct.
|
|
107
|
+
if (isSameCycle || refs.inProgress.has(refName)) {
|
|
108
|
+
return `z.lazy(() => ${refName})`;
|
|
109
|
+
}
|
|
84
110
|
return refName;
|
|
85
111
|
};
|
|
86
112
|
const addDescribes = (schema, parsed, refs) => {
|
|
@@ -175,60 +201,60 @@ const addAnnotations = (schema, parsed) => {
|
|
|
175
201
|
return parsed;
|
|
176
202
|
};
|
|
177
203
|
const selectParser = (schema, refs) => {
|
|
178
|
-
if (its.a.nullable(schema)) {
|
|
179
|
-
return parseNullable(schema, refs);
|
|
204
|
+
if (exports.its.a.nullable(schema)) {
|
|
205
|
+
return (0, parseNullable_js_1.parseNullable)(schema, refs);
|
|
180
206
|
}
|
|
181
|
-
else if (its.an.object(schema)) {
|
|
182
|
-
return parseObject(schema, refs);
|
|
207
|
+
else if (exports.its.an.object(schema)) {
|
|
208
|
+
return (0, parseObject_js_1.parseObject)(schema, refs);
|
|
183
209
|
}
|
|
184
|
-
else if (its.an.array(schema)) {
|
|
185
|
-
return parseArray(schema, refs);
|
|
210
|
+
else if (exports.its.an.array(schema)) {
|
|
211
|
+
return (0, parseArray_js_1.parseArray)(schema, refs);
|
|
186
212
|
}
|
|
187
|
-
else if (its.an.anyOf(schema)) {
|
|
188
|
-
return parseAnyOf(schema, refs);
|
|
213
|
+
else if (exports.its.an.anyOf(schema)) {
|
|
214
|
+
return (0, parseAnyOf_js_1.parseAnyOf)(schema, refs);
|
|
189
215
|
}
|
|
190
|
-
else if (its.an.allOf(schema)) {
|
|
191
|
-
return parseAllOf(schema, refs);
|
|
216
|
+
else if (exports.its.an.allOf(schema)) {
|
|
217
|
+
return (0, parseAllOf_js_1.parseAllOf)(schema, refs);
|
|
192
218
|
}
|
|
193
|
-
else if (its.a.simpleDiscriminatedOneOf(schema)) {
|
|
194
|
-
return parseSimpleDiscriminatedOneOf(schema, refs);
|
|
219
|
+
else if (exports.its.a.simpleDiscriminatedOneOf(schema)) {
|
|
220
|
+
return (0, parseSimpleDiscriminatedOneOf_js_1.parseSimpleDiscriminatedOneOf)(schema, refs);
|
|
195
221
|
}
|
|
196
|
-
else if (its.a.oneOf(schema)) {
|
|
197
|
-
return parseOneOf(schema, refs);
|
|
222
|
+
else if (exports.its.a.oneOf(schema)) {
|
|
223
|
+
return (0, parseOneOf_js_1.parseOneOf)(schema, refs);
|
|
198
224
|
}
|
|
199
|
-
else if (its.a.not(schema)) {
|
|
200
|
-
return parseNot(schema, refs);
|
|
225
|
+
else if (exports.its.a.not(schema)) {
|
|
226
|
+
return (0, parseNot_js_1.parseNot)(schema, refs);
|
|
201
227
|
}
|
|
202
|
-
else if (its.an.enum(schema)) {
|
|
203
|
-
return parseEnum(schema); //<-- needs to come before primitives
|
|
228
|
+
else if (exports.its.an.enum(schema)) {
|
|
229
|
+
return (0, parseEnum_js_1.parseEnum)(schema); //<-- needs to come before primitives
|
|
204
230
|
}
|
|
205
|
-
else if (its.a.const(schema)) {
|
|
206
|
-
return parseConst(schema);
|
|
231
|
+
else if (exports.its.a.const(schema)) {
|
|
232
|
+
return (0, parseConst_js_1.parseConst)(schema);
|
|
207
233
|
}
|
|
208
|
-
else if (its.a.multipleType(schema)) {
|
|
209
|
-
return parseMultipleType(schema, refs);
|
|
234
|
+
else if (exports.its.a.multipleType(schema)) {
|
|
235
|
+
return (0, parseMultipleType_js_1.parseMultipleType)(schema, refs);
|
|
210
236
|
}
|
|
211
|
-
else if (its.a.primitive(schema, "string")) {
|
|
212
|
-
return parseString(schema);
|
|
237
|
+
else if (exports.its.a.primitive(schema, "string")) {
|
|
238
|
+
return (0, parseString_js_1.parseString)(schema, refs);
|
|
213
239
|
}
|
|
214
|
-
else if (its.a.primitive(schema, "number") ||
|
|
215
|
-
its.a.primitive(schema, "integer")) {
|
|
216
|
-
return parseNumber(schema);
|
|
240
|
+
else if (exports.its.a.primitive(schema, "number") ||
|
|
241
|
+
exports.its.a.primitive(schema, "integer")) {
|
|
242
|
+
return (0, parseNumber_js_1.parseNumber)(schema);
|
|
217
243
|
}
|
|
218
|
-
else if (its.a.primitive(schema, "boolean")) {
|
|
219
|
-
return parseBoolean(schema);
|
|
244
|
+
else if (exports.its.a.primitive(schema, "boolean")) {
|
|
245
|
+
return (0, parseBoolean_js_1.parseBoolean)(schema);
|
|
220
246
|
}
|
|
221
|
-
else if (its.a.primitive(schema, "null")) {
|
|
222
|
-
return parseNull(schema);
|
|
247
|
+
else if (exports.its.a.primitive(schema, "null")) {
|
|
248
|
+
return (0, parseNull_js_1.parseNull)(schema);
|
|
223
249
|
}
|
|
224
|
-
else if (its.a.conditional(schema)) {
|
|
225
|
-
return parseIfThenElse(schema, refs);
|
|
250
|
+
else if (exports.its.a.conditional(schema)) {
|
|
251
|
+
return (0, parseIfThenElse_js_1.parseIfThenElse)(schema, refs);
|
|
226
252
|
}
|
|
227
253
|
else {
|
|
228
|
-
return parseDefault(schema, refs);
|
|
254
|
+
return (0, parseDefault_js_1.parseDefault)(schema, refs);
|
|
229
255
|
}
|
|
230
256
|
};
|
|
231
|
-
|
|
257
|
+
exports.its = {
|
|
232
258
|
an: {
|
|
233
259
|
object: (x) => x.type === "object",
|
|
234
260
|
array: (x) => x.type === "array",
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseSimpleDiscriminatedOneOf = void 0;
|
|
4
|
+
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
+
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
6
|
+
const parseSimpleDiscriminatedOneOf = (schema, refs) => {
|
|
4
7
|
const discriminator = schema.discriminator.propertyName;
|
|
5
|
-
const options = schema.oneOf.map((option, i) => parseSchema(option, {
|
|
8
|
+
const options = schema.oneOf.map((option, i) => (0, parseSchema_js_1.parseSchema)(option, {
|
|
6
9
|
...refs,
|
|
7
10
|
path: [...refs.path, "oneOf", i],
|
|
8
11
|
}));
|
|
9
12
|
return schema.oneOf.length
|
|
10
13
|
? schema.oneOf.length === 1
|
|
11
|
-
? parseSchema(schema.oneOf[0], {
|
|
14
|
+
? (0, parseSchema_js_1.parseSchema)(schema.oneOf[0], {
|
|
12
15
|
...refs,
|
|
13
16
|
path: [...refs.path, "oneOf", 0],
|
|
14
17
|
})
|
|
15
18
|
: `z.discriminatedUnion("${discriminator}", [${options.join(", ")}])`
|
|
16
|
-
: anyOrUnknown(refs);
|
|
19
|
+
: (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
17
20
|
};
|
|
21
|
+
exports.parseSimpleDiscriminatedOneOf = parseSimpleDiscriminatedOneOf;
|