zod-nest 2.1.3 → 2.1.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.
package/dist/index.js CHANGED
@@ -419,10 +419,10 @@ var combine = /* @__PURE__ */ __name((...overrides) => {
419
419
  }, "combine");
420
420
 
421
421
  // src/schema/post-process.ts
422
- var rewriteRefs = /* @__PURE__ */ __name((node, selfRef) => {
422
+ var rewriteRefs = /* @__PURE__ */ __name((node, rootRef) => {
423
423
  if (Array.isArray(node)) {
424
424
  for (const item of node) {
425
- rewriteRefs(item, selfRef);
425
+ rewriteRefs(item, rootRef);
426
426
  }
427
427
  return;
428
428
  }
@@ -433,11 +433,11 @@ var rewriteRefs = /* @__PURE__ */ __name((node, selfRef) => {
433
433
  const ref = obj.$ref;
434
434
  if (typeof ref === "string" && ref.startsWith(DEFS_PREFIX)) {
435
435
  obj.$ref = COMPONENTS_SCHEMAS_PREFIX + ref.slice(DEFS_PREFIX.length);
436
- } else if (ref === "#" && selfRef !== void 0) {
437
- obj.$ref = selfRef;
436
+ } else if (ref === "#" && rootRef !== void 0) {
437
+ obj.$ref = rootRef;
438
438
  }
439
439
  for (const value of Object.values(obj)) {
440
- rewriteRefs(value, selfRef);
440
+ rewriteRefs(value, rootRef);
441
441
  }
442
442
  }, "rewriteRefs");
443
443
  var refsIn = /* @__PURE__ */ __name(function* (node) {
@@ -474,18 +474,14 @@ var isReferenced = /* @__PURE__ */ __name((defs, id) => {
474
474
  }
475
475
  return false;
476
476
  }, "isReferenced");
477
- var liftRoot = /* @__PURE__ */ __name((raw, refs) => {
478
- const id = rootDefId(raw);
479
- if (id === void 0) {
480
- return void 0;
481
- }
482
- const body = refs.get(id);
483
- if (body === void 0 || isReferenced(raw.$defs, id)) {
484
- return void 0;
477
+ var referencesRoot = /* @__PURE__ */ __name((raw) => {
478
+ for (const ref of refsIn(raw)) {
479
+ if (ref === "#") {
480
+ return true;
481
+ }
485
482
  }
486
- refs.delete(id);
487
- return body;
488
- }, "liftRoot");
483
+ return false;
484
+ }, "referencesRoot");
489
485
  var stripEnvelope = /* @__PURE__ */ __name((raw) => {
490
486
  const root = {
491
487
  ...raw
@@ -494,7 +490,49 @@ var stripEnvelope = /* @__PURE__ */ __name((raw) => {
494
490
  delete root.$defs;
495
491
  return root;
496
492
  }, "stripEnvelope");
497
- var postProcess = /* @__PURE__ */ __name((raw) => {
493
+ var resolveRoot = /* @__PURE__ */ __name((raw, refs, options) => {
494
+ const liftedId = rootDefId(raw);
495
+ if (liftedId !== void 0) {
496
+ const body = refs.get(liftedId);
497
+ if (body !== void 0 && !isReferenced(raw.$defs, liftedId)) {
498
+ refs.delete(liftedId);
499
+ return {
500
+ schema: body,
501
+ rootRef: void 0
502
+ };
503
+ }
504
+ return {
505
+ schema: stripEnvelope(raw),
506
+ rootRef: `${COMPONENTS_SCHEMAS_PREFIX}${liftedId}`
507
+ };
508
+ }
509
+ const root = stripEnvelope(raw);
510
+ if (!referencesRoot(raw)) {
511
+ return {
512
+ schema: root,
513
+ rootRef: void 0
514
+ };
515
+ }
516
+ const rootId = options?.rootId;
517
+ if (rootId === void 0) {
518
+ if (options?.strict ?? true) {
519
+ throw new ZodNestError('A schema referenced from inside its own emission has no id, so the reference cannot be resolved. Add `.meta({ id: "\u2026" })` to the root schema, or set strict: false to leave the reference unresolved.');
520
+ }
521
+ return {
522
+ schema: root,
523
+ rootRef: void 0
524
+ };
525
+ }
526
+ refs.set(rootId, root);
527
+ const rootRef = `${COMPONENTS_SCHEMAS_PREFIX}${rootId}`;
528
+ return {
529
+ schema: {
530
+ $ref: rootRef
531
+ },
532
+ rootRef
533
+ };
534
+ }, "resolveRoot");
535
+ var postProcess = /* @__PURE__ */ __name((raw, options) => {
498
536
  const refs = /* @__PURE__ */ new Map();
499
537
  const rawDefs = raw.$defs;
500
538
  if (rawDefs !== void 0) {
@@ -502,13 +540,13 @@ var postProcess = /* @__PURE__ */ __name((raw) => {
502
540
  refs.set(id, body);
503
541
  }
504
542
  }
505
- const root = liftRoot(raw, refs) ?? stripEnvelope(raw);
506
- rewriteRefs(root, void 0);
507
- for (const [id, body] of refs) {
508
- rewriteRefs(body, `${COMPONENTS_SCHEMAS_PREFIX}${id}`);
543
+ const { schema, rootRef } = resolveRoot(raw, refs, options);
544
+ rewriteRefs(schema, rootRef);
545
+ for (const body of refs.values()) {
546
+ rewriteRefs(body, rootRef);
509
547
  }
510
548
  return {
511
- schema: root,
549
+ schema,
512
550
  refs
513
551
  };
514
552
  }, "postProcess");
@@ -595,6 +633,10 @@ var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
595
633
  }, "consumeUnrepresentable")
596
634
  };
597
635
  }, "buildToJsonSchemaOptions");
636
+ var resolveRootId = /* @__PURE__ */ __name((schema, registry) => {
637
+ const id = registry.zodRegistry.get(schema)?.id;
638
+ return typeof id === "string" && id !== "" ? id : void 0;
639
+ }, "resolveRootId");
598
640
  var toOpenApi = /* @__PURE__ */ __name((schema, opts) => {
599
641
  const built = buildToJsonSchemaOptions({
600
642
  registry: opts.registry,
@@ -605,7 +647,10 @@ var toOpenApi = /* @__PURE__ */ __name((schema, opts) => {
605
647
  });
606
648
  const raw = zod.z.toJSONSchema(schema, built.options);
607
649
  built.consumeUnrepresentable();
608
- const result = postProcess(raw);
650
+ const result = postProcess(raw, {
651
+ rootId: resolveRootId(schema, opts.registry),
652
+ strict: opts.strict
653
+ });
609
654
  for (const [id] of opts.registry.getCollisions()) {
610
655
  if (!result.refs.has(id)) {
611
656
  continue;