@composio/json-schema-to-zod 0.1.19 → 0.1.20

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/dist/index.cjs CHANGED
@@ -1,830 +1,656 @@
1
- "use strict";
1
+ //#region rolldown:runtime
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
8
  var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
19
21
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- jsonSchemaToZod: () => jsonSchemaToZod
34
- });
35
- module.exports = __toCommonJS(index_exports);
36
-
37
- // src/parsers/parse-schema.ts
38
- var z16 = __toESM(require("zod/v3"), 1);
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
39
26
 
40
- // src/parsers/parse-all-of.ts
41
- var import_v3 = require("zod/v3");
27
+ //#endregion
28
+ let zod_v3 = require("zod/v3");
29
+ zod_v3 = __toESM(zod_v3);
42
30
 
43
- // src/utils/half.ts
44
- var half = (arr) => {
45
- return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
31
+ //#region src/utils/half.ts
32
+ const half = (arr) => {
33
+ return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
46
34
  };
47
35
 
48
- // src/parsers/parse-all-of.ts
49
- var originalIndex = Symbol("Original index");
50
- var ensureOriginalIndex = (arr) => {
51
- const newArr = [];
52
- for (let i = 0; i < arr.length; i++) {
53
- const item = arr[i];
54
- if (typeof item === "boolean") {
55
- newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
56
- } else if (originalIndex in item) {
57
- return arr;
58
- } else {
59
- newArr.push({ ...item, [originalIndex]: i });
60
- }
61
- }
62
- return newArr;
36
+ //#endregion
37
+ //#region src/parsers/parse-all-of.ts
38
+ const originalIndex = Symbol("Original index");
39
+ const ensureOriginalIndex = (arr) => {
40
+ const newArr = [];
41
+ for (let i = 0; i < arr.length; i++) {
42
+ const item = arr[i];
43
+ if (typeof item === "boolean") newArr.push(item ? { [originalIndex]: i } : {
44
+ [originalIndex]: i,
45
+ not: {}
46
+ });
47
+ else if (originalIndex in item) return arr;
48
+ else newArr.push({
49
+ ...item,
50
+ [originalIndex]: i
51
+ });
52
+ }
53
+ return newArr;
63
54
  };
64
55
  function parseAllOf(jsonSchema, refs) {
65
- if (jsonSchema.allOf.length === 0) {
66
- return import_v3.z.never();
67
- }
68
- if (jsonSchema.allOf.length === 1) {
69
- const item = jsonSchema.allOf[0];
70
- return parseSchema(item, {
71
- ...refs,
72
- path: [...refs.path, "allOf", item[originalIndex]]
73
- });
74
- }
75
- const [left, right] = half(ensureOriginalIndex(jsonSchema.allOf));
76
- return import_v3.z.intersection(parseAllOf({ allOf: left }, refs), parseAllOf({ allOf: right }, refs));
56
+ if (jsonSchema.allOf.length === 0) return zod_v3.z.never();
57
+ if (jsonSchema.allOf.length === 1) {
58
+ const item = jsonSchema.allOf[0];
59
+ return parseSchema(item, {
60
+ ...refs,
61
+ path: [
62
+ ...refs.path,
63
+ "allOf",
64
+ item[originalIndex]
65
+ ]
66
+ });
67
+ }
68
+ const [left, right] = half(ensureOriginalIndex(jsonSchema.allOf));
69
+ return zod_v3.z.intersection(parseAllOf({ allOf: left }, refs), parseAllOf({ allOf: right }, refs));
77
70
  }
78
71
 
79
- // src/parsers/parse-any-of.ts
80
- var import_v32 = require("zod/v3");
81
- var parseAnyOf = (jsonSchema, refs) => {
82
- return jsonSchema.anyOf.length ? jsonSchema.anyOf.length === 1 ? parseSchema(jsonSchema.anyOf[0], {
83
- ...refs,
84
- path: [...refs.path, "anyOf", 0]
85
- }) : import_v32.z.union(
86
- jsonSchema.anyOf.map(
87
- (schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] })
88
- )
89
- ) : import_v32.z.any();
72
+ //#endregion
73
+ //#region src/parsers/parse-any-of.ts
74
+ const parseAnyOf = (jsonSchema, refs) => {
75
+ return jsonSchema.anyOf.length ? jsonSchema.anyOf.length === 1 ? parseSchema(jsonSchema.anyOf[0], {
76
+ ...refs,
77
+ path: [
78
+ ...refs.path,
79
+ "anyOf",
80
+ 0
81
+ ]
82
+ }) : zod_v3.z.union(jsonSchema.anyOf.map((schema, i) => parseSchema(schema, {
83
+ ...refs,
84
+ path: [
85
+ ...refs.path,
86
+ "anyOf",
87
+ i
88
+ ]
89
+ }))) : zod_v3.z.any();
90
90
  };
91
91
 
92
- // src/parsers/parse-array.ts
93
- var import_v33 = require("zod/v3");
94
-
95
- // src/utils/extend-schema.ts
92
+ //#endregion
93
+ //#region src/utils/extend-schema.ts
96
94
  function extendSchemaWithMessage(zodSchema, jsonSchema, key, extend) {
97
- const value = jsonSchema[key];
98
- if (value !== void 0) {
99
- const errorMessage = jsonSchema.errorMessage?.[key];
100
- return extend(zodSchema, value, errorMessage);
101
- }
102
- return zodSchema;
95
+ const value = jsonSchema[key];
96
+ if (value !== void 0) {
97
+ const errorMessage = jsonSchema.errorMessage?.[key];
98
+ return extend(zodSchema, value, errorMessage);
99
+ }
100
+ return zodSchema;
103
101
  }
104
102
 
105
- // src/utils/its.ts
106
- var its = {
107
- an: {
108
- object: (x) => x.type === "object" || !x.type && (x.properties !== void 0 || x.additionalProperties !== void 0 || x.patternProperties !== void 0),
109
- array: (x) => x.type === "array",
110
- anyOf: (x) => x.anyOf !== void 0,
111
- allOf: (x) => x.allOf !== void 0,
112
- enum: (x) => x.enum !== void 0
113
- },
114
- a: {
115
- nullable: (x) => x.nullable === true,
116
- multipleType: (x) => Array.isArray(x.type),
117
- not: (x) => x.not !== void 0,
118
- const: (x) => x.const !== void 0,
119
- primitive: (x, p) => x.type === p,
120
- conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
121
- oneOf: (x) => x.oneOf !== void 0
122
- }
103
+ //#endregion
104
+ //#region src/utils/its.ts
105
+ const its = {
106
+ an: {
107
+ object: (x) => x.type === "object" || !x.type && (x.properties !== void 0 || x.additionalProperties !== void 0 || x.patternProperties !== void 0),
108
+ array: (x) => x.type === "array",
109
+ anyOf: (x) => x.anyOf !== void 0,
110
+ allOf: (x) => x.allOf !== void 0,
111
+ enum: (x) => x.enum !== void 0
112
+ },
113
+ a: {
114
+ nullable: (x) => x.nullable === true,
115
+ multipleType: (x) => Array.isArray(x.type),
116
+ not: (x) => x.not !== void 0,
117
+ const: (x) => x.const !== void 0,
118
+ primitive: (x, p) => x.type === p,
119
+ conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
120
+ oneOf: (x) => x.oneOf !== void 0
121
+ }
123
122
  };
124
123
 
125
- // src/parsers/parse-array.ts
126
- var parseArray = (jsonSchema, refs) => {
127
- if (its.an.anyOf(jsonSchema)) {
128
- const types = /* @__PURE__ */ new Set();
129
- const itemsSchemas = [];
130
- jsonSchema.anyOf.forEach((option) => {
131
- if (typeof option === "object" && option.type) {
132
- types.add(typeof option.type === "string" ? option.type : option.type[0]);
133
- }
134
- if (typeof option === "object" && option.items) {
135
- const optionItems = option.items;
136
- if (!Array.isArray(optionItems) && typeof optionItems === "object") {
137
- itemsSchemas.push(optionItems);
138
- }
139
- }
140
- });
141
- let finalItems;
142
- if (itemsSchemas.length === 1) {
143
- finalItems = itemsSchemas[0];
144
- } else if (itemsSchemas.length > 1) {
145
- finalItems = { anyOf: itemsSchemas };
146
- }
147
- const newSchema = {
148
- ...types.size > 0 ? { type: Array.from(types) } : { type: "array" },
149
- ...finalItems && { items: finalItems }
150
- };
151
- const metadataFields = ["default", "description", "examples", "title"];
152
- metadataFields.forEach((field) => {
153
- const value = jsonSchema[field];
154
- if (value !== void 0) {
155
- newSchema[field] = value;
156
- }
157
- });
158
- return parseSchema(newSchema, refs);
159
- }
160
- if (Array.isArray(jsonSchema.items)) {
161
- return import_v33.z.tuple(
162
- jsonSchema.items.map(
163
- (v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] })
164
- )
165
- );
166
- }
167
- let zodSchema = !jsonSchema.items ? import_v33.z.array(import_v33.z.any()) : import_v33.z.array(parseSchema(jsonSchema.items, { ...refs, path: [...refs.path, "items"] }));
168
- zodSchema = extendSchemaWithMessage(
169
- zodSchema,
170
- jsonSchema,
171
- "minItems",
172
- (zs, minItems, errorMessage) => zs.min(minItems, errorMessage)
173
- );
174
- zodSchema = extendSchemaWithMessage(
175
- zodSchema,
176
- jsonSchema,
177
- "maxItems",
178
- (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage)
179
- );
180
- if (typeof jsonSchema.min === "number" && typeof jsonSchema.minItems !== "number") {
181
- zodSchema = extendSchemaWithMessage(
182
- zodSchema,
183
- { ...jsonSchema, minItems: jsonSchema.min },
184
- "minItems",
185
- (zs, minItems, errorMessage) => zs.min(minItems, errorMessage)
186
- );
187
- }
188
- if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxItems !== "number") {
189
- zodSchema = extendSchemaWithMessage(
190
- zodSchema,
191
- { ...jsonSchema, maxItems: jsonSchema.max },
192
- "maxItems",
193
- (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage)
194
- );
195
- }
196
- return zodSchema;
124
+ //#endregion
125
+ //#region src/parsers/parse-array.ts
126
+ const parseArray = (jsonSchema, refs) => {
127
+ if (its.an.anyOf(jsonSchema)) {
128
+ const types = /* @__PURE__ */ new Set();
129
+ const itemsSchemas = [];
130
+ jsonSchema.anyOf.forEach((option) => {
131
+ if (typeof option === "object" && option.type) types.add(typeof option.type === "string" ? option.type : option.type[0]);
132
+ if (typeof option === "object" && option.items) {
133
+ const optionItems = option.items;
134
+ if (!Array.isArray(optionItems) && typeof optionItems === "object") itemsSchemas.push(optionItems);
135
+ }
136
+ });
137
+ let finalItems;
138
+ if (itemsSchemas.length === 1) finalItems = itemsSchemas[0];
139
+ else if (itemsSchemas.length > 1) finalItems = { anyOf: itemsSchemas };
140
+ const newSchema = {
141
+ ...types.size > 0 ? { type: Array.from(types) } : { type: "array" },
142
+ ...finalItems && { items: finalItems }
143
+ };
144
+ [
145
+ "default",
146
+ "description",
147
+ "examples",
148
+ "title"
149
+ ].forEach((field) => {
150
+ const value = jsonSchema[field];
151
+ if (value !== void 0) newSchema[field] = value;
152
+ });
153
+ return parseSchema(newSchema, refs);
154
+ }
155
+ if (Array.isArray(jsonSchema.items)) return zod_v3.z.tuple(jsonSchema.items.map((v, i) => parseSchema(v, {
156
+ ...refs,
157
+ path: [
158
+ ...refs.path,
159
+ "items",
160
+ i
161
+ ]
162
+ })));
163
+ let zodSchema = !jsonSchema.items ? zod_v3.z.array(zod_v3.z.any()) : zod_v3.z.array(parseSchema(jsonSchema.items, {
164
+ ...refs,
165
+ path: [...refs.path, "items"]
166
+ }));
167
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minItems", (zs, minItems, errorMessage) => zs.min(minItems, errorMessage));
168
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maxItems", (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage));
169
+ if (typeof jsonSchema.min === "number" && typeof jsonSchema.minItems !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
170
+ ...jsonSchema,
171
+ minItems: jsonSchema.min
172
+ }, "minItems", (zs, minItems, errorMessage) => zs.min(minItems, errorMessage));
173
+ if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxItems !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
174
+ ...jsonSchema,
175
+ maxItems: jsonSchema.max
176
+ }, "maxItems", (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage));
177
+ return zodSchema;
197
178
  };
198
179
 
199
- // src/parsers/parse-boolean.ts
200
- var import_v34 = require("zod/v3");
201
- var parseBoolean = (_jsonSchema) => {
202
- return import_v34.z.boolean();
180
+ //#endregion
181
+ //#region src/parsers/parse-boolean.ts
182
+ const parseBoolean = (_jsonSchema) => {
183
+ return zod_v3.z.boolean();
203
184
  };
204
185
 
205
- // src/parsers/parse-const.ts
206
- var import_v35 = require("zod/v3");
207
- var parseConst = (jsonSchema) => {
208
- return import_v35.z.literal(jsonSchema.const);
186
+ //#endregion
187
+ //#region src/parsers/parse-const.ts
188
+ const parseConst = (jsonSchema) => {
189
+ return zod_v3.z.literal(jsonSchema.const);
209
190
  };
210
191
 
211
- // src/parsers/parse-default.ts
212
- var import_v36 = require("zod/v3");
213
- var parseDefault = (_jsonSchema) => {
214
- return import_v36.z.any();
192
+ //#endregion
193
+ //#region src/parsers/parse-default.ts
194
+ const parseDefault = (_jsonSchema) => {
195
+ return zod_v3.z.any();
215
196
  };
216
197
 
217
- // src/parsers/parse-enum.ts
218
- var import_v37 = require("zod/v3");
219
- var parseEnum = (jsonSchema) => {
220
- if (jsonSchema.enum.length === 0) {
221
- return import_v37.z.never();
222
- }
223
- if (jsonSchema.enum.length === 1) {
224
- return import_v37.z.literal(jsonSchema.enum[0]);
225
- }
226
- if (jsonSchema.enum.every((x) => typeof x === "string")) {
227
- return import_v37.z.enum(jsonSchema.enum);
228
- }
229
- return import_v37.z.union(
230
- jsonSchema.enum.map((x) => import_v37.z.literal(x))
231
- );
198
+ //#endregion
199
+ //#region src/parsers/parse-enum.ts
200
+ const parseEnum = (jsonSchema) => {
201
+ if (jsonSchema.enum.length === 0) return zod_v3.z.never();
202
+ if (jsonSchema.enum.length === 1) return zod_v3.z.literal(jsonSchema.enum[0]);
203
+ if (jsonSchema.enum.every((x) => typeof x === "string")) return zod_v3.z.enum(jsonSchema.enum);
204
+ return zod_v3.z.union(jsonSchema.enum.map((x) => zod_v3.z.literal(x)));
232
205
  };
233
206
 
234
- // src/parsers/parse-if-then-else.ts
235
- var import_v38 = require("zod/v3");
236
- var parseIfThenElse = (jsonSchema, refs) => {
237
- const $if = parseSchema(jsonSchema.if, { ...refs, path: [...refs.path, "if"] });
238
- const $then = parseSchema(jsonSchema.then, {
239
- ...refs,
240
- path: [...refs.path, "then"]
241
- });
242
- const $else = parseSchema(jsonSchema.else, {
243
- ...refs,
244
- path: [...refs.path, "else"]
245
- });
246
- return import_v38.z.union([$then, $else]).superRefine((value, ctx) => {
247
- const result = $if.safeParse(value).success ? $then.safeParse(value) : $else.safeParse(value);
248
- if (!result.success) {
249
- result.error.errors.forEach((error) => ctx.addIssue(error));
250
- }
251
- });
207
+ //#endregion
208
+ //#region src/parsers/parse-if-then-else.ts
209
+ const parseIfThenElse = (jsonSchema, refs) => {
210
+ const $if = parseSchema(jsonSchema.if, {
211
+ ...refs,
212
+ path: [...refs.path, "if"]
213
+ });
214
+ const $then = parseSchema(jsonSchema.then, {
215
+ ...refs,
216
+ path: [...refs.path, "then"]
217
+ });
218
+ const $else = parseSchema(jsonSchema.else, {
219
+ ...refs,
220
+ path: [...refs.path, "else"]
221
+ });
222
+ return zod_v3.z.union([$then, $else]).superRefine((value, ctx) => {
223
+ const result = $if.safeParse(value).success ? $then.safeParse(value) : $else.safeParse(value);
224
+ if (!result.success) result.error.errors.forEach((error) => ctx.addIssue(error));
225
+ });
252
226
  };
253
227
 
254
- // src/parsers/parse-multiple-type.ts
255
- var import_v39 = require("zod/v3");
256
- var parseMultipleType = (jsonSchema, refs) => {
257
- return import_v39.z.union(
258
- jsonSchema.type.map((type) => parseSchema({ ...jsonSchema, type }, refs))
259
- );
228
+ //#endregion
229
+ //#region src/parsers/parse-multiple-type.ts
230
+ const parseMultipleType = (jsonSchema, refs) => {
231
+ return zod_v3.z.union(jsonSchema.type.map((type) => parseSchema({
232
+ ...jsonSchema,
233
+ type
234
+ }, refs)));
260
235
  };
261
236
 
262
- // src/parsers/parse-not.ts
263
- var import_v310 = require("zod/v3");
264
- var parseNot = (jsonSchema, refs) => {
265
- return import_v310.z.any().refine(
266
- (value) => !parseSchema(jsonSchema.not, {
267
- ...refs,
268
- path: [...refs.path, "not"]
269
- }).safeParse(value).success,
270
- "Invalid input: Should NOT be valid against schema"
271
- );
237
+ //#endregion
238
+ //#region src/parsers/parse-not.ts
239
+ const parseNot = (jsonSchema, refs) => {
240
+ return zod_v3.z.any().refine((value) => !parseSchema(jsonSchema.not, {
241
+ ...refs,
242
+ path: [...refs.path, "not"]
243
+ }).safeParse(value).success, "Invalid input: Should NOT be valid against schema");
272
244
  };
273
245
 
274
- // src/parsers/parse-null.ts
275
- var import_v311 = require("zod/v3");
276
- var parseNull = (_jsonSchema) => {
277
- return import_v311.z.null();
246
+ //#endregion
247
+ //#region src/parsers/parse-null.ts
248
+ const parseNull = (_jsonSchema) => {
249
+ return zod_v3.z.null();
278
250
  };
279
251
 
280
- // src/utils/omit.ts
281
- var omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
282
- if (!keys.includes(key)) {
283
- acc[key] = obj[key];
284
- }
285
- return acc;
252
+ //#endregion
253
+ //#region src/utils/omit.ts
254
+ const omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
255
+ if (!keys.includes(key)) acc[key] = obj[key];
256
+ return acc;
286
257
  }, {});
287
258
 
288
- // src/parsers/parse-nullable.ts
289
- var parseNullable = (jsonSchema, refs) => {
290
- const hasNullDefault = jsonSchema.default === null;
291
- const baseSchema = hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable");
292
- const zodSchema = parseSchema(baseSchema, refs, true).nullable();
293
- return hasNullDefault ? zodSchema.default(null) : zodSchema;
259
+ //#endregion
260
+ //#region src/parsers/parse-nullable.ts
261
+ /**
262
+ * For compatibility with open api 3.0 nullable
263
+ */
264
+ const parseNullable = (jsonSchema, refs) => {
265
+ const hasNullDefault = jsonSchema.default === null;
266
+ const zodSchema = parseSchema(hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable"), refs, true).nullable();
267
+ return hasNullDefault ? zodSchema.default(null) : zodSchema;
294
268
  };
295
269
 
296
- // src/parsers/parse-number.ts
297
- var import_v312 = require("zod/v3");
298
- var parseNumber = (jsonSchema) => {
299
- let zodSchema = import_v312.z.number();
300
- let isInteger = false;
301
- if (jsonSchema.type === "integer") {
302
- isInteger = true;
303
- zodSchema = extendSchemaWithMessage(
304
- zodSchema,
305
- jsonSchema,
306
- "type",
307
- (zs, _, errorMsg) => zs.int(errorMsg)
308
- );
309
- } else if (jsonSchema.format === "int64") {
310
- isInteger = true;
311
- zodSchema = extendSchemaWithMessage(
312
- zodSchema,
313
- jsonSchema,
314
- "format",
315
- (zs, _, errorMsg) => zs.int(errorMsg)
316
- );
317
- }
318
- zodSchema = extendSchemaWithMessage(
319
- zodSchema,
320
- jsonSchema,
321
- "multipleOf",
322
- (zs, multipleOf, errorMsg) => {
323
- if (multipleOf === 1) {
324
- if (isInteger) return zs;
325
- return zs.int(errorMsg);
326
- }
327
- return zs.multipleOf(multipleOf, errorMsg);
328
- }
329
- );
330
- if (typeof jsonSchema.minimum === "number") {
331
- if (jsonSchema.exclusiveMinimum === true) {
332
- zodSchema = extendSchemaWithMessage(
333
- zodSchema,
334
- jsonSchema,
335
- "minimum",
336
- (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg)
337
- );
338
- } else {
339
- zodSchema = extendSchemaWithMessage(
340
- zodSchema,
341
- jsonSchema,
342
- "minimum",
343
- (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg)
344
- );
345
- }
346
- } else if (typeof jsonSchema.exclusiveMinimum === "number") {
347
- zodSchema = extendSchemaWithMessage(
348
- zodSchema,
349
- jsonSchema,
350
- "exclusiveMinimum",
351
- (zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg)
352
- );
353
- }
354
- if (typeof jsonSchema.maximum === "number") {
355
- if (jsonSchema.exclusiveMaximum === true) {
356
- zodSchema = extendSchemaWithMessage(
357
- zodSchema,
358
- jsonSchema,
359
- "maximum",
360
- (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg)
361
- );
362
- } else {
363
- zodSchema = extendSchemaWithMessage(
364
- zodSchema,
365
- jsonSchema,
366
- "maximum",
367
- (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg)
368
- );
369
- }
370
- } else if (typeof jsonSchema.exclusiveMaximum === "number") {
371
- zodSchema = extendSchemaWithMessage(
372
- zodSchema,
373
- jsonSchema,
374
- "exclusiveMaximum",
375
- (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg)
376
- );
377
- }
378
- if (typeof jsonSchema.min === "number" && typeof jsonSchema.minimum !== "number") {
379
- zodSchema = extendSchemaWithMessage(
380
- zodSchema,
381
- { ...jsonSchema, minimum: jsonSchema.min },
382
- "minimum",
383
- (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg)
384
- );
385
- }
386
- if (typeof jsonSchema.max === "number" && typeof jsonSchema.maximum !== "number") {
387
- zodSchema = extendSchemaWithMessage(
388
- zodSchema,
389
- { ...jsonSchema, maximum: jsonSchema.max },
390
- "maximum",
391
- (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg)
392
- );
393
- }
394
- return zodSchema;
270
+ //#endregion
271
+ //#region src/parsers/parse-number.ts
272
+ const parseNumber = (jsonSchema) => {
273
+ let zodSchema = zod_v3.z.number();
274
+ let isInteger = false;
275
+ if (jsonSchema.type === "integer") {
276
+ isInteger = true;
277
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "type", (zs, _, errorMsg) => zs.int(errorMsg));
278
+ } else if (jsonSchema.format === "int64") {
279
+ isInteger = true;
280
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, _, errorMsg) => zs.int(errorMsg));
281
+ }
282
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "multipleOf", (zs, multipleOf, errorMsg) => {
283
+ if (multipleOf === 1) {
284
+ if (isInteger) return zs;
285
+ return zs.int(errorMsg);
286
+ }
287
+ return zs.multipleOf(multipleOf, errorMsg);
288
+ });
289
+ if (typeof jsonSchema.minimum === "number") if (jsonSchema.exclusiveMinimum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg));
290
+ else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
291
+ else if (typeof jsonSchema.exclusiveMinimum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMinimum", (zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg));
292
+ if (typeof jsonSchema.maximum === "number") if (jsonSchema.exclusiveMaximum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
293
+ else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
294
+ else if (typeof jsonSchema.exclusiveMaximum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMaximum", (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg));
295
+ if (typeof jsonSchema.min === "number" && typeof jsonSchema.minimum !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
296
+ ...jsonSchema,
297
+ minimum: jsonSchema.min
298
+ }, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
299
+ if (typeof jsonSchema.max === "number" && typeof jsonSchema.maximum !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
300
+ ...jsonSchema,
301
+ maximum: jsonSchema.max
302
+ }, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
303
+ return zodSchema;
395
304
  };
396
305
 
397
- // src/parsers/parse-object.ts
398
- var z14 = __toESM(require("zod/v3"), 1);
399
-
400
- // src/parsers/parse-one-of.ts
401
- var import_v313 = require("zod/v3");
402
- var parseOneOf = (jsonSchema, refs) => {
403
- if (!jsonSchema.oneOf.length) {
404
- return import_v313.z.any();
405
- }
406
- if (jsonSchema.oneOf.length === 1) {
407
- return parseSchema(jsonSchema.oneOf[0], {
408
- ...refs,
409
- path: [...refs.path, "oneOf", 0]
410
- });
411
- }
412
- return import_v313.z.any().superRefine((x, ctx) => {
413
- const schemas = jsonSchema.oneOf.map(
414
- (schema, i) => parseSchema(schema, {
415
- ...refs,
416
- path: [...refs.path, "oneOf", i]
417
- })
418
- );
419
- const unionErrors = schemas.reduce(
420
- (errors, schema) => ((result) => result.error ? [...errors, result.error] : errors)(schema.safeParse(x)),
421
- []
422
- );
423
- if (schemas.length - unionErrors.length !== 1) {
424
- ctx.addIssue({
425
- path: ctx.path,
426
- code: "invalid_union",
427
- unionErrors,
428
- message: "Invalid input: Should pass single schema"
429
- });
430
- }
431
- });
306
+ //#endregion
307
+ //#region src/parsers/parse-one-of.ts
308
+ const parseOneOf = (jsonSchema, refs) => {
309
+ if (!jsonSchema.oneOf.length) return zod_v3.z.any();
310
+ if (jsonSchema.oneOf.length === 1) return parseSchema(jsonSchema.oneOf[0], {
311
+ ...refs,
312
+ path: [
313
+ ...refs.path,
314
+ "oneOf",
315
+ 0
316
+ ]
317
+ });
318
+ return zod_v3.z.any().superRefine((x, ctx) => {
319
+ const schemas = jsonSchema.oneOf.map((schema, i) => parseSchema(schema, {
320
+ ...refs,
321
+ path: [
322
+ ...refs.path,
323
+ "oneOf",
324
+ i
325
+ ]
326
+ }));
327
+ const unionErrors = schemas.reduce((errors, schema) => ((result) => result.error ? [...errors, result.error] : errors)(schema.safeParse(x)), []);
328
+ if (schemas.length - unionErrors.length !== 1) ctx.addIssue({
329
+ path: ctx.path,
330
+ code: "invalid_union",
331
+ unionErrors,
332
+ message: "Invalid input: Should pass single schema"
333
+ });
334
+ });
432
335
  };
433
336
 
434
- // src/parsers/parse-object.ts
337
+ //#endregion
338
+ //#region src/parsers/parse-object.ts
435
339
  function parseObjectProperties(objectSchema, refs) {
436
- if (!objectSchema.properties) {
437
- return z14.object({});
438
- }
439
- const propertyKeys = Object.keys(objectSchema.properties);
440
- if (propertyKeys.length === 0) {
441
- return z14.object({});
442
- }
443
- const properties = {};
444
- for (const key of propertyKeys) {
445
- const propJsonSchema = objectSchema.properties[key];
446
- const propZodSchema = parseSchema(propJsonSchema, {
447
- ...refs,
448
- path: [...refs.path, "properties", key]
449
- });
450
- const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
451
- if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
452
- if (propJsonSchema.default === null) {
453
- const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some(
454
- (schema) => typeof schema === "object" && schema !== null && schema.type === "null"
455
- );
456
- const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some(
457
- (schema) => typeof schema === "object" && schema !== null && schema.type === "null"
458
- );
459
- const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
460
- if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) {
461
- properties[key] = propZodSchema.optional().default(null);
462
- } else {
463
- properties[key] = propZodSchema.nullable().optional().default(null);
464
- }
465
- } else {
466
- properties[key] = propZodSchema.optional().default(propJsonSchema.default);
467
- }
468
- } else {
469
- properties[key] = required ? propZodSchema : propZodSchema.optional();
470
- }
471
- }
472
- return z14.object(properties);
340
+ if (!objectSchema.properties) return zod_v3.object({});
341
+ const propertyKeys = Object.keys(objectSchema.properties);
342
+ if (propertyKeys.length === 0) return zod_v3.object({});
343
+ const properties = {};
344
+ for (const key of propertyKeys) {
345
+ const propJsonSchema = objectSchema.properties[key];
346
+ const propZodSchema = parseSchema(propJsonSchema, {
347
+ ...refs,
348
+ path: [
349
+ ...refs.path,
350
+ "properties",
351
+ key
352
+ ]
353
+ });
354
+ const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
355
+ if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) if (propJsonSchema.default === null) {
356
+ const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
357
+ const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
358
+ const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
359
+ if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
360
+ else properties[key] = propZodSchema.nullable().optional().default(null);
361
+ } else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
362
+ else properties[key] = required ? propZodSchema : propZodSchema.optional();
363
+ }
364
+ return zod_v3.object(properties);
473
365
  }
474
366
  function parseObject(objectSchema, refs) {
475
- const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
476
- const normalizedSchema = objectSchema.type === "object" ? objectSchema : { ...objectSchema, type: "object" };
477
- const propertiesSchema = parseObjectProperties(normalizedSchema, refs);
478
- let zodSchema = propertiesSchema;
479
- const additionalProperties = normalizedSchema.additionalProperties !== void 0 ? parseSchema(normalizedSchema.additionalProperties, {
480
- ...refs,
481
- path: [...refs.path, "additionalProperties"]
482
- }) : void 0;
483
- const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
484
- if (normalizedSchema.patternProperties) {
485
- const parsedPatternProperties = Object.fromEntries(
486
- Object.entries(normalizedSchema.patternProperties).map(([key, value]) => {
487
- return [
488
- key,
489
- parseSchema(value, {
490
- ...refs,
491
- path: [...refs.path, "patternProperties", key]
492
- })
493
- ];
494
- })
495
- );
496
- const patternPropertyValues = Object.values(parsedPatternProperties);
497
- if (propertiesSchema) {
498
- if (additionalProperties) {
499
- zodSchema = propertiesSchema.catchall(
500
- z14.union([...patternPropertyValues, additionalProperties])
501
- );
502
- } else if (Object.keys(parsedPatternProperties).length > 1) {
503
- zodSchema = propertiesSchema.catchall(
504
- z14.union(patternPropertyValues)
505
- );
506
- } else {
507
- zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
508
- }
509
- } else {
510
- if (additionalProperties) {
511
- zodSchema = z14.record(
512
- z14.union([...patternPropertyValues, additionalProperties])
513
- );
514
- } else if (patternPropertyValues.length > 1) {
515
- zodSchema = z14.record(z14.union(patternPropertyValues));
516
- } else {
517
- zodSchema = z14.record(patternPropertyValues[0]);
518
- }
519
- }
520
- const objectPropertyKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
521
- zodSchema = zodSchema.superRefine((value, ctx) => {
522
- for (const key in value) {
523
- let wasMatched = objectPropertyKeys.has(key);
524
- for (const patternPropertyKey in normalizedSchema.patternProperties) {
525
- const regex = new RegExp(patternPropertyKey);
526
- if (key.match(regex)) {
527
- wasMatched = true;
528
- const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
529
- if (!result.success) {
530
- ctx.addIssue({
531
- path: [...ctx.path, key],
532
- code: "custom",
533
- message: `Invalid input: Key matching regex /${key}/ must match schema`,
534
- params: {
535
- issues: result.error.issues
536
- }
537
- });
538
- }
539
- }
540
- }
541
- if (!wasMatched && additionalProperties) {
542
- const result = additionalProperties.safeParse(value[key]);
543
- if (!result.success) {
544
- ctx.addIssue({
545
- path: [...ctx.path, key],
546
- code: "custom",
547
- message: "Invalid input: must match catchall schema",
548
- params: {
549
- issues: result.error.issues
550
- }
551
- });
552
- }
553
- }
554
- }
555
- });
556
- }
557
- let output;
558
- if (propertiesSchema) {
559
- if (hasPatternProperties) {
560
- output = zodSchema;
561
- } else if (additionalProperties) {
562
- if (additionalProperties instanceof z14.ZodNever) {
563
- output = propertiesSchema.strict();
564
- } else if (isAdditionalPropertiesTrue) {
565
- output = propertiesSchema.passthrough();
566
- } else {
567
- output = propertiesSchema.catchall(additionalProperties);
568
- }
569
- } else {
570
- output = propertiesSchema.strict();
571
- }
572
- } else {
573
- if (hasPatternProperties) {
574
- output = zodSchema;
575
- } else if (additionalProperties) {
576
- if (additionalProperties instanceof z14.ZodNever) {
577
- output = z14.object({}).strict();
578
- } else if (isAdditionalPropertiesTrue) {
579
- output = z14.object({}).passthrough();
580
- } else {
581
- output = z14.record(additionalProperties);
582
- }
583
- } else {
584
- output = z14.object({}).passthrough();
585
- }
586
- }
587
- if (its.an.anyOf(objectSchema)) {
588
- output = output.and(
589
- parseAnyOf(
590
- {
591
- ...objectSchema,
592
- anyOf: objectSchema.anyOf.map(
593
- (x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
594
- )
595
- },
596
- refs
597
- )
598
- );
599
- }
600
- if (its.a.oneOf(objectSchema)) {
601
- output = output.and(
602
- parseOneOf(
603
- {
604
- ...objectSchema,
605
- oneOf: objectSchema.oneOf.map(
606
- (x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
607
- )
608
- },
609
- refs
610
- )
611
- );
612
- }
613
- if (its.an.allOf(objectSchema)) {
614
- output = output.and(
615
- parseAllOf(
616
- {
617
- ...objectSchema,
618
- allOf: objectSchema.allOf.map(
619
- (x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
620
- )
621
- },
622
- refs
623
- )
624
- );
625
- }
626
- return output;
367
+ const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
368
+ const normalizedSchema = objectSchema.type === "object" ? objectSchema : {
369
+ ...objectSchema,
370
+ type: "object"
371
+ };
372
+ const propertiesSchema = parseObjectProperties(normalizedSchema, refs);
373
+ let zodSchema = propertiesSchema;
374
+ const additionalProperties = normalizedSchema.additionalProperties !== void 0 ? parseSchema(normalizedSchema.additionalProperties, {
375
+ ...refs,
376
+ path: [...refs.path, "additionalProperties"]
377
+ }) : void 0;
378
+ const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
379
+ if (normalizedSchema.patternProperties) {
380
+ const parsedPatternProperties = Object.fromEntries(Object.entries(normalizedSchema.patternProperties).map(([key, value]) => {
381
+ return [key, parseSchema(value, {
382
+ ...refs,
383
+ path: [
384
+ ...refs.path,
385
+ "patternProperties",
386
+ key
387
+ ]
388
+ })];
389
+ }));
390
+ const patternPropertyValues = Object.values(parsedPatternProperties);
391
+ if (propertiesSchema) if (additionalProperties) zodSchema = propertiesSchema.catchall(zod_v3.union([...patternPropertyValues, additionalProperties]));
392
+ else if (Object.keys(parsedPatternProperties).length > 1) zodSchema = propertiesSchema.catchall(zod_v3.union(patternPropertyValues));
393
+ else zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
394
+ else if (additionalProperties) zodSchema = zod_v3.record(zod_v3.union([...patternPropertyValues, additionalProperties]));
395
+ else if (patternPropertyValues.length > 1) zodSchema = zod_v3.record(zod_v3.union(patternPropertyValues));
396
+ else zodSchema = zod_v3.record(patternPropertyValues[0]);
397
+ const objectPropertyKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
398
+ zodSchema = zodSchema.superRefine((value, ctx) => {
399
+ for (const key in value) {
400
+ let wasMatched = objectPropertyKeys.has(key);
401
+ for (const patternPropertyKey in normalizedSchema.patternProperties) {
402
+ const regex = new RegExp(patternPropertyKey);
403
+ if (key.match(regex)) {
404
+ wasMatched = true;
405
+ const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
406
+ if (!result.success) ctx.addIssue({
407
+ path: [...ctx.path, key],
408
+ code: "custom",
409
+ message: `Invalid input: Key matching regex /${key}/ must match schema`,
410
+ params: { issues: result.error.issues }
411
+ });
412
+ }
413
+ }
414
+ if (!wasMatched && additionalProperties) {
415
+ const result = additionalProperties.safeParse(value[key]);
416
+ if (!result.success) ctx.addIssue({
417
+ path: [...ctx.path, key],
418
+ code: "custom",
419
+ message: "Invalid input: must match catchall schema",
420
+ params: { issues: result.error.issues }
421
+ });
422
+ }
423
+ }
424
+ });
425
+ }
426
+ let output;
427
+ if (propertiesSchema) if (hasPatternProperties) output = zodSchema;
428
+ else if (additionalProperties) if (additionalProperties instanceof zod_v3.ZodNever) output = propertiesSchema.strict();
429
+ else if (isAdditionalPropertiesTrue) output = propertiesSchema.passthrough();
430
+ else output = propertiesSchema.catchall(additionalProperties);
431
+ else output = propertiesSchema.strict();
432
+ else if (hasPatternProperties) output = zodSchema;
433
+ else if (additionalProperties) if (additionalProperties instanceof zod_v3.ZodNever) output = zod_v3.object({}).strict();
434
+ else if (isAdditionalPropertiesTrue) output = zod_v3.object({}).passthrough();
435
+ else output = zod_v3.record(additionalProperties);
436
+ else output = zod_v3.object({}).passthrough();
437
+ if (its.an.anyOf(objectSchema)) output = output.and(parseAnyOf({
438
+ ...objectSchema,
439
+ anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
440
+ ...x,
441
+ type: "object"
442
+ } : x)
443
+ }, refs));
444
+ if (its.a.oneOf(objectSchema)) output = output.and(parseOneOf({
445
+ ...objectSchema,
446
+ oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
447
+ ...x,
448
+ type: "object"
449
+ } : x)
450
+ }, refs));
451
+ if (its.an.allOf(objectSchema)) output = output.and(parseAllOf({
452
+ ...objectSchema,
453
+ allOf: objectSchema.allOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
454
+ ...x,
455
+ type: "object"
456
+ } : x)
457
+ }, refs));
458
+ return output;
627
459
  }
628
460
 
629
- // src/parsers/parse-string.ts
630
- var import_v314 = require("zod/v3");
631
- var parseString = (jsonSchema) => {
632
- let zodSchema = import_v314.z.string();
633
- zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, format, errorMsg) => {
634
- switch (format) {
635
- case "email":
636
- return zs.email(errorMsg);
637
- case "ip":
638
- return zs.ip(errorMsg);
639
- case "ipv4":
640
- return zs.ip({ version: "v4", message: errorMsg });
641
- case "ipv6":
642
- return zs.ip({ version: "v6", message: errorMsg });
643
- case "uri":
644
- return zs.url(errorMsg);
645
- case "uuid":
646
- return zs.uuid(errorMsg);
647
- case "date-time":
648
- return zs.datetime({ offset: true, message: errorMsg });
649
- case "time":
650
- return zs.time(errorMsg);
651
- case "date":
652
- return zs.date(errorMsg);
653
- case "binary":
654
- return zs.base64(errorMsg);
655
- case "duration":
656
- return zs.duration(errorMsg);
657
- default:
658
- return zs;
659
- }
660
- });
661
- zodSchema = extendSchemaWithMessage(
662
- zodSchema,
663
- jsonSchema,
664
- "contentEncoding",
665
- (zs, _, errorMsg) => zs.base64(errorMsg)
666
- );
667
- zodSchema = extendSchemaWithMessage(
668
- zodSchema,
669
- jsonSchema,
670
- "pattern",
671
- (zs, pattern, errorMsg) => zs.regex(new RegExp(pattern), errorMsg)
672
- );
673
- zodSchema = extendSchemaWithMessage(
674
- zodSchema,
675
- jsonSchema,
676
- "minLength",
677
- (zs, minLength, errorMsg) => zs.min(minLength, errorMsg)
678
- );
679
- zodSchema = extendSchemaWithMessage(
680
- zodSchema,
681
- jsonSchema,
682
- "maxLength",
683
- (zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg)
684
- );
685
- if (typeof jsonSchema.min === "number" && typeof jsonSchema.minLength !== "number") {
686
- zodSchema = extendSchemaWithMessage(
687
- zodSchema,
688
- { ...jsonSchema, minLength: jsonSchema.min },
689
- "minLength",
690
- (zs, minLength, errorMsg) => zs.min(minLength, errorMsg)
691
- );
692
- }
693
- if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxLength !== "number") {
694
- zodSchema = extendSchemaWithMessage(
695
- zodSchema,
696
- { ...jsonSchema, maxLength: jsonSchema.max },
697
- "maxLength",
698
- (zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg)
699
- );
700
- }
701
- return zodSchema;
461
+ //#endregion
462
+ //#region src/parsers/parse-string.ts
463
+ const parseString = (jsonSchema) => {
464
+ let zodSchema = zod_v3.z.string();
465
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, format, errorMsg) => {
466
+ switch (format) {
467
+ case "email": return zs.email(errorMsg);
468
+ case "ip": return zs.ip(errorMsg);
469
+ case "ipv4": return zs.ip({
470
+ version: "v4",
471
+ message: errorMsg
472
+ });
473
+ case "ipv6": return zs.ip({
474
+ version: "v6",
475
+ message: errorMsg
476
+ });
477
+ case "uri": return zs.url(errorMsg);
478
+ case "uuid": return zs.uuid(errorMsg);
479
+ case "date-time": return zs.datetime({
480
+ offset: true,
481
+ message: errorMsg
482
+ });
483
+ case "time": return zs.time(errorMsg);
484
+ case "date": return zs.date(errorMsg);
485
+ case "binary": return zs.base64(errorMsg);
486
+ case "duration": return zs.duration(errorMsg);
487
+ default: return zs;
488
+ }
489
+ });
490
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "contentEncoding", (zs, _, errorMsg) => zs.base64(errorMsg));
491
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "pattern", (zs, pattern, errorMsg) => zs.regex(new RegExp(pattern), errorMsg));
492
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minLength", (zs, minLength, errorMsg) => zs.min(minLength, errorMsg));
493
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maxLength", (zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg));
494
+ if (typeof jsonSchema.min === "number" && typeof jsonSchema.minLength !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
495
+ ...jsonSchema,
496
+ minLength: jsonSchema.min
497
+ }, "minLength", (zs, minLength, errorMsg) => zs.min(minLength, errorMsg));
498
+ if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxLength !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
499
+ ...jsonSchema,
500
+ maxLength: jsonSchema.max
501
+ }, "maxLength", (zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg));
502
+ return zodSchema;
702
503
  };
703
504
 
704
- // src/parsers/parse-schema.ts
705
- var addDescribes = (jsonSchema, zodSchema) => {
706
- let description = "";
707
- if (jsonSchema.description) {
708
- description = jsonSchema.description;
709
- } else if (jsonSchema.title) {
710
- description = jsonSchema.title;
711
- }
712
- if (jsonSchema.example !== void 0) {
713
- const exampleText = `Example: ${JSON.stringify(jsonSchema.example)}`;
714
- description = description ? `${description}
715
- ${exampleText}` : exampleText;
716
- } else if (jsonSchema.examples !== void 0 && Array.isArray(jsonSchema.examples)) {
717
- const examples = jsonSchema.examples;
718
- if (examples && examples.length && examples.length > 0) {
719
- const exampleText = examples.length === 1 ? `Example: ${JSON.stringify(examples[0])}` : `Examples:
720
- ${examples.map((ex) => ` ${JSON.stringify(ex)}`).join("\n")}`;
721
- description = description ? `${description}
722
- ${exampleText}` : exampleText;
723
- }
724
- }
725
- if (description) {
726
- zodSchema = zodSchema.describe(description);
727
- }
728
- return zodSchema;
505
+ //#endregion
506
+ //#region src/parsers/parse-schema.ts
507
+ const addDescribes = (jsonSchema, zodSchema) => {
508
+ let description = "";
509
+ if (jsonSchema.description) description = jsonSchema.description;
510
+ else if (jsonSchema.title) description = jsonSchema.title;
511
+ if (jsonSchema.example !== void 0) {
512
+ const exampleText = `Example: ${JSON.stringify(jsonSchema.example)}`;
513
+ description = description ? `${description}\n${exampleText}` : exampleText;
514
+ } else if (jsonSchema.examples !== void 0 && Array.isArray(jsonSchema.examples)) {
515
+ const examples = jsonSchema.examples;
516
+ if (examples && examples.length && examples.length > 0) {
517
+ const exampleText = examples.length === 1 ? `Example: ${JSON.stringify(examples[0])}` : `Examples:\n${examples.map((ex) => ` ${JSON.stringify(ex)}`).join("\n")}`;
518
+ description = description ? `${description}\n${exampleText}` : exampleText;
519
+ }
520
+ }
521
+ if (description) zodSchema = zodSchema.describe(description);
522
+ return zodSchema;
729
523
  };
730
- var addDefaults = (jsonSchema, zodSchema, refs) => {
731
- if (jsonSchema.default !== void 0) {
732
- if (jsonSchema.default === null) {
733
- const inAnyOfOrOneOf = refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf");
734
- if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== "null" && // @ts-ignore: `nullable` is not publicly available in Zod 4
735
- !jsonSchema.nullable) {
736
- return zodSchema;
737
- }
738
- }
739
- zodSchema = zodSchema.default(jsonSchema.default);
740
- }
741
- return zodSchema;
524
+ const addDefaults = (jsonSchema, zodSchema, refs) => {
525
+ if (jsonSchema.default !== void 0) {
526
+ if (jsonSchema.default === null) {
527
+ if (refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf") && jsonSchema.type && jsonSchema.type !== "null" && !jsonSchema.nullable) return zodSchema;
528
+ }
529
+ zodSchema = zodSchema.default(jsonSchema.default);
530
+ }
531
+ return zodSchema;
742
532
  };
743
- var addAnnotations = (jsonSchema, zodSchema) => {
744
- if (jsonSchema.readOnly) {
745
- zodSchema = zodSchema.readonly();
746
- }
747
- return zodSchema;
533
+ const addAnnotations = (jsonSchema, zodSchema) => {
534
+ if (jsonSchema.readOnly) zodSchema = zodSchema.readonly();
535
+ return zodSchema;
748
536
  };
749
- var selectParser = (schema, refs) => {
750
- if (its.a.nullable(schema)) {
751
- return parseNullable(schema, refs);
752
- } else if (its.an.object(schema)) {
753
- return parseObject(schema, refs);
754
- } else if (its.an.array(schema)) {
755
- return parseArray(schema, refs);
756
- } else if (its.an.anyOf(schema)) {
757
- return parseAnyOf(schema, refs);
758
- } else if (its.an.allOf(schema)) {
759
- return parseAllOf(schema, refs);
760
- } else if (its.a.oneOf(schema)) {
761
- return parseOneOf(schema, refs);
762
- } else if (its.a.not(schema)) {
763
- return parseNot(schema, refs);
764
- } else if (its.an.enum(schema)) {
765
- return parseEnum(schema);
766
- } else if (its.a.const(schema)) {
767
- return parseConst(schema);
768
- } else if (its.a.multipleType(schema)) {
769
- return parseMultipleType(schema, refs);
770
- } else if (its.a.primitive(schema, "string")) {
771
- return parseString(schema);
772
- } else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
773
- return parseNumber(schema);
774
- } else if (its.a.primitive(schema, "boolean")) {
775
- return parseBoolean(schema);
776
- } else if (its.a.primitive(schema, "null")) {
777
- return parseNull(schema);
778
- } else if (its.a.conditional(schema)) {
779
- return parseIfThenElse(schema, refs);
780
- } else {
781
- return parseDefault(schema);
782
- }
537
+ const selectParser = (schema, refs) => {
538
+ if (its.a.nullable(schema)) return parseNullable(schema, refs);
539
+ else if (its.an.object(schema)) return parseObject(schema, refs);
540
+ else if (its.an.array(schema)) return parseArray(schema, refs);
541
+ else if (its.an.anyOf(schema)) return parseAnyOf(schema, refs);
542
+ else if (its.an.allOf(schema)) return parseAllOf(schema, refs);
543
+ else if (its.a.oneOf(schema)) return parseOneOf(schema, refs);
544
+ else if (its.a.not(schema)) return parseNot(schema, refs);
545
+ else if (its.an.enum(schema)) return parseEnum(schema);
546
+ else if (its.a.const(schema)) return parseConst(schema);
547
+ else if (its.a.multipleType(schema)) return parseMultipleType(schema, refs);
548
+ else if (its.a.primitive(schema, "string")) return parseString(schema);
549
+ else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) return parseNumber(schema);
550
+ else if (its.a.primitive(schema, "boolean")) return parseBoolean(schema);
551
+ else if (its.a.primitive(schema, "null")) return parseNull(schema);
552
+ else if (its.a.conditional(schema)) return parseIfThenElse(schema, refs);
553
+ else return parseDefault(schema);
783
554
  };
784
- var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [] }, blockMeta) => {
785
- if (typeof jsonSchema !== "object") return jsonSchema ? z16.any() : z16.never();
786
- if (refs.parserOverride) {
787
- const custom = refs.parserOverride(jsonSchema, refs);
788
- if (custom instanceof z16.ZodType) {
789
- return custom;
790
- }
791
- }
792
- let seen = refs.seen.get(jsonSchema);
793
- if (seen) {
794
- if (seen.r !== void 0) {
795
- return seen.r;
796
- }
797
- if (refs.depth === void 0 || seen.n >= refs.depth) {
798
- return z16.any();
799
- }
800
- seen.n += 1;
801
- } else {
802
- seen = { r: void 0, n: 0 };
803
- refs.seen.set(jsonSchema, seen);
804
- }
805
- let parsedZodSchema = selectParser(jsonSchema, refs);
806
- if (!blockMeta) {
807
- if (!refs.withoutDescribes) {
808
- parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
809
- }
810
- if (!refs.withoutDefaults) {
811
- parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
812
- }
813
- parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
814
- }
815
- seen.r = parsedZodSchema;
816
- return parsedZodSchema;
555
+ const parseSchema = (jsonSchema, refs = {
556
+ seen: /* @__PURE__ */ new Map(),
557
+ path: []
558
+ }, blockMeta) => {
559
+ if (typeof jsonSchema !== "object") return jsonSchema ? zod_v3.any() : zod_v3.never();
560
+ if (refs.parserOverride) {
561
+ const custom = refs.parserOverride(jsonSchema, refs);
562
+ if (custom instanceof zod_v3.ZodType) return custom;
563
+ }
564
+ let seen = refs.seen.get(jsonSchema);
565
+ if (seen) {
566
+ if (seen.r !== void 0) return seen.r;
567
+ if (refs.depth === void 0 || seen.n >= refs.depth) return zod_v3.any();
568
+ seen.n += 1;
569
+ } else {
570
+ seen = {
571
+ r: void 0,
572
+ n: 0
573
+ };
574
+ refs.seen.set(jsonSchema, seen);
575
+ }
576
+ let parsedZodSchema = selectParser(jsonSchema, refs);
577
+ if (!blockMeta) {
578
+ if (!refs.withoutDescribes) parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
579
+ if (!refs.withoutDefaults) parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
580
+ parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
581
+ }
582
+ seen.r = parsedZodSchema;
583
+ return parsedZodSchema;
817
584
  };
818
585
 
819
- // src/json-schema-to-zod.ts
820
- var jsonSchemaToZod = (schema, options = {}) => {
821
- return parseSchema(schema, {
822
- path: [],
823
- seen: /* @__PURE__ */ new Map(),
824
- ...options
825
- });
586
+ //#endregion
587
+ //#region src/parsers/parse-object-shape.ts
588
+ /**
589
+ * Parses a JSON Schema object and returns the raw shape (property keys mapped to Zod types).
590
+ * This is useful when you need ZodRawShape instead of a complete ZodObject.
591
+ *
592
+ * @param objectSchema - The JSON Schema object to parse
593
+ * @param refs - Parser references for handling nested schemas
594
+ * @returns A Zod raw shape object
595
+ */
596
+ function parseObjectShape(objectSchema, refs) {
597
+ if (!objectSchema.properties) return {};
598
+ const propertyKeys = Object.keys(objectSchema.properties);
599
+ if (propertyKeys.length === 0) return {};
600
+ const properties = {};
601
+ for (const key of propertyKeys) {
602
+ const propJsonSchema = objectSchema.properties[key];
603
+ const propZodSchema = parseSchema(propJsonSchema, {
604
+ ...refs,
605
+ path: [
606
+ ...refs.path,
607
+ "properties",
608
+ key
609
+ ]
610
+ });
611
+ const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
612
+ if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) if (propJsonSchema.default === null) {
613
+ const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
614
+ const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
615
+ const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
616
+ if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
617
+ else properties[key] = propZodSchema.nullable().optional().default(null);
618
+ } else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
619
+ else properties[key] = required ? propZodSchema : propZodSchema.optional();
620
+ }
621
+ return properties;
622
+ }
623
+
624
+ //#endregion
625
+ //#region src/json-schema-to-zod.ts
626
+ const jsonSchemaToZod = (schema, options = {}) => {
627
+ return parseSchema(schema, {
628
+ path: [],
629
+ seen: /* @__PURE__ */ new Map(),
630
+ ...options
631
+ });
826
632
  };
827
- // Annotate the CommonJS export names for ESM import in node:
828
- 0 && (module.exports = {
829
- jsonSchemaToZod
830
- });
633
+ /**
634
+ * Converts a JSON Schema object to a Zod raw shape.
635
+ * This is useful when you need the shape object (e.g., `{ name: z.string(), age: z.number() }`)
636
+ * rather than a complete ZodObject schema.
637
+ *
638
+ * This is particularly useful for APIs that expect ZodRawShape instead of ZodType,
639
+ * such as the Claude Agent SDK's `tool()` function.
640
+ *
641
+ * @param schema - The JSON Schema to convert (must be an object schema)
642
+ * @param options - Optional configuration for the conversion
643
+ * @returns A Zod raw shape object with property keys mapped to Zod types
644
+ */
645
+ const jsonSchemaToZodShape = (schema, options = {}) => {
646
+ if (typeof schema !== "object" || schema === null) return {};
647
+ return parseObjectShape(schema, {
648
+ path: [],
649
+ seen: /* @__PURE__ */ new Map(),
650
+ ...options
651
+ });
652
+ };
653
+
654
+ //#endregion
655
+ exports.jsonSchemaToZod = jsonSchemaToZod;
656
+ exports.jsonSchemaToZodShape = jsonSchemaToZodShape;