@gabrielbryk/json-schema-to-zod 2.10.1 → 2.11.1
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/AGENTS.md +44 -0
- package/CHANGELOG.md +38 -0
- package/README.md +6 -33
- package/check-types-lift.sh +23 -0
- package/check-types.sh +20 -0
- package/dist/{esm/cli.js → cli.js} +0 -6
- package/dist/{esm/core → core}/analyzeSchema.js +4 -5
- package/dist/core/emitZod.js +263 -0
- package/dist/{esm/generators → generators}/generateBundle.js +26 -13
- package/dist/{esm/index.js → index.js} +6 -0
- package/dist/jsonSchemaToZod.js +17 -0
- package/dist/parsers/parseAllOf.js +125 -0
- package/dist/parsers/parseAnyOf.js +28 -0
- package/dist/{esm/parsers → parsers}/parseArray.js +27 -11
- package/dist/parsers/parseBoolean.js +4 -0
- package/dist/parsers/parseConst.js +22 -0
- package/dist/parsers/parseEnum.js +35 -0
- package/dist/{esm/parsers → parsers}/parseIfThenElse.js +10 -6
- package/dist/parsers/parseMultipleType.js +10 -0
- package/dist/parsers/parseNot.js +14 -0
- package/dist/parsers/parseNull.js +4 -0
- package/dist/parsers/parseNullable.js +12 -0
- package/dist/{esm/parsers → parsers}/parseNumber.js +4 -1
- package/dist/{esm/parsers → parsers}/parseObject.js +200 -37
- package/dist/parsers/parseOneOf.js +365 -0
- package/dist/{esm/parsers → parsers}/parseSchema.js +55 -117
- package/dist/parsers/parseSimpleDiscriminatedOneOf.js +24 -0
- package/dist/{esm/parsers → parsers}/parseString.js +29 -18
- package/dist/types/Types.d.ts +32 -4
- package/dist/types/core/analyzeSchema.d.ts +3 -2
- package/dist/types/generators/generateBundle.d.ts +0 -2
- package/dist/types/index.d.ts +6 -0
- package/dist/types/parsers/parseAllOf.d.ts +2 -2
- package/dist/types/parsers/parseAnyOf.d.ts +2 -2
- package/dist/types/parsers/parseArray.d.ts +2 -2
- package/dist/types/parsers/parseBoolean.d.ts +2 -1
- package/dist/types/parsers/parseConst.d.ts +2 -2
- package/dist/types/parsers/parseDefault.d.ts +2 -2
- package/dist/types/parsers/parseEnum.d.ts +2 -2
- package/dist/types/parsers/parseIfThenElse.d.ts +2 -2
- package/dist/types/parsers/parseMultipleType.d.ts +2 -2
- package/dist/types/parsers/parseNot.d.ts +2 -2
- package/dist/types/parsers/parseNull.d.ts +2 -1
- package/dist/types/parsers/parseNullable.d.ts +2 -2
- package/dist/types/parsers/parseNumber.d.ts +2 -2
- package/dist/types/parsers/parseObject.d.ts +2 -2
- package/dist/types/parsers/parseOneOf.d.ts +2 -2
- package/dist/types/parsers/parseSchema.d.ts +2 -2
- package/dist/types/parsers/parseSimpleDiscriminatedOneOf.d.ts +2 -2
- package/dist/types/parsers/parseString.d.ts +2 -2
- package/dist/types/utils/anyOrUnknown.d.ts +5 -4
- package/dist/types/utils/esmEmitter.d.ts +29 -0
- package/dist/types/utils/extractInlineObject.d.ts +15 -0
- package/dist/types/utils/liftInlineObjects.d.ts +21 -0
- package/dist/types/utils/namingService.d.ts +21 -0
- package/dist/types/utils/resolveRef.d.ts +7 -0
- package/dist/types/utils/schemaRepresentation.d.ts +71 -0
- package/dist/utils/anyOrUnknown.js +13 -0
- package/dist/{esm/utils → utils}/buildRefRegistry.js +4 -0
- package/dist/utils/esmEmitter.js +87 -0
- package/dist/utils/extractInlineObject.js +119 -0
- package/dist/utils/liftInlineObjects.js +476 -0
- package/dist/utils/namingService.js +58 -0
- package/dist/utils/resolveRef.js +92 -0
- package/dist/utils/schemaRepresentation.js +569 -0
- package/docs/IMPROVEMENT-PLAN.md +243 -0
- package/docs/ZOD-V4-RECURSIVE-TYPE-LIMITATIONS.md +292 -0
- package/docs/proposals/bundle-refactor.md +1 -1
- package/docs/proposals/discriminated-union-with-default.md +248 -0
- package/docs/proposals/inline-object-lifting.md +77 -0
- package/eslint.config.js +4 -2
- package/jest.config.mjs +19 -0
- package/package.json +17 -20
- package/scripts/generateWorkflowSchema.ts +0 -1
- package/dist/cjs/Types.js +0 -2
- package/dist/cjs/cli.js +0 -70
- package/dist/cjs/core/analyzeSchema.js +0 -62
- package/dist/cjs/core/emitZod.js +0 -157
- package/dist/cjs/generators/generateBundle.js +0 -510
- package/dist/cjs/index.js +0 -50
- package/dist/cjs/jsonSchemaToZod.js +0 -10
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/parsers/parseAllOf.js +0 -46
- package/dist/cjs/parsers/parseAnyOf.js +0 -18
- package/dist/cjs/parsers/parseArray.js +0 -90
- package/dist/cjs/parsers/parseBoolean.js +0 -5
- package/dist/cjs/parsers/parseConst.js +0 -7
- package/dist/cjs/parsers/parseDefault.js +0 -8
- package/dist/cjs/parsers/parseEnum.js +0 -21
- package/dist/cjs/parsers/parseIfThenElse.js +0 -35
- package/dist/cjs/parsers/parseMultipleType.js +0 -10
- package/dist/cjs/parsers/parseNot.js +0 -12
- package/dist/cjs/parsers/parseNull.js +0 -5
- package/dist/cjs/parsers/parseNullable.js +0 -12
- package/dist/cjs/parsers/parseNumber.js +0 -116
- package/dist/cjs/parsers/parseObject.js +0 -318
- package/dist/cjs/parsers/parseOneOf.js +0 -53
- package/dist/cjs/parsers/parseSchema.js +0 -419
- package/dist/cjs/parsers/parseSimpleDiscriminatedOneOf.js +0 -21
- package/dist/cjs/parsers/parseString.js +0 -317
- package/dist/cjs/utils/anyOrUnknown.js +0 -14
- package/dist/cjs/utils/buildRefRegistry.js +0 -56
- package/dist/cjs/utils/cliTools.js +0 -108
- package/dist/cjs/utils/cycles.js +0 -113
- package/dist/cjs/utils/half.js +0 -7
- package/dist/cjs/utils/jsdocs.js +0 -20
- package/dist/cjs/utils/omit.js +0 -11
- package/dist/cjs/utils/resolveUri.js +0 -16
- package/dist/cjs/utils/withMessage.js +0 -21
- package/dist/cjs/zodToJsonSchema.js +0 -89
- package/dist/esm/core/emitZod.js +0 -153
- package/dist/esm/jsonSchemaToZod.js +0 -6
- package/dist/esm/package.json +0 -1
- package/dist/esm/parsers/parseAllOf.js +0 -43
- package/dist/esm/parsers/parseAnyOf.js +0 -14
- package/dist/esm/parsers/parseBoolean.js +0 -1
- package/dist/esm/parsers/parseConst.js +0 -3
- package/dist/esm/parsers/parseEnum.js +0 -17
- package/dist/esm/parsers/parseMultipleType.js +0 -6
- package/dist/esm/parsers/parseNot.js +0 -8
- package/dist/esm/parsers/parseNull.js +0 -1
- package/dist/esm/parsers/parseNullable.js +0 -8
- package/dist/esm/parsers/parseOneOf.js +0 -49
- package/dist/esm/parsers/parseSimpleDiscriminatedOneOf.js +0 -17
- package/dist/esm/utils/anyOrUnknown.js +0 -10
- package/jest.config.cjs +0 -4
- package/postcjs.cjs +0 -1
- package/postesm.cjs +0 -1
- /package/dist/{esm/Types.js → Types.js} +0 -0
- /package/dist/{esm/parsers → parsers}/parseDefault.js +0 -0
- /package/dist/{esm/utils → utils}/cliTools.js +0 -0
- /package/dist/{esm/utils → utils}/cycles.js +0 -0
- /package/dist/{esm/utils → utils}/half.js +0 -0
- /package/dist/{esm/utils → utils}/jsdocs.js +0 -0
- /package/dist/{esm/utils → utils}/omit.js +0 -0
- /package/dist/{esm/utils → utils}/resolveUri.js +0 -0
- /package/dist/{esm/utils → utils}/withMessage.js +0 -0
- /package/dist/{esm/zodToJsonSchema.js → zodToJsonSchema.js} +0 -0
|
@@ -1,318 +0,0 @@
|
|
|
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) {
|
|
12
|
-
let properties = undefined;
|
|
13
|
-
if (objectSchema.properties) {
|
|
14
|
-
if (!Object.keys(objectSchema.properties).length) {
|
|
15
|
-
properties = "z.object({})";
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
properties = "z.object({ ";
|
|
19
|
-
properties += Object.keys(objectSchema.properties)
|
|
20
|
-
.map((key) => {
|
|
21
|
-
const propSchema = objectSchema.properties[key];
|
|
22
|
-
const parsedProp = (0, parseSchema_js_1.parseSchema)(propSchema, {
|
|
23
|
-
...refs,
|
|
24
|
-
path: [...refs.path, "properties", key],
|
|
25
|
-
});
|
|
26
|
-
const hasDefault = typeof propSchema === "object" && propSchema.default !== undefined;
|
|
27
|
-
const required = Array.isArray(objectSchema.required)
|
|
28
|
-
? objectSchema.required.includes(key)
|
|
29
|
-
: typeof propSchema === "object" && propSchema.required === true;
|
|
30
|
-
const optional = !hasDefault && !required;
|
|
31
|
-
const valueWithOptional = optional
|
|
32
|
-
? `${parsedProp}.optional()`
|
|
33
|
-
: parsedProp;
|
|
34
|
-
let result = shouldUseGetter(valueWithOptional, refs)
|
|
35
|
-
? `get ${JSON.stringify(key)}(){ return ${valueWithOptional} }`
|
|
36
|
-
: `${JSON.stringify(key)}: ${valueWithOptional}`;
|
|
37
|
-
if (refs.withJsdocs && typeof propSchema === "object") {
|
|
38
|
-
result = (0, jsdocs_js_1.addJsdocs)(propSchema, result);
|
|
39
|
-
}
|
|
40
|
-
return result;
|
|
41
|
-
})
|
|
42
|
-
.join(", ");
|
|
43
|
-
properties += " })";
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const additionalProperties = objectSchema.additionalProperties !== undefined
|
|
47
|
-
? (0, parseSchema_js_1.parseSchema)(objectSchema.additionalProperties, {
|
|
48
|
-
...refs,
|
|
49
|
-
path: [...refs.path, "additionalProperties"],
|
|
50
|
-
})
|
|
51
|
-
: undefined;
|
|
52
|
-
const unevaluated = objectSchema.unevaluatedProperties;
|
|
53
|
-
let patternProperties = undefined;
|
|
54
|
-
if (objectSchema.patternProperties) {
|
|
55
|
-
const parsedPatternProperties = Object.fromEntries(Object.entries(objectSchema.patternProperties).map(([key, value]) => {
|
|
56
|
-
return [
|
|
57
|
-
key,
|
|
58
|
-
(0, parseSchema_js_1.parseSchema)(value, {
|
|
59
|
-
...refs,
|
|
60
|
-
path: [...refs.path, "patternProperties", key],
|
|
61
|
-
}),
|
|
62
|
-
];
|
|
63
|
-
}, {}));
|
|
64
|
-
patternProperties = "";
|
|
65
|
-
if (properties) {
|
|
66
|
-
if (additionalProperties) {
|
|
67
|
-
patternProperties += `.catchall(z.union([${[
|
|
68
|
-
...Object.values(parsedPatternProperties),
|
|
69
|
-
additionalProperties,
|
|
70
|
-
].join(", ")}]))`;
|
|
71
|
-
}
|
|
72
|
-
else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
73
|
-
patternProperties += `.catchall(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
patternProperties += `.catchall(${Object.values(parsedPatternProperties)})`;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
if (additionalProperties) {
|
|
81
|
-
patternProperties += `z.record(z.string(), z.union([${[
|
|
82
|
-
...Object.values(parsedPatternProperties),
|
|
83
|
-
additionalProperties,
|
|
84
|
-
].join(", ")}]))`;
|
|
85
|
-
}
|
|
86
|
-
else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
87
|
-
patternProperties += `z.record(z.string(), z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
patternProperties += `z.record(z.string(), ${Object.values(parsedPatternProperties)})`;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
patternProperties += ".superRefine((value, ctx) => {\n";
|
|
94
|
-
patternProperties += "for (const key in value) {\n";
|
|
95
|
-
if (additionalProperties) {
|
|
96
|
-
if (objectSchema.properties) {
|
|
97
|
-
patternProperties += `let evaluated = [${Object.keys(objectSchema.properties)
|
|
98
|
-
.map((key) => JSON.stringify(key))
|
|
99
|
-
.join(", ")}].includes(key)\n`;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
patternProperties += `let evaluated = false\n`;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
for (const key in objectSchema.patternProperties) {
|
|
106
|
-
patternProperties +=
|
|
107
|
-
"if (key.match(new RegExp(" + JSON.stringify(key) + "))) {\n";
|
|
108
|
-
if (additionalProperties) {
|
|
109
|
-
patternProperties += "evaluated = true\n";
|
|
110
|
-
}
|
|
111
|
-
patternProperties +=
|
|
112
|
-
"const result = " +
|
|
113
|
-
parsedPatternProperties[key] +
|
|
114
|
-
".safeParse(value[key])\n";
|
|
115
|
-
patternProperties += "if (!result.success) {\n";
|
|
116
|
-
patternProperties += `ctx.addIssue({
|
|
117
|
-
path: [...(ctx.path ?? []), key],
|
|
118
|
-
code: 'custom',
|
|
119
|
-
message: \`Invalid input: Key matching regex /\${key}/ must match schema\`,
|
|
120
|
-
params: {
|
|
121
|
-
issues: result.error.issues
|
|
122
|
-
}
|
|
123
|
-
})\n`;
|
|
124
|
-
patternProperties += "}\n";
|
|
125
|
-
patternProperties += "}\n";
|
|
126
|
-
}
|
|
127
|
-
if (additionalProperties) {
|
|
128
|
-
patternProperties += "if (!evaluated) {\n";
|
|
129
|
-
patternProperties +=
|
|
130
|
-
"const result = " + additionalProperties + ".safeParse(value[key])\n";
|
|
131
|
-
patternProperties += "if (!result.success) {\n";
|
|
132
|
-
patternProperties += `ctx.addIssue({
|
|
133
|
-
path: [...(ctx.path ?? []), key],
|
|
134
|
-
code: 'custom',
|
|
135
|
-
message: \`Invalid input: must match catchall schema\`,
|
|
136
|
-
params: {
|
|
137
|
-
issues: result.error.issues
|
|
138
|
-
}
|
|
139
|
-
})\n`;
|
|
140
|
-
patternProperties += "}\n";
|
|
141
|
-
patternProperties += "}\n";
|
|
142
|
-
}
|
|
143
|
-
patternProperties += "}\n";
|
|
144
|
-
patternProperties += "})";
|
|
145
|
-
// Store original patternProperties in meta for JSON Schema round-trip
|
|
146
|
-
if (refs.preserveJsonSchemaForRoundTrip) {
|
|
147
|
-
const patternPropsJson = JSON.stringify(Object.fromEntries(Object.entries(objectSchema.patternProperties).map(([pattern, schema]) => [
|
|
148
|
-
pattern,
|
|
149
|
-
schema
|
|
150
|
-
])));
|
|
151
|
-
patternProperties += `.meta({ __jsonSchema: { patternProperties: ${patternPropsJson} } })`;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
// Check if there will be an .and() call that adds properties from oneOf/anyOf/allOf/if-then-else
|
|
155
|
-
// In that case, we should NOT use .strict() because it will reject the additional keys
|
|
156
|
-
// before the union gets a chance to validate them.
|
|
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);
|
|
158
|
-
let output = properties
|
|
159
|
-
? patternProperties
|
|
160
|
-
? properties + patternProperties
|
|
161
|
-
: additionalProperties
|
|
162
|
-
? additionalProperties === "z.never()"
|
|
163
|
-
// Don't use .strict() if there are composition keywords that add properties
|
|
164
|
-
? hasCompositionKeywords
|
|
165
|
-
? properties
|
|
166
|
-
: properties + ".strict()"
|
|
167
|
-
: properties + `.catchall(${additionalProperties})`
|
|
168
|
-
: properties
|
|
169
|
-
: patternProperties
|
|
170
|
-
? patternProperties
|
|
171
|
-
: additionalProperties
|
|
172
|
-
? `z.record(z.string(), ${additionalProperties})`
|
|
173
|
-
: `z.record(z.string(), ${(0, anyOrUnknown_js_1.anyOrUnknown)(refs)})`;
|
|
174
|
-
if (unevaluated === false && properties && !hasCompositionKeywords) {
|
|
175
|
-
output += ".strict()";
|
|
176
|
-
}
|
|
177
|
-
else if (unevaluated && typeof unevaluated !== 'boolean') {
|
|
178
|
-
const unevaluatedSchema = (0, parseSchema_js_1.parseSchema)(unevaluated, {
|
|
179
|
-
...refs,
|
|
180
|
-
path: [...refs.path, "unevaluatedProperties"],
|
|
181
|
-
});
|
|
182
|
-
const knownKeys = objectSchema.properties ? Object.keys(objectSchema.properties) : [];
|
|
183
|
-
const patterns = objectSchema.patternProperties
|
|
184
|
-
? Object.keys(objectSchema.patternProperties).map((p) => new RegExp(p))
|
|
185
|
-
: [];
|
|
186
|
-
output += `.superRefine((value, ctx) => {
|
|
187
|
-
for (const key in value) {
|
|
188
|
-
const isKnown = ${JSON.stringify(knownKeys)}.includes(key);
|
|
189
|
-
const matchesPattern = ${patterns.length ? "[" + patterns.map((r) => r.toString()).join(",") + "]" : "[]"}.some((r) => r.test(key));
|
|
190
|
-
if (!isKnown && !matchesPattern) {
|
|
191
|
-
const result = ${unevaluatedSchema}.safeParse(value[key]);
|
|
192
|
-
if (!result.success) {
|
|
193
|
-
ctx.addIssue({ code: "custom", path: [key], message: "Invalid unevaluated property", params: { issues: result.error.issues } });
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
})`;
|
|
198
|
-
}
|
|
199
|
-
if (parseSchema_js_1.its.an.anyOf(objectSchema)) {
|
|
200
|
-
output += `.and(${(0, parseAnyOf_js_1.parseAnyOf)({
|
|
201
|
-
...objectSchema,
|
|
202
|
-
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" &&
|
|
203
|
-
x !== null &&
|
|
204
|
-
!x.type &&
|
|
205
|
-
(x.properties || x.additionalProperties || x.patternProperties)
|
|
206
|
-
? { ...x, type: "object" }
|
|
207
|
-
: x),
|
|
208
|
-
}, refs)})`;
|
|
209
|
-
}
|
|
210
|
-
if (parseSchema_js_1.its.a.oneOf(objectSchema)) {
|
|
211
|
-
output += `.and(${(0, parseOneOf_js_1.parseOneOf)({
|
|
212
|
-
...objectSchema,
|
|
213
|
-
oneOf: objectSchema.oneOf.map((x) => typeof x === "object" &&
|
|
214
|
-
x !== null &&
|
|
215
|
-
!x.type &&
|
|
216
|
-
(x.properties || x.additionalProperties || x.patternProperties)
|
|
217
|
-
? { ...x, type: "object" }
|
|
218
|
-
: x),
|
|
219
|
-
}, refs)})`;
|
|
220
|
-
}
|
|
221
|
-
if (parseSchema_js_1.its.an.allOf(objectSchema)) {
|
|
222
|
-
output += `.and(${(0, parseAllOf_js_1.parseAllOf)({
|
|
223
|
-
...objectSchema,
|
|
224
|
-
allOf: objectSchema.allOf.map((x) => typeof x === "object" &&
|
|
225
|
-
x !== null &&
|
|
226
|
-
!x.type &&
|
|
227
|
-
(x.properties || x.additionalProperties || x.patternProperties)
|
|
228
|
-
? { ...x, type: "object" }
|
|
229
|
-
: x),
|
|
230
|
-
}, refs)})`;
|
|
231
|
-
}
|
|
232
|
-
// Handle if/then/else conditionals on object schemas
|
|
233
|
-
if (parseSchema_js_1.its.a.conditional(objectSchema)) {
|
|
234
|
-
output += `.and(${(0, parseIfThenElse_js_1.parseIfThenElse)(objectSchema, refs)})`;
|
|
235
|
-
}
|
|
236
|
-
// propertyNames
|
|
237
|
-
if (objectSchema.propertyNames) {
|
|
238
|
-
const normalizedPropNames = typeof objectSchema.propertyNames === "object" &&
|
|
239
|
-
objectSchema.propertyNames !== null &&
|
|
240
|
-
!objectSchema.propertyNames.type &&
|
|
241
|
-
objectSchema.propertyNames.pattern
|
|
242
|
-
? { ...objectSchema.propertyNames, type: "string" }
|
|
243
|
-
: objectSchema.propertyNames;
|
|
244
|
-
const propNameSchema = (0, parseSchema_js_1.parseSchema)(normalizedPropNames, {
|
|
245
|
-
...refs,
|
|
246
|
-
path: [...refs.path, "propertyNames"],
|
|
247
|
-
});
|
|
248
|
-
output += `.superRefine((value, ctx) => {
|
|
249
|
-
for (const key in value) {
|
|
250
|
-
const result = ${propNameSchema}.safeParse(key);
|
|
251
|
-
if (!result.success) {
|
|
252
|
-
ctx.addIssue({
|
|
253
|
-
path: [key],
|
|
254
|
-
code: "custom",
|
|
255
|
-
message: "Invalid property name",
|
|
256
|
-
params: { issues: result.error.issues }
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
})`;
|
|
261
|
-
}
|
|
262
|
-
// dependentSchemas
|
|
263
|
-
if (objectSchema.dependentSchemas && typeof objectSchema.dependentSchemas === "object") {
|
|
264
|
-
const entries = Object.entries(objectSchema.dependentSchemas);
|
|
265
|
-
if (entries.length) {
|
|
266
|
-
output += `.superRefine((obj, ctx) => {
|
|
267
|
-
${entries
|
|
268
|
-
.map(([key, schema]) => {
|
|
269
|
-
const parsed = (0, parseSchema_js_1.parseSchema)(schema, { ...refs, path: [...refs.path, "dependentSchemas", key] });
|
|
270
|
-
return `if (Object.prototype.hasOwnProperty.call(obj, ${JSON.stringify(key)})) {
|
|
271
|
-
const result = ${parsed}.safeParse(obj);
|
|
272
|
-
if (!result.success) {
|
|
273
|
-
ctx.addIssue({ code: "custom", message: ${objectSchema.errorMessage?.dependentSchemas ?? JSON.stringify("Dependent schema failed")}, path: [], params: { issues: result.error.issues } });
|
|
274
|
-
}
|
|
275
|
-
}`;
|
|
276
|
-
})
|
|
277
|
-
.join("\n ")}
|
|
278
|
-
})`;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
// dependentRequired
|
|
282
|
-
if (objectSchema.dependentRequired && typeof objectSchema.dependentRequired === "object") {
|
|
283
|
-
const entries = Object.entries(objectSchema.dependentRequired);
|
|
284
|
-
if (entries.length) {
|
|
285
|
-
const depRequiredMessage = objectSchema.errorMessage?.dependentRequired ??
|
|
286
|
-
"Dependent required properties missing";
|
|
287
|
-
output += `.superRefine((obj, ctx) => {
|
|
288
|
-
${entries
|
|
289
|
-
.map(([prop, deps]) => {
|
|
290
|
-
const arr = Array.isArray(deps) ? deps : [];
|
|
291
|
-
if (!arr.length)
|
|
292
|
-
return "";
|
|
293
|
-
const jsonDeps = JSON.stringify(arr);
|
|
294
|
-
return `if (Object.prototype.hasOwnProperty.call(obj, ${JSON.stringify(prop)})) {
|
|
295
|
-
const missing = ${jsonDeps}.filter((d) => !Object.prototype.hasOwnProperty.call(obj, d));
|
|
296
|
-
if (missing.length) {
|
|
297
|
-
ctx.addIssue({ code: "custom", message: ${JSON.stringify(depRequiredMessage)}, path: [], params: { missing } });
|
|
298
|
-
}
|
|
299
|
-
}`;
|
|
300
|
-
})
|
|
301
|
-
.filter(Boolean)
|
|
302
|
-
.join("\n ")}
|
|
303
|
-
})`;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
return output;
|
|
307
|
-
}
|
|
308
|
-
const shouldUseGetter = (parsed, refs) => {
|
|
309
|
-
if (!parsed)
|
|
310
|
-
return false;
|
|
311
|
-
if (refs.currentSchemaName && parsed.includes(refs.currentSchemaName)) {
|
|
312
|
-
return true;
|
|
313
|
-
}
|
|
314
|
-
if (refs.cycleRefNames?.has(parsed)) {
|
|
315
|
-
return true;
|
|
316
|
-
}
|
|
317
|
-
return Boolean(refs.inProgress && refs.inProgress.has(parsed));
|
|
318
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
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) => {
|
|
7
|
-
if (!schema.oneOf.length) {
|
|
8
|
-
return (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
9
|
-
}
|
|
10
|
-
if (schema.oneOf.length === 1) {
|
|
11
|
-
return (0, parseSchema_js_1.parseSchema)(schema.oneOf[0], {
|
|
12
|
-
...refs,
|
|
13
|
-
path: [...refs.path, "oneOf", 0],
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
// Generate parsed schemas for each oneOf option
|
|
17
|
-
const parsedSchemas = schema.oneOf.map((s, i) => (0, parseSchema_js_1.parseSchema)(s, {
|
|
18
|
-
...refs,
|
|
19
|
-
path: [...refs.path, "oneOf", i],
|
|
20
|
-
}));
|
|
21
|
-
// JSON Schema oneOf = exactly one must match (exclusive OR)
|
|
22
|
-
// Zod union = at least one must match (inclusive OR)
|
|
23
|
-
//
|
|
24
|
-
// By default, use simple z.union() which provides "at least one must match".
|
|
25
|
-
// This is more practical for most use cases, as strict oneOf enforcement
|
|
26
|
-
// often fails when schemas have overlapping base types.
|
|
27
|
-
//
|
|
28
|
-
// If strictOneOf is enabled, add superRefine to enforce "exactly one" constraint.
|
|
29
|
-
if (refs.strictOneOf) {
|
|
30
|
-
return `z.union([${parsedSchemas.join(", ")}]).superRefine((x, ctx) => {
|
|
31
|
-
const schemas = [${parsedSchemas.join(", ")}];
|
|
32
|
-
const errors = schemas.reduce<z.ZodError[]>(
|
|
33
|
-
(errors, schema) =>
|
|
34
|
-
((result) =>
|
|
35
|
-
result.error ? [...errors, result.error] : errors)(
|
|
36
|
-
schema.safeParse(x),
|
|
37
|
-
),
|
|
38
|
-
[],
|
|
39
|
-
);
|
|
40
|
-
if (schemas.length - errors.length !== 1) {
|
|
41
|
-
ctx.addIssue({
|
|
42
|
-
path: [],
|
|
43
|
-
code: "invalid_union",
|
|
44
|
-
errors: errors.map(e => e.issues),
|
|
45
|
-
message: "Invalid input: Should pass single schema",
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
})`;
|
|
49
|
-
}
|
|
50
|
-
// Default: use simple z.union() (at least one must match)
|
|
51
|
-
return `z.union([${parsedSchemas.join(", ")}])`;
|
|
52
|
-
};
|
|
53
|
-
exports.parseOneOf = parseOneOf;
|