zod-openapi 5.0.0-beta.3 → 5.0.0-beta.4

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 (38) hide show
  1. package/README.md +1 -1
  2. package/dist/api.d.mts +22 -5
  3. package/dist/api.d.ts +22 -5
  4. package/dist/api.js +8 -0
  5. package/dist/api.mjs +4 -8
  6. package/dist/components-5_CJdR73.d.ts +543 -0
  7. package/dist/components-CXjVnBr-.js +782 -0
  8. package/dist/components-CvutxtFV.mjs +741 -0
  9. package/dist/components-DAYTA1Um.d.mts +543 -0
  10. package/dist/create/componentsSideEffects.d.mts +6 -0
  11. package/dist/create/componentsSideEffects.d.ts +6 -0
  12. package/dist/create/componentsSideEffects.js +48 -0
  13. package/dist/create/componentsSideEffects.mjs +48 -0
  14. package/dist/index.d.mts +23 -5
  15. package/dist/index.d.ts +23 -5
  16. package/dist/index.js +20 -0
  17. package/dist/index.mjs +17 -22
  18. package/dist/zod-BvA30wad.mjs +5 -0
  19. package/dist/zod-i2t01GF0.js +40 -0
  20. package/package.json +9 -26
  21. package/api/index.d.ts +0 -1
  22. package/api/package.json +0 -5
  23. package/dist/api.cjs +0 -8
  24. package/dist/components.chunk.cjs +0 -1023
  25. package/dist/components.chunk.mjs +0 -1024
  26. package/dist/create/components.d.ts +0 -65
  27. package/dist/create/content.d.ts +0 -10
  28. package/dist/create/document.d.ts +0 -97
  29. package/dist/create/object.d.ts +0 -5
  30. package/dist/create/parameters.d.ts +0 -14
  31. package/dist/create/schema/schema.d.ts +0 -21
  32. package/dist/index.cjs +0 -23
  33. package/dist/openapi.d.ts +0 -5
  34. package/dist/openapi3-ts/dist/model/oas-common.d.ts +0 -16
  35. package/dist/openapi3-ts/dist/model/openapi31.d.ts +0 -302
  36. package/dist/openapi3-ts/dist/model/specification-extension.d.ts +0 -7
  37. package/dist/openapi3-ts/oas31.d.ts +0 -3
  38. package/dist/zod.d.ts +0 -49
@@ -1,1023 +0,0 @@
1
- "use strict";
2
- const core = require("zod/v4/core");
3
- const v4 = require("zod/v4");
4
- const isAnyZodType = (schema) => typeof schema === "object" && schema !== null && "_zod" in schema;
5
- const unwrapZodObject = (zodType, io, path) => {
6
- const def = zodType._zod.def;
7
- switch (def.type) {
8
- case "object": {
9
- return zodType;
10
- }
11
- case "lazy": {
12
- return unwrapZodObject(def.getter(), io, path);
13
- }
14
- case "pipe": {
15
- if (io === "input") {
16
- return unwrapZodObject(def.in, io, path);
17
- }
18
- return unwrapZodObject(def.out, io, path);
19
- }
20
- }
21
- throw new Error(
22
- `Failed to unwrap ZodObject from type: ${zodType._zod.def.type} at ${path.join(" > ")}`
23
- );
24
- };
25
- const isRequired = (zodType, io) => {
26
- if (io === "input") {
27
- return zodType._zod.optin === void 0;
28
- }
29
- return zodType._zod.optout === void 0;
30
- };
31
- const createParameter = (parameter, location, ctx, path) => {
32
- var _a, _b;
33
- const seenParameter = ctx.registry.parameters.seen.get(parameter);
34
- if (seenParameter) {
35
- return seenParameter;
36
- }
37
- const meta = v4.globalRegistry.get(parameter);
38
- const name = (location == null ? void 0 : location.name) ?? ((_a = meta == null ? void 0 : meta.param) == null ? void 0 : _a.name);
39
- const inLocation = (location == null ? void 0 : location.in) ?? ((_b = meta == null ? void 0 : meta.param) == null ? void 0 : _b.in);
40
- if (!name || !inLocation) {
41
- throw new Error(
42
- `Parameter at ${path.join(" > ")} is missing \`.meta({ param: { name, in } })\` information`
43
- );
44
- }
45
- const computedPath = [...path, inLocation, name].join(" > ");
46
- const schemaObject = ctx.registry.schemas.setSchema(
47
- computedPath,
48
- parameter,
49
- ctx.io
50
- );
51
- const { id, ...rest } = (meta == null ? void 0 : meta.param) ?? {};
52
- const parameterObject = {
53
- ...rest,
54
- name,
55
- in: inLocation,
56
- schema: schemaObject
57
- };
58
- if (isRequired(parameter, ctx.io)) {
59
- parameterObject.required = true;
60
- }
61
- if (!parameterObject.description && (meta == null ? void 0 : meta.description)) {
62
- parameterObject.description = meta.description;
63
- }
64
- if (id) {
65
- const ref = {
66
- $ref: `#/components/parameters/${id}`
67
- };
68
- ctx.registry.parameters.seen.set(parameter, ref);
69
- ctx.registry.parameters.ids.set(id, parameterObject);
70
- return ref;
71
- }
72
- ctx.registry.parameters.seen.set(parameter, parameterObject);
73
- return parameterObject;
74
- };
75
- const createManualParameters = (parameters, ctx, path) => {
76
- if (!parameters) {
77
- return void 0;
78
- }
79
- const parameterObjects = [];
80
- for (const parameter of parameters) {
81
- if (isAnyZodType(parameter)) {
82
- const seenParameter = ctx.registry.parameters.seen.get(parameter);
83
- if (seenParameter) {
84
- parameterObjects.push(seenParameter);
85
- continue;
86
- }
87
- const paramObject = createParameter(parameter, void 0, ctx, [
88
- ...path,
89
- "parameters"
90
- ]);
91
- parameterObjects.push(paramObject);
92
- continue;
93
- }
94
- parameterObjects.push(parameter);
95
- }
96
- return parameterObjects;
97
- };
98
- const createParameters = (requestParams, ctx, path) => {
99
- if (!requestParams) {
100
- return void 0;
101
- }
102
- const parameterObjects = [];
103
- for (const [location, schema] of Object.entries(requestParams ?? {})) {
104
- if (!schema) {
105
- continue;
106
- }
107
- const zodObject = unwrapZodObject(schema, ctx.io, path);
108
- for (const [name, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
109
- const seenParameter = ctx.registry.parameters.seen.get(zodSchema);
110
- if (seenParameter) {
111
- parameterObjects.push(seenParameter);
112
- continue;
113
- }
114
- const paramObject = createParameter(
115
- zodSchema,
116
- {
117
- in: location,
118
- name
119
- },
120
- ctx,
121
- [...path, location, name]
122
- );
123
- parameterObjects.push(paramObject);
124
- }
125
- }
126
- return parameterObjects;
127
- };
128
- const createMediaTypeObject = (mediaTypeObject, ctx, path) => {
129
- const computedPath = path.join(" > ");
130
- if (isAnyZodType(mediaTypeObject.schema)) {
131
- const schemaObject = ctx.registry.schemas.setSchema(
132
- computedPath,
133
- mediaTypeObject.schema,
134
- ctx.io
135
- );
136
- return {
137
- ...mediaTypeObject,
138
- schema: schemaObject
139
- };
140
- }
141
- return mediaTypeObject;
142
- };
143
- const createContent = (content, ctx, path) => {
144
- const contentObject = {};
145
- for (const [mediaType, mediaTypeObject] of Object.entries(content)) {
146
- if (mediaTypeObject) {
147
- contentObject[mediaType] = createMediaTypeObject(mediaTypeObject, ctx, [
148
- ...path,
149
- mediaType
150
- ]);
151
- }
152
- }
153
- return contentObject;
154
- };
155
- const createRequestBody = (requestBody, ctx, path) => {
156
- if (!requestBody) {
157
- return void 0;
158
- }
159
- const seenRequestBody = ctx.registry.requestBodies.seen.get(requestBody);
160
- if (seenRequestBody) {
161
- return seenRequestBody;
162
- }
163
- const { content, id, ...rest } = requestBody;
164
- const requestBodyObject = {
165
- ...rest,
166
- content: createContent(content, ctx, [...path, "content"])
167
- };
168
- if (id) {
169
- const ref = {
170
- $ref: `#/components/requestBodies/${id}`
171
- };
172
- ctx.registry.requestBodies.ids.set(id, requestBodyObject);
173
- ctx.registry.requestBodies.seen.set(requestBody, ref);
174
- return ref;
175
- }
176
- ctx.registry.requestBodies.seen.set(requestBody, requestBodyObject);
177
- return requestBodyObject;
178
- };
179
- const createHeader = (header, ctx, path) => {
180
- const seenHeader = ctx.registry.headers.seen.get(header);
181
- if (seenHeader) {
182
- return seenHeader;
183
- }
184
- const meta = v4.globalRegistry.get(header);
185
- const { id, ...rest } = (meta == null ? void 0 : meta.header) ?? {};
186
- const headerObject = rest;
187
- if (isRequired(header, ctx.io)) {
188
- headerObject.required = true;
189
- }
190
- if (!headerObject.description && (meta == null ? void 0 : meta.description)) {
191
- headerObject.description = meta.description;
192
- }
193
- const computedPath = path.join(" > ");
194
- headerObject.schema = ctx.registry.schemas.setSchema(
195
- computedPath,
196
- header,
197
- ctx.io
198
- );
199
- if (id) {
200
- const ref = {
201
- $ref: `#/components/headers/${id}`
202
- };
203
- ctx.registry.headers.ids.set(id, headerObject);
204
- ctx.registry.headers.seen.set(header, ref);
205
- return ref;
206
- }
207
- ctx.registry.headers.seen.set(header, headerObject);
208
- return headerObject;
209
- };
210
- const createHeaders = (headers, ctx, path) => {
211
- if (!headers) {
212
- return void 0;
213
- }
214
- if (isAnyZodType(headers)) {
215
- const zodObject = unwrapZodObject(headers, ctx.io, path);
216
- const headersObject = {};
217
- for (const [key, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
218
- const header = createHeader(zodSchema, ctx, [...path, key]);
219
- headersObject[key] = header;
220
- }
221
- return headersObject;
222
- }
223
- return headers;
224
- };
225
- const isISpecificationExtension = (key) => key.startsWith("x-");
226
- const createResponse = (response, ctx, path) => {
227
- const seenResponse = ctx.registry.responses.seen.get(response);
228
- if (seenResponse) {
229
- return seenResponse;
230
- }
231
- const { content, headers, id, ...rest } = response;
232
- const responseObject = rest;
233
- const maybeHeaders = createHeaders(headers, ctx, [...path, "headers"]);
234
- if (maybeHeaders) {
235
- responseObject.headers = maybeHeaders;
236
- }
237
- if (content) {
238
- responseObject.content = createContent(content, ctx, [...path, "content"]);
239
- }
240
- if (id) {
241
- const ref = {
242
- $ref: `#/components/responses/${id}`
243
- };
244
- ctx.registry.responses.ids.set(id, responseObject);
245
- ctx.registry.responses.seen.set(response, ref);
246
- return ref;
247
- }
248
- ctx.registry.responses.seen.set(response, responseObject);
249
- return responseObject;
250
- };
251
- const createResponses = (responses, ctx, path) => {
252
- if (!responses) {
253
- return void 0;
254
- }
255
- const responsesObject = {};
256
- for (const [statusCode, response] of Object.entries(responses)) {
257
- if (!response) {
258
- continue;
259
- }
260
- if (isISpecificationExtension(statusCode)) {
261
- responsesObject[statusCode] = response;
262
- continue;
263
- }
264
- if ("$ref" in response) {
265
- responsesObject[statusCode] = response;
266
- continue;
267
- }
268
- const responseObject = createResponse(
269
- response,
270
- ctx,
271
- [...path, statusCode]
272
- );
273
- responsesObject[statusCode] = responseObject;
274
- }
275
- return responsesObject;
276
- };
277
- const createOperation = (operationObject, registry, path) => {
278
- const {
279
- parameters,
280
- requestParams,
281
- requestBody,
282
- responses,
283
- callbacks,
284
- ...rest
285
- } = operationObject;
286
- const operation = rest;
287
- const maybeManualParameters = createManualParameters(
288
- parameters,
289
- {
290
- registry,
291
- io: "input"
292
- },
293
- [...path, "parameters"]
294
- );
295
- const maybeRequestParams = createParameters(
296
- requestParams,
297
- {
298
- registry,
299
- io: "input"
300
- },
301
- [...path, "requestParams"]
302
- );
303
- if (maybeRequestParams || maybeManualParameters) {
304
- operation.parameters = [
305
- ...maybeRequestParams ?? [],
306
- ...maybeManualParameters ?? []
307
- ];
308
- }
309
- const maybeRequestBody = createRequestBody(
310
- requestBody,
311
- {
312
- registry,
313
- io: "input"
314
- },
315
- [...path, "requestBody"]
316
- );
317
- if (maybeRequestBody) {
318
- operation.requestBody = maybeRequestBody;
319
- }
320
- const maybeResponses = createResponses(
321
- responses,
322
- {
323
- registry,
324
- io: "output"
325
- },
326
- [...path, "responses"]
327
- );
328
- if (maybeResponses) {
329
- operation.responses = maybeResponses;
330
- }
331
- const maybeCallbacks = createCallbacks(callbacks, registry, [
332
- ...path,
333
- "callbacks"
334
- ]);
335
- if (maybeCallbacks) {
336
- operation.callbacks = maybeCallbacks;
337
- }
338
- return operation;
339
- };
340
- const createPathItem = (pathItem, registry, path) => {
341
- const pathItemObject = {};
342
- const { id, ...rest } = pathItem;
343
- for (const [key, value] of Object.entries(rest)) {
344
- if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
345
- pathItemObject[key] = createOperation(
346
- value,
347
- registry,
348
- [...path, key]
349
- );
350
- continue;
351
- }
352
- if (key === "parameters") {
353
- pathItemObject[key] = createManualParameters(
354
- value,
355
- {
356
- registry,
357
- io: "input"
358
- },
359
- [...path, key]
360
- );
361
- continue;
362
- }
363
- pathItemObject[key] = value;
364
- }
365
- if (id) {
366
- const ref = {
367
- $ref: `#/components/pathItems/${id}`
368
- };
369
- registry.pathItems.ids.set(id, pathItemObject);
370
- registry.pathItems.seen.set(pathItem, ref);
371
- return ref;
372
- }
373
- registry.pathItems.seen.set(pathItem, pathItemObject);
374
- return pathItemObject;
375
- };
376
- const createPaths = (paths, registry, path) => {
377
- if (!paths) {
378
- return void 0;
379
- }
380
- const pathsObject = {};
381
- for (const [singlePath, pathItemObject] of Object.entries(paths)) {
382
- if (isISpecificationExtension(singlePath)) {
383
- pathsObject[singlePath] = pathItemObject;
384
- continue;
385
- }
386
- pathsObject[singlePath] = createPathItem(pathItemObject, registry, [
387
- ...path,
388
- singlePath
389
- ]);
390
- }
391
- return pathsObject;
392
- };
393
- const createCallback = (callbackObject, registry, path) => {
394
- const seenCallback = registry.callbacks.seen.get(callbackObject);
395
- if (seenCallback) {
396
- return seenCallback;
397
- }
398
- const { id, ...rest } = callbackObject;
399
- const callback = {};
400
- for (const [name, pathItem] of Object.entries(rest)) {
401
- if (isISpecificationExtension(name)) {
402
- callback[name] = pathItem;
403
- continue;
404
- }
405
- callback[name] = createPathItem(
406
- pathItem,
407
- registry,
408
- [...path, name]
409
- );
410
- }
411
- if (id) {
412
- const ref = {
413
- $ref: `#/components/callbacks/${id}`
414
- };
415
- registry.callbacks.ids.set(id, callback);
416
- registry.callbacks.seen.set(callbackObject, ref);
417
- return ref;
418
- }
419
- registry.callbacks.seen.set(callbackObject, callback);
420
- return callback;
421
- };
422
- const createCallbacks = (callbacks, registry, path) => {
423
- if (!callbacks) {
424
- return void 0;
425
- }
426
- const callbacksObject = {};
427
- for (const [name, value] of Object.entries(callbacks)) {
428
- if (isISpecificationExtension(name)) {
429
- callbacksObject[name] = value;
430
- continue;
431
- }
432
- callbacksObject[name] = createCallback(
433
- value,
434
- registry,
435
- [...path, name]
436
- );
437
- }
438
- return callbacksObject;
439
- };
440
- const override = (ctx) => {
441
- var _a;
442
- const def = ctx.zodSchema._zod.def;
443
- switch (def.type) {
444
- case "bigint": {
445
- ctx.jsonSchema.type = "integer";
446
- ctx.jsonSchema.format = "int64";
447
- break;
448
- }
449
- case "union": {
450
- if ("discriminator" in def && typeof def.discriminator === "string") {
451
- ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
452
- delete ctx.jsonSchema.anyOf;
453
- ctx.jsonSchema.type = "object";
454
- ctx.jsonSchema.discriminator = {
455
- propertyName: def.discriminator
456
- };
457
- const mapping = {};
458
- for (const [index, obj] of Object.entries(
459
- ctx.jsonSchema.oneOf
460
- )) {
461
- const ref = obj.$ref;
462
- if (!ref) {
463
- return;
464
- }
465
- const discriminatorValues = (_a = def.options[Number(index)]._zod.propValues) == null ? void 0 : _a[def.discriminator];
466
- if (!(discriminatorValues == null ? void 0 : discriminatorValues.size)) {
467
- return;
468
- }
469
- for (const value of [...discriminatorValues ?? []]) {
470
- if (typeof value !== "string") {
471
- return;
472
- }
473
- mapping[value] = ref;
474
- }
475
- }
476
- ctx.jsonSchema.discriminator.mapping = mapping;
477
- }
478
- const meta = ctx.zodSchema.meta();
479
- if (typeof (meta == null ? void 0 : meta.unionOneOf) === "boolean") {
480
- if (meta.unionOneOf) {
481
- ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
482
- delete ctx.jsonSchema.anyOf;
483
- }
484
- delete ctx.jsonSchema.unionOneOf;
485
- }
486
- break;
487
- }
488
- case "date": {
489
- ctx.jsonSchema.type = "string";
490
- break;
491
- }
492
- case "literal": {
493
- if (def.values.includes(void 0)) {
494
- break;
495
- }
496
- break;
497
- }
498
- }
499
- };
500
- const validate = (ctx, opts) => {
501
- var _a;
502
- if (Object.keys(ctx.jsonSchema).length) {
503
- return;
504
- }
505
- const def = ctx.zodSchema._zod.def;
506
- if ((_a = opts.allowEmptySchema) == null ? void 0 : _a[def.type]) {
507
- return;
508
- }
509
- switch (def.type) {
510
- case "any": {
511
- return;
512
- }
513
- case "unknown": {
514
- return;
515
- }
516
- case "pipe": {
517
- if (ctx.io === "output") {
518
- throw new Error(
519
- "Zod transform schemas are not supported in output schemas. Please use `.overwrite()` or wrap the schema in a `.pipe()`"
520
- );
521
- }
522
- return;
523
- }
524
- case "transform": {
525
- if (ctx.io === "output") {
526
- return;
527
- }
528
- break;
529
- }
530
- case "literal": {
531
- if (def.values.includes(void 0)) {
532
- throw new Error(
533
- "Zod literal schemas cannot include `undefined` as a value. Please use `z.undefined()` or `.optional()` instead."
534
- );
535
- }
536
- return;
537
- }
538
- }
539
- throw new Error(
540
- `Zod schema of type \`${def.type}\` cannot be represented in OpenAPI. Please assign it metadata with \`.meta()\``
541
- );
542
- };
543
- const renameComponents = (components, outputIds, ctx) => {
544
- const componentsToRename = /* @__PURE__ */ new Map();
545
- const componentDependencies = /* @__PURE__ */ new Map();
546
- const stringifiedComponents = /* @__PURE__ */ new Map();
547
- for (const [key, value] of Object.entries(components)) {
548
- const stringified = JSON.stringify(value);
549
- const matches = stringified.matchAll(/"#\/components\/schemas\/([^"]+)"/g);
550
- const dependencies = /* @__PURE__ */ new Set();
551
- for (const match of matches) {
552
- const dep = match[1];
553
- if (dep !== key) {
554
- dependencies.add(dep);
555
- }
556
- }
557
- stringifiedComponents.set(key, stringified);
558
- componentDependencies.set(key, {
559
- dependencies
560
- });
561
- }
562
- for (const [key] of stringifiedComponents) {
563
- const registeredComponent = ctx.registry.schemas.ids.get(key);
564
- if (!registeredComponent) {
565
- continue;
566
- }
567
- if (isDependencyPure(
568
- componentDependencies,
569
- stringifiedComponents,
570
- ctx.registry,
571
- key
572
- )) {
573
- continue;
574
- }
575
- const newName = outputIds.get(key) ?? `${key}Output`;
576
- componentsToRename.set(key, newName);
577
- components[newName] = components[key];
578
- delete components[key];
579
- continue;
580
- }
581
- return componentsToRename;
582
- };
583
- const isDependencyPure = (componentDependencies, stringifiedComponents, registry, key, visited = /* @__PURE__ */ new Set()) => {
584
- if (visited.has(key)) {
585
- return true;
586
- }
587
- const dependencies = componentDependencies.get(key);
588
- if (dependencies.pure !== void 0) {
589
- return dependencies.pure;
590
- }
591
- const stringified = stringifiedComponents.get(key);
592
- const component = registry.schemas.ids.get(key);
593
- if (component && stringified !== JSON.stringify(component)) {
594
- dependencies.pure = false;
595
- return false;
596
- }
597
- visited.add(key);
598
- const result = [...dependencies.dependencies].every(
599
- (dep) => isDependencyPure(
600
- componentDependencies,
601
- stringifiedComponents,
602
- registry,
603
- dep,
604
- new Set(visited)
605
- )
606
- );
607
- dependencies.pure = result;
608
- return result;
609
- };
610
- const deleteZodOpenApiMeta = (jsonSchema) => {
611
- delete jsonSchema.param;
612
- delete jsonSchema.header;
613
- delete jsonSchema.unusedIO;
614
- delete jsonSchema.override;
615
- delete jsonSchema.outputId;
616
- };
617
- const createSchema = (schema, ctx = {
618
- registry: createRegistry(),
619
- io: "output",
620
- opts: {}
621
- }) => {
622
- ctx.registry ?? (ctx.registry = createRegistry());
623
- ctx.opts ?? (ctx.opts = {});
624
- ctx.io ?? (ctx.io = "output");
625
- const registrySchemas = Object.fromEntries(ctx.registry.schemas[ctx.io]);
626
- const schemas = {
627
- zodOpenApiCreateSchema: { zodType: schema }
628
- };
629
- Object.assign(schemas, registrySchemas);
630
- const jsonSchemas = createSchemas(schemas, {
631
- registry: ctx.registry,
632
- io: ctx.io,
633
- opts: ctx.opts
634
- });
635
- return {
636
- schema: jsonSchemas.schemas.zodOpenApiCreateSchema,
637
- components: jsonSchemas.components
638
- };
639
- };
640
- const createSchemas = (schemas, ctx) => {
641
- var _a, _b;
642
- const entries = {};
643
- for (const [name, { zodType }] of Object.entries(schemas)) {
644
- entries[name] = zodType;
645
- }
646
- const schemaRegistry = v4.object(entries);
647
- const outputIds = /* @__PURE__ */ new Map();
648
- const jsonSchema = v4.toJSONSchema(schemaRegistry, {
649
- override(context) {
650
- const meta = context.zodSchema.meta();
651
- if ((meta == null ? void 0 : meta.outputId) && (meta == null ? void 0 : meta.id)) {
652
- outputIds.set(meta.id, meta.outputId);
653
- }
654
- if (context.jsonSchema.$ref) {
655
- return;
656
- }
657
- const enrichedContext = { ...context, io: ctx.io };
658
- override(enrichedContext);
659
- if (typeof ctx.opts.override === "function") {
660
- ctx.opts.override(enrichedContext);
661
- }
662
- if (typeof (meta == null ? void 0 : meta.override) === "function") {
663
- meta.override(enrichedContext);
664
- delete context.jsonSchema.override;
665
- }
666
- if (typeof (meta == null ? void 0 : meta.override) === "object" && meta.override !== null) {
667
- Object.assign(context.jsonSchema, meta.override);
668
- delete context.jsonSchema.override;
669
- }
670
- delete context.jsonSchema.$schema;
671
- delete context.jsonSchema.id;
672
- deleteZodOpenApiMeta(context.jsonSchema);
673
- validate(enrichedContext, ctx.opts);
674
- },
675
- io: ctx.io,
676
- unrepresentable: "any"
677
- });
678
- const components = jsonSchema.$defs ?? {};
679
- const dynamicComponents = /* @__PURE__ */ new Map();
680
- for (const [key, value] of Object.entries(components)) {
681
- if (/^__schema\d+$/.test(key)) {
682
- const newName = `__schema${ctx.registry.schemas.dynamicSchemaCount++}`;
683
- dynamicComponents.set(key, `"#/components/schemas/${newName}"`);
684
- if (newName !== key) {
685
- components[newName] = value;
686
- delete components[key];
687
- }
688
- }
689
- }
690
- const parsedJsonSchema = JSON.parse(
691
- JSON.stringify(jsonSchema).replace(
692
- /"#\/\$defs\/([^"]+)"/g,
693
- (_, match) => {
694
- const dynamic = dynamicComponents.get(match);
695
- if (dynamic) {
696
- return dynamic;
697
- }
698
- const manualComponent = ctx.registry.schemas.manual.get(match);
699
- if (manualComponent) {
700
- manualComponent.io[ctx.io].used++;
701
- }
702
- return `"#/components/schemas/${match}"`;
703
- }
704
- )
705
- );
706
- for (const [key, value] of ctx.registry.schemas.manual) {
707
- if (value.io[ctx.io].used === 1 && ((_a = parsedJsonSchema.properties) == null ? void 0 : _a[value.identifier]) && ((_b = parsedJsonSchema.$defs) == null ? void 0 : _b[key])) {
708
- parsedJsonSchema.properties[value.identifier] = parsedJsonSchema.$defs[key];
709
- delete parsedJsonSchema.$defs[key];
710
- continue;
711
- }
712
- }
713
- const renamedComponents = renameComponents(
714
- parsedJsonSchema.$defs ?? {},
715
- outputIds,
716
- ctx
717
- );
718
- if (!renamedComponents.size) {
719
- return {
720
- schemas: parsedJsonSchema.properties,
721
- components: parsedJsonSchema.$defs ?? {}
722
- };
723
- }
724
- const renamedJsonSchema = JSON.parse(
725
- JSON.stringify(parsedJsonSchema).replace(
726
- /"#\/components\/schemas\/([^"]+)"/g,
727
- (_, match) => {
728
- const replacement = renamedComponents.get(match);
729
- if (replacement) {
730
- return `"#/components/schemas/${replacement}"`;
731
- }
732
- return `"#/components/schemas/${match}"`;
733
- }
734
- )
735
- );
736
- return {
737
- schemas: renamedJsonSchema.properties,
738
- components: renamedJsonSchema.$defs ?? {}
739
- };
740
- };
741
- const createRegistry = (components) => {
742
- const registry = {
743
- schemas: {
744
- dynamicSchemaCount: 0,
745
- input: /* @__PURE__ */ new Map(),
746
- output: /* @__PURE__ */ new Map(),
747
- ids: /* @__PURE__ */ new Map(),
748
- manual: /* @__PURE__ */ new Map(),
749
- setSchema: (key, schema, io) => {
750
- const schemaObject = {};
751
- registry.schemas[io].set(key, {
752
- schemaObject,
753
- zodType: schema
754
- });
755
- return schemaObject;
756
- }
757
- },
758
- headers: {
759
- ids: /* @__PURE__ */ new Map(),
760
- seen: /* @__PURE__ */ new WeakMap()
761
- },
762
- requestBodies: {
763
- ids: /* @__PURE__ */ new Map(),
764
- seen: /* @__PURE__ */ new WeakMap()
765
- },
766
- responses: {
767
- ids: /* @__PURE__ */ new Map(),
768
- seen: /* @__PURE__ */ new WeakMap()
769
- },
770
- parameters: {
771
- ids: /* @__PURE__ */ new Map(),
772
- seen: /* @__PURE__ */ new WeakMap()
773
- },
774
- callbacks: {
775
- ids: /* @__PURE__ */ new Map(),
776
- seen: /* @__PURE__ */ new WeakMap()
777
- },
778
- pathItems: {
779
- ids: /* @__PURE__ */ new Map(),
780
- seen: /* @__PURE__ */ new WeakMap()
781
- }
782
- };
783
- registerSchemas(components == null ? void 0 : components.schemas, registry);
784
- registerParameters(components == null ? void 0 : components.parameters, registry);
785
- registerHeaders(components == null ? void 0 : components.headers, registry);
786
- registerResponses(components == null ? void 0 : components.responses, registry);
787
- registerPathItems(components == null ? void 0 : components.pathItems, registry);
788
- registerRequestBodies(components == null ? void 0 : components.requestBodies, registry);
789
- registerCallbacks(components == null ? void 0 : components.callbacks, registry);
790
- return registry;
791
- };
792
- const registerSchemas = (schemas, registry) => {
793
- if (!schemas) {
794
- return;
795
- }
796
- for (const [key, schema] of Object.entries(schemas)) {
797
- if (registry.schemas.ids.has(key)) {
798
- throw new Error(`Schema "${key}" is already registered`);
799
- }
800
- if (isAnyZodType(schema)) {
801
- const inputSchemaObject = {};
802
- const outputSchemaObject = {};
803
- const identifier = `components > schemas > ${key}`;
804
- registry.schemas.input.set(identifier, {
805
- zodType: schema,
806
- schemaObject: inputSchemaObject
807
- });
808
- registry.schemas.output.set(identifier, {
809
- zodType: schema,
810
- schemaObject: outputSchemaObject
811
- });
812
- registry.schemas.manual.set(key, {
813
- identifier,
814
- io: {
815
- input: {
816
- schemaObject: inputSchemaObject,
817
- used: 0
818
- },
819
- output: {
820
- schemaObject: outputSchemaObject,
821
- used: 0
822
- }
823
- },
824
- zodType: schema
825
- });
826
- const meta = core.globalRegistry.get(schema);
827
- if (meta == null ? void 0 : meta.id) {
828
- continue;
829
- }
830
- core.globalRegistry.add(schema, {
831
- ...meta,
832
- id: key
833
- });
834
- continue;
835
- }
836
- registry.schemas.ids.set(key, schema);
837
- }
838
- };
839
- const registerParameters = (parameters, registry) => {
840
- if (!parameters) {
841
- return;
842
- }
843
- for (const [key, schema] of Object.entries(parameters)) {
844
- if (registry.parameters.ids.has(key)) {
845
- throw new Error(`Parameter "${key}" is already registered`);
846
- }
847
- if (isAnyZodType(schema)) {
848
- const path = ["components", "parameters", key];
849
- const paramObject = createParameter(
850
- schema,
851
- void 0,
852
- {
853
- registry,
854
- io: "input"
855
- },
856
- path
857
- );
858
- registry.parameters.ids.set(key, paramObject);
859
- registry.parameters.seen.set(schema, paramObject);
860
- continue;
861
- }
862
- registry.parameters.ids.set(key, schema);
863
- }
864
- };
865
- const registerHeaders = (headers, registry) => {
866
- if (!headers) {
867
- return;
868
- }
869
- for (const [key, schema] of Object.entries(headers)) {
870
- if (registry.headers.ids.has(key)) {
871
- throw new Error(`Header "${key}" is already registered`);
872
- }
873
- if (isAnyZodType(schema)) {
874
- const path = ["components", "headers", key];
875
- const headerObject = createHeader(
876
- schema,
877
- {
878
- registry,
879
- io: "output"
880
- },
881
- path
882
- );
883
- registry.headers.ids.set(key, headerObject);
884
- registry.headers.seen.set(schema, headerObject);
885
- continue;
886
- }
887
- registry.headers.ids.set(key, schema);
888
- }
889
- };
890
- const registerResponses = (responses, registry) => {
891
- if (!responses) {
892
- return;
893
- }
894
- for (const [key, schema] of Object.entries(responses)) {
895
- const path = ["components", "responses", key];
896
- if (registry.responses.ids.has(key)) {
897
- throw new Error(`Response "${key}" is already registered`);
898
- }
899
- const responseObject = createResponse(
900
- schema,
901
- {
902
- registry,
903
- io: "output"
904
- },
905
- path
906
- );
907
- registry.responses.ids.set(key, responseObject);
908
- registry.responses.seen.set(schema, responseObject);
909
- }
910
- };
911
- const registerRequestBodies = (requestBodies, registry) => {
912
- if (!requestBodies) {
913
- return;
914
- }
915
- for (const [key, schema] of Object.entries(requestBodies)) {
916
- if (registry.requestBodies.ids.has(key)) {
917
- throw new Error(`RequestBody "${key}" is already registered`);
918
- }
919
- if (isAnyZodType(schema)) {
920
- const path = ["components", "requestBodies", key];
921
- const requestBodyObject = createRequestBody(
922
- schema,
923
- {
924
- registry,
925
- io: "input"
926
- },
927
- path
928
- );
929
- registry.requestBodies.ids.set(key, requestBodyObject);
930
- continue;
931
- }
932
- registry.requestBodies.ids.set(key, schema);
933
- }
934
- };
935
- const registerCallbacks = (callbacks, registry) => {
936
- if (!callbacks) {
937
- return;
938
- }
939
- for (const [key, schema] of Object.entries(callbacks)) {
940
- if (registry.callbacks.ids.has(key)) {
941
- throw new Error(`Callback "${key}" is already registered`);
942
- }
943
- const path = ["components", "callbacks", key];
944
- const callbackObject = createCallback(schema, registry, path);
945
- registry.callbacks.ids.set(key, callbackObject);
946
- registry.callbacks.seen.set(schema, callbackObject);
947
- }
948
- };
949
- const registerPathItems = (pathItems, registry) => {
950
- if (!pathItems) {
951
- return;
952
- }
953
- for (const [key, schema] of Object.entries(pathItems)) {
954
- if (registry.pathItems.ids.has(key)) {
955
- throw new Error(`PathItem "${key}" is already registered`);
956
- }
957
- const path = ["components", "pathItems", key];
958
- const pathItemObject = createPathItem(schema, registry, path);
959
- registry.pathItems.ids.set(key, pathItemObject);
960
- registry.pathItems.seen.set(schema, pathItemObject);
961
- continue;
962
- }
963
- };
964
- const createIOSchemas = (ctx) => {
965
- const { schemas, components } = createSchemas(
966
- Object.fromEntries(ctx.registry.schemas[ctx.io]),
967
- ctx
968
- );
969
- for (const [key, schema] of Object.entries(components)) {
970
- ctx.registry.schemas.ids.set(key, schema);
971
- }
972
- for (const [key, schema] of Object.entries(schemas)) {
973
- const ioSchema = ctx.registry.schemas[ctx.io].get(key);
974
- if (ioSchema) {
975
- Object.assign(ioSchema.schemaObject, schema);
976
- }
977
- }
978
- };
979
- const createManualSchemas = (registry) => {
980
- var _a;
981
- for (const [key, value] of registry.schemas.manual) {
982
- if (value.io.input.used === 1) {
983
- const io = ((_a = core.globalRegistry.get(value.zodType)) == null ? void 0 : _a.unusedIO) ?? "output";
984
- const schema = value.io[io].schemaObject;
985
- registry.schemas.ids.set(key, schema);
986
- }
987
- }
988
- };
989
- const createComponents = (registry, opts) => {
990
- createIOSchemas({ registry, io: "input", opts });
991
- createIOSchemas({ registry, io: "output", opts });
992
- createManualSchemas(registry);
993
- const components = {};
994
- if (registry.schemas.ids.size > 0) {
995
- components.schemas = Object.fromEntries(registry.schemas.ids);
996
- }
997
- if (registry.headers.ids.size > 0) {
998
- components.headers = Object.fromEntries(registry.headers.ids);
999
- }
1000
- if (registry.requestBodies.ids.size > 0) {
1001
- components.requestBodies = Object.fromEntries(registry.requestBodies.ids);
1002
- }
1003
- if (registry.responses.ids.size > 0) {
1004
- components.responses = Object.fromEntries(registry.responses.ids);
1005
- }
1006
- if (registry.parameters.ids.size > 0) {
1007
- components.parameters = Object.fromEntries(registry.parameters.ids);
1008
- }
1009
- if (registry.callbacks.ids.size > 0) {
1010
- components.callbacks = Object.fromEntries(registry.callbacks.ids);
1011
- }
1012
- if (registry.pathItems.ids.size > 0) {
1013
- components.pathItems = Object.fromEntries(registry.pathItems.ids);
1014
- }
1015
- return components;
1016
- };
1017
- exports.createComponents = createComponents;
1018
- exports.createMediaTypeObject = createMediaTypeObject;
1019
- exports.createParameter = createParameter;
1020
- exports.createPaths = createPaths;
1021
- exports.createRegistry = createRegistry;
1022
- exports.createSchema = createSchema;
1023
- exports.unwrapZodObject = unwrapZodObject;