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.mjs CHANGED
@@ -413,10 +413,10 @@ var combine = /* @__PURE__ */ __name((...overrides) => {
413
413
  }, "combine");
414
414
 
415
415
  // src/schema/post-process.ts
416
- var rewriteRefs = /* @__PURE__ */ __name((node, selfRef) => {
416
+ var rewriteRefs = /* @__PURE__ */ __name((node, rootRef) => {
417
417
  if (Array.isArray(node)) {
418
418
  for (const item of node) {
419
- rewriteRefs(item, selfRef);
419
+ rewriteRefs(item, rootRef);
420
420
  }
421
421
  return;
422
422
  }
@@ -427,11 +427,11 @@ var rewriteRefs = /* @__PURE__ */ __name((node, selfRef) => {
427
427
  const ref = obj.$ref;
428
428
  if (typeof ref === "string" && ref.startsWith(DEFS_PREFIX)) {
429
429
  obj.$ref = COMPONENTS_SCHEMAS_PREFIX + ref.slice(DEFS_PREFIX.length);
430
- } else if (ref === "#" && selfRef !== void 0) {
431
- obj.$ref = selfRef;
430
+ } else if (ref === "#" && rootRef !== void 0) {
431
+ obj.$ref = rootRef;
432
432
  }
433
433
  for (const value of Object.values(obj)) {
434
- rewriteRefs(value, selfRef);
434
+ rewriteRefs(value, rootRef);
435
435
  }
436
436
  }, "rewriteRefs");
437
437
  var refsIn = /* @__PURE__ */ __name(function* (node) {
@@ -468,18 +468,14 @@ var isReferenced = /* @__PURE__ */ __name((defs, id) => {
468
468
  }
469
469
  return false;
470
470
  }, "isReferenced");
471
- var liftRoot = /* @__PURE__ */ __name((raw, refs) => {
472
- const id = rootDefId(raw);
473
- if (id === void 0) {
474
- return void 0;
475
- }
476
- const body = refs.get(id);
477
- if (body === void 0 || isReferenced(raw.$defs, id)) {
478
- return void 0;
471
+ var referencesRoot = /* @__PURE__ */ __name((raw) => {
472
+ for (const ref of refsIn(raw)) {
473
+ if (ref === "#") {
474
+ return true;
475
+ }
479
476
  }
480
- refs.delete(id);
481
- return body;
482
- }, "liftRoot");
477
+ return false;
478
+ }, "referencesRoot");
483
479
  var stripEnvelope = /* @__PURE__ */ __name((raw) => {
484
480
  const root = {
485
481
  ...raw
@@ -488,7 +484,49 @@ var stripEnvelope = /* @__PURE__ */ __name((raw) => {
488
484
  delete root.$defs;
489
485
  return root;
490
486
  }, "stripEnvelope");
491
- var postProcess = /* @__PURE__ */ __name((raw) => {
487
+ var resolveRoot = /* @__PURE__ */ __name((raw, refs, options) => {
488
+ const liftedId = rootDefId(raw);
489
+ if (liftedId !== void 0) {
490
+ const body = refs.get(liftedId);
491
+ if (body !== void 0 && !isReferenced(raw.$defs, liftedId)) {
492
+ refs.delete(liftedId);
493
+ return {
494
+ schema: body,
495
+ rootRef: void 0
496
+ };
497
+ }
498
+ return {
499
+ schema: stripEnvelope(raw),
500
+ rootRef: `${COMPONENTS_SCHEMAS_PREFIX}${liftedId}`
501
+ };
502
+ }
503
+ const root = stripEnvelope(raw);
504
+ if (!referencesRoot(raw)) {
505
+ return {
506
+ schema: root,
507
+ rootRef: void 0
508
+ };
509
+ }
510
+ const rootId = options?.rootId;
511
+ if (rootId === void 0) {
512
+ if (options?.strict ?? true) {
513
+ 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.');
514
+ }
515
+ return {
516
+ schema: root,
517
+ rootRef: void 0
518
+ };
519
+ }
520
+ refs.set(rootId, root);
521
+ const rootRef = `${COMPONENTS_SCHEMAS_PREFIX}${rootId}`;
522
+ return {
523
+ schema: {
524
+ $ref: rootRef
525
+ },
526
+ rootRef
527
+ };
528
+ }, "resolveRoot");
529
+ var postProcess = /* @__PURE__ */ __name((raw, options) => {
492
530
  const refs = /* @__PURE__ */ new Map();
493
531
  const rawDefs = raw.$defs;
494
532
  if (rawDefs !== void 0) {
@@ -496,13 +534,13 @@ var postProcess = /* @__PURE__ */ __name((raw) => {
496
534
  refs.set(id, body);
497
535
  }
498
536
  }
499
- const root = liftRoot(raw, refs) ?? stripEnvelope(raw);
500
- rewriteRefs(root, void 0);
501
- for (const [id, body] of refs) {
502
- rewriteRefs(body, `${COMPONENTS_SCHEMAS_PREFIX}${id}`);
537
+ const { schema, rootRef } = resolveRoot(raw, refs, options);
538
+ rewriteRefs(schema, rootRef);
539
+ for (const body of refs.values()) {
540
+ rewriteRefs(body, rootRef);
503
541
  }
504
542
  return {
505
- schema: root,
543
+ schema,
506
544
  refs
507
545
  };
508
546
  }, "postProcess");
@@ -589,6 +627,10 @@ var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
589
627
  }, "consumeUnrepresentable")
590
628
  };
591
629
  }, "buildToJsonSchemaOptions");
630
+ var resolveRootId = /* @__PURE__ */ __name((schema, registry) => {
631
+ const id = registry.zodRegistry.get(schema)?.id;
632
+ return typeof id === "string" && id !== "" ? id : void 0;
633
+ }, "resolveRootId");
592
634
  var toOpenApi = /* @__PURE__ */ __name((schema, opts) => {
593
635
  const built = buildToJsonSchemaOptions({
594
636
  registry: opts.registry,
@@ -599,7 +641,10 @@ var toOpenApi = /* @__PURE__ */ __name((schema, opts) => {
599
641
  });
600
642
  const raw = z.toJSONSchema(schema, built.options);
601
643
  built.consumeUnrepresentable();
602
- const result = postProcess(raw);
644
+ const result = postProcess(raw, {
645
+ rootId: resolveRootId(schema, opts.registry),
646
+ strict: opts.strict
647
+ });
603
648
  for (const [id] of opts.registry.getCollisions()) {
604
649
  if (!result.refs.has(id)) {
605
650
  continue;