@gabrielbryk/json-schema-to-zod 2.7.2

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.
Files changed (99) hide show
  1. package/.prettierrc.json +3 -0
  2. package/CONTRIBUTING.md +9 -0
  3. package/LICENSE +15 -0
  4. package/README.md +157 -0
  5. package/createIndex.ts +32 -0
  6. package/dist/cjs/Types.js +2 -0
  7. package/dist/cjs/cli.js +70 -0
  8. package/dist/cjs/index.js +44 -0
  9. package/dist/cjs/jsonSchemaToZod.js +78 -0
  10. package/dist/cjs/package.json +1 -0
  11. package/dist/cjs/parsers/parseAllOf.js +40 -0
  12. package/dist/cjs/parsers/parseAnyOf.js +18 -0
  13. package/dist/cjs/parsers/parseArray.js +71 -0
  14. package/dist/cjs/parsers/parseBoolean.js +7 -0
  15. package/dist/cjs/parsers/parseConst.js +7 -0
  16. package/dist/cjs/parsers/parseDefault.js +8 -0
  17. package/dist/cjs/parsers/parseEnum.js +21 -0
  18. package/dist/cjs/parsers/parseIfThenElse.js +34 -0
  19. package/dist/cjs/parsers/parseMultipleType.js +10 -0
  20. package/dist/cjs/parsers/parseNot.js +12 -0
  21. package/dist/cjs/parsers/parseNull.js +7 -0
  22. package/dist/cjs/parsers/parseNullable.js +12 -0
  23. package/dist/cjs/parsers/parseNumber.js +74 -0
  24. package/dist/cjs/parsers/parseObject.js +286 -0
  25. package/dist/cjs/parsers/parseOneOf.js +53 -0
  26. package/dist/cjs/parsers/parseSchema.js +285 -0
  27. package/dist/cjs/parsers/parseSimpleDiscriminatedOneOf.js +29 -0
  28. package/dist/cjs/parsers/parseString.js +77 -0
  29. package/dist/cjs/utils/anyOrUnknown.js +14 -0
  30. package/dist/cjs/utils/cliTools.js +108 -0
  31. package/dist/cjs/utils/half.js +7 -0
  32. package/dist/cjs/utils/jsdocs.js +20 -0
  33. package/dist/cjs/utils/omit.js +10 -0
  34. package/dist/cjs/utils/withMessage.js +22 -0
  35. package/dist/cjs/zodToJsonSchema.js +89 -0
  36. package/dist/esm/Types.js +1 -0
  37. package/dist/esm/cli.js +68 -0
  38. package/dist/esm/index.js +28 -0
  39. package/dist/esm/jsonSchemaToZod.js +74 -0
  40. package/dist/esm/package.json +1 -0
  41. package/dist/esm/parsers/parseAllOf.js +37 -0
  42. package/dist/esm/parsers/parseAnyOf.js +14 -0
  43. package/dist/esm/parsers/parseArray.js +67 -0
  44. package/dist/esm/parsers/parseBoolean.js +3 -0
  45. package/dist/esm/parsers/parseConst.js +3 -0
  46. package/dist/esm/parsers/parseDefault.js +4 -0
  47. package/dist/esm/parsers/parseEnum.js +17 -0
  48. package/dist/esm/parsers/parseIfThenElse.js +30 -0
  49. package/dist/esm/parsers/parseMultipleType.js +6 -0
  50. package/dist/esm/parsers/parseNot.js +8 -0
  51. package/dist/esm/parsers/parseNull.js +3 -0
  52. package/dist/esm/parsers/parseNullable.js +8 -0
  53. package/dist/esm/parsers/parseNumber.js +70 -0
  54. package/dist/esm/parsers/parseObject.js +283 -0
  55. package/dist/esm/parsers/parseOneOf.js +49 -0
  56. package/dist/esm/parsers/parseSchema.js +281 -0
  57. package/dist/esm/parsers/parseSimpleDiscriminatedOneOf.js +25 -0
  58. package/dist/esm/parsers/parseString.js +73 -0
  59. package/dist/esm/utils/anyOrUnknown.js +10 -0
  60. package/dist/esm/utils/cliTools.js +102 -0
  61. package/dist/esm/utils/half.js +3 -0
  62. package/dist/esm/utils/jsdocs.js +15 -0
  63. package/dist/esm/utils/omit.js +6 -0
  64. package/dist/esm/utils/withMessage.js +19 -0
  65. package/dist/esm/zodToJsonSchema.js +86 -0
  66. package/dist/types/Types.d.ts +125 -0
  67. package/dist/types/cli.d.ts +2 -0
  68. package/dist/types/index.d.ts +28 -0
  69. package/dist/types/jsonSchemaToZod.d.ts +2 -0
  70. package/dist/types/parsers/parseAllOf.d.ts +4 -0
  71. package/dist/types/parsers/parseAnyOf.d.ts +4 -0
  72. package/dist/types/parsers/parseArray.d.ts +4 -0
  73. package/dist/types/parsers/parseBoolean.d.ts +4 -0
  74. package/dist/types/parsers/parseConst.d.ts +4 -0
  75. package/dist/types/parsers/parseDefault.d.ts +2 -0
  76. package/dist/types/parsers/parseEnum.d.ts +4 -0
  77. package/dist/types/parsers/parseIfThenElse.d.ts +6 -0
  78. package/dist/types/parsers/parseMultipleType.d.ts +4 -0
  79. package/dist/types/parsers/parseNot.d.ts +4 -0
  80. package/dist/types/parsers/parseNull.d.ts +4 -0
  81. package/dist/types/parsers/parseNullable.d.ts +7 -0
  82. package/dist/types/parsers/parseNumber.d.ts +4 -0
  83. package/dist/types/parsers/parseObject.d.ts +4 -0
  84. package/dist/types/parsers/parseOneOf.d.ts +4 -0
  85. package/dist/types/parsers/parseSchema.d.ts +50 -0
  86. package/dist/types/parsers/parseSimpleDiscriminatedOneOf.d.ts +2 -0
  87. package/dist/types/parsers/parseString.d.ts +4 -0
  88. package/dist/types/utils/anyOrUnknown.d.ts +9 -0
  89. package/dist/types/utils/cliTools.d.ts +28 -0
  90. package/dist/types/utils/half.d.ts +1 -0
  91. package/dist/types/utils/jsdocs.d.ts +3 -0
  92. package/dist/types/utils/omit.d.ts +1 -0
  93. package/dist/types/utils/withMessage.d.ts +10 -0
  94. package/dist/types/zodToJsonSchema.d.ts +26 -0
  95. package/jest.config.js +4 -0
  96. package/package.json +83 -0
  97. package/postcjs.js +1 -0
  98. package/postesm.js +1 -0
  99. package/scripts/generateWorkflowSchema.ts +82 -0
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNull = void 0;
4
+ const parseNull = (_schema) => {
5
+ return "z.null()";
6
+ };
7
+ exports.parseNull = parseNull;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNullable = void 0;
4
+ const omit_js_1 = require("../utils/omit.js");
5
+ const parseSchema_js_1 = require("./parseSchema.js");
6
+ /**
7
+ * For compatibility with open api 3.0 nullable
8
+ */
9
+ const parseNullable = (schema, refs) => {
10
+ return `${(0, parseSchema_js_1.parseSchema)((0, omit_js_1.omit)(schema, "nullable"), refs, true)}.nullable()`;
11
+ };
12
+ exports.parseNullable = parseNullable;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNumber = void 0;
4
+ const withMessage_js_1 = require("../utils/withMessage.js");
5
+ const parseNumber = (schema) => {
6
+ let r = "z.number()";
7
+ if (schema.type === "integer") {
8
+ r += (0, withMessage_js_1.withMessage)(schema, "type", () => [".int(", ")"]);
9
+ }
10
+ else {
11
+ r += (0, withMessage_js_1.withMessage)(schema, "format", ({ value }) => {
12
+ if (value === "int64") {
13
+ return [".int(", ")"];
14
+ }
15
+ });
16
+ }
17
+ r += (0, withMessage_js_1.withMessage)(schema, "multipleOf", ({ value, json }) => {
18
+ if (value === 1) {
19
+ if (r.startsWith("z.number().int(")) {
20
+ return;
21
+ }
22
+ return [".int(", ")"];
23
+ }
24
+ return [`.multipleOf(${json}`, ", ", ")"];
25
+ });
26
+ if (typeof schema.minimum === "number") {
27
+ if (schema.exclusiveMinimum === true) {
28
+ r += (0, withMessage_js_1.withMessage)(schema, "minimum", ({ json }) => [
29
+ `.gt(${json}`,
30
+ ", ",
31
+ ")",
32
+ ]);
33
+ }
34
+ else {
35
+ r += (0, withMessage_js_1.withMessage)(schema, "minimum", ({ json }) => [
36
+ `.gte(${json}`,
37
+ ", ",
38
+ ")",
39
+ ]);
40
+ }
41
+ }
42
+ else if (typeof schema.exclusiveMinimum === "number") {
43
+ r += (0, withMessage_js_1.withMessage)(schema, "exclusiveMinimum", ({ json }) => [
44
+ `.gt(${json}`,
45
+ ", ",
46
+ ")",
47
+ ]);
48
+ }
49
+ if (typeof schema.maximum === "number") {
50
+ if (schema.exclusiveMaximum === true) {
51
+ r += (0, withMessage_js_1.withMessage)(schema, "maximum", ({ json }) => [
52
+ `.lt(${json}`,
53
+ ", ",
54
+ ")",
55
+ ]);
56
+ }
57
+ else {
58
+ r += (0, withMessage_js_1.withMessage)(schema, "maximum", ({ json }) => [
59
+ `.lte(${json}`,
60
+ ", ",
61
+ ")",
62
+ ]);
63
+ }
64
+ }
65
+ else if (typeof schema.exclusiveMaximum === "number") {
66
+ r += (0, withMessage_js_1.withMessage)(schema, "exclusiveMaximum", ({ json }) => [
67
+ `.lt(${json}`,
68
+ ", ",
69
+ ")",
70
+ ]);
71
+ }
72
+ return r;
73
+ };
74
+ exports.parseNumber = parseNumber;
@@ -0,0 +1,286 @@
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: [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: [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
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);
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.type &&
204
+ (x.properties || x.additionalProperties || x.patternProperties)
205
+ ? { ...x, type: "object" }
206
+ : x),
207
+ }, refs)})`;
208
+ }
209
+ if (parseSchema_js_1.its.a.oneOf(objectSchema)) {
210
+ output += `.and(${(0, parseOneOf_js_1.parseOneOf)({
211
+ ...objectSchema,
212
+ oneOf: objectSchema.oneOf.map((x) => typeof x === "object" &&
213
+ !x.type &&
214
+ (x.properties || x.additionalProperties || x.patternProperties)
215
+ ? { ...x, type: "object" }
216
+ : x),
217
+ }, refs)})`;
218
+ }
219
+ if (parseSchema_js_1.its.an.allOf(objectSchema)) {
220
+ output += `.and(${(0, parseAllOf_js_1.parseAllOf)({
221
+ ...objectSchema,
222
+ allOf: objectSchema.allOf.map((x) => typeof x === "object" &&
223
+ !x.type &&
224
+ (x.properties || x.additionalProperties || x.patternProperties)
225
+ ? { ...x, type: "object" }
226
+ : x),
227
+ }, refs)})`;
228
+ }
229
+ // Handle if/then/else conditionals on object schemas
230
+ if (parseSchema_js_1.its.a.conditional(objectSchema)) {
231
+ output += `.and(${(0, parseIfThenElse_js_1.parseIfThenElse)(objectSchema, refs)})`;
232
+ }
233
+ // propertyNames
234
+ if (objectSchema.propertyNames) {
235
+ const normalizedPropNames = typeof objectSchema.propertyNames === "object" &&
236
+ !objectSchema.propertyNames.type &&
237
+ objectSchema.propertyNames.pattern
238
+ ? { ...objectSchema.propertyNames, type: "string" }
239
+ : objectSchema.propertyNames;
240
+ const propNameSchema = (0, parseSchema_js_1.parseSchema)(normalizedPropNames, {
241
+ ...refs,
242
+ path: [...refs.path, "propertyNames"],
243
+ });
244
+ output += `.superRefine((value, ctx) => {
245
+ for (const key in value) {
246
+ const result = ${propNameSchema}.safeParse(key);
247
+ if (!result.success) {
248
+ ctx.addIssue({
249
+ path: [key],
250
+ code: "custom",
251
+ message: "Invalid property name",
252
+ params: { issues: result.error.issues }
253
+ });
254
+ }
255
+ }
256
+ })`;
257
+ }
258
+ // dependentSchemas
259
+ if (objectSchema.dependentSchemas && typeof objectSchema.dependentSchemas === "object") {
260
+ const entries = Object.entries(objectSchema.dependentSchemas);
261
+ if (entries.length) {
262
+ output += `.superRefine((obj, ctx) => {
263
+ ${entries
264
+ .map(([key, schema], idx) => {
265
+ const parsed = (0, parseSchema_js_1.parseSchema)(schema, { ...refs, path: [...refs.path, "dependentSchemas", key] });
266
+ return `if (Object.prototype.hasOwnProperty.call(obj, ${JSON.stringify(key)})) {
267
+ const result = ${parsed}.safeParse(obj);
268
+ if (!result.success) {
269
+ ctx.addIssue({ code: "custom", message: "Dependent schema failed", path: [], params: { issues: result.error.issues } });
270
+ }
271
+ }`;
272
+ })
273
+ .join("\n ")}
274
+ })`;
275
+ }
276
+ }
277
+ return output;
278
+ }
279
+ const shouldUseGetter = (parsed, refs) => {
280
+ if (!parsed)
281
+ return false;
282
+ if (refs.currentSchemaName && parsed.includes(refs.currentSchemaName)) {
283
+ return true;
284
+ }
285
+ return Boolean(refs.inProgress && refs.inProgress.has(parsed));
286
+ };
@@ -0,0 +1,53 @@
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;