zod-nest 2.1.2 → 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,32 +427,120 @@ 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
- var postProcess = /* @__PURE__ */ __name((raw) => {
438
- const refs = /* @__PURE__ */ new Map();
439
- const rawDefs = raw.$defs;
440
- if (rawDefs !== void 0) {
441
- for (const [id, body] of Object.entries(rawDefs)) {
442
- refs.set(id, body);
437
+ var refsIn = /* @__PURE__ */ __name(function* (node) {
438
+ if (Array.isArray(node)) {
439
+ for (const item of node) {
440
+ yield* refsIn(item);
443
441
  }
442
+ return;
443
+ }
444
+ if (node === null || typeof node !== "object") {
445
+ return;
446
+ }
447
+ const obj = node;
448
+ if (typeof obj.$ref === "string") {
449
+ yield obj.$ref;
444
450
  }
451
+ for (const value of Object.values(obj)) {
452
+ yield* refsIn(value);
453
+ }
454
+ }, "refsIn");
455
+ var rootDefId = /* @__PURE__ */ __name((raw) => {
456
+ const ref = raw.$ref;
457
+ if (typeof ref !== "string" || !ref.startsWith(DEFS_PREFIX)) {
458
+ return void 0;
459
+ }
460
+ return ref.slice(DEFS_PREFIX.length);
461
+ }, "rootDefId");
462
+ var isReferenced = /* @__PURE__ */ __name((defs, id) => {
463
+ const ownRef = `${DEFS_PREFIX}${id}`;
464
+ for (const ref of refsIn(defs)) {
465
+ if (ref === "#" || ref === ownRef) {
466
+ return true;
467
+ }
468
+ }
469
+ return false;
470
+ }, "isReferenced");
471
+ var referencesRoot = /* @__PURE__ */ __name((raw) => {
472
+ for (const ref of refsIn(raw)) {
473
+ if (ref === "#") {
474
+ return true;
475
+ }
476
+ }
477
+ return false;
478
+ }, "referencesRoot");
479
+ var stripEnvelope = /* @__PURE__ */ __name((raw) => {
445
480
  const root = {
446
481
  ...raw
447
482
  };
448
483
  delete root.$schema;
449
484
  delete root.$defs;
450
- rewriteRefs(root, void 0);
451
- for (const [id, body] of refs) {
452
- rewriteRefs(body, `${COMPONENTS_SCHEMAS_PREFIX}${id}`);
485
+ return root;
486
+ }, "stripEnvelope");
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
+ };
453
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}`;
454
522
  return {
455
- schema: root,
523
+ schema: {
524
+ $ref: rootRef
525
+ },
526
+ rootRef
527
+ };
528
+ }, "resolveRoot");
529
+ var postProcess = /* @__PURE__ */ __name((raw, options) => {
530
+ const refs = /* @__PURE__ */ new Map();
531
+ const rawDefs = raw.$defs;
532
+ if (rawDefs !== void 0) {
533
+ for (const [id, body] of Object.entries(rawDefs)) {
534
+ refs.set(id, body);
535
+ }
536
+ }
537
+ const { schema, rootRef } = resolveRoot(raw, refs, options);
538
+ rewriteRefs(schema, rootRef);
539
+ for (const body of refs.values()) {
540
+ rewriteRefs(body, rootRef);
541
+ }
542
+ return {
543
+ schema,
456
544
  refs
457
545
  };
458
546
  }, "postProcess");
@@ -539,6 +627,10 @@ var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
539
627
  }, "consumeUnrepresentable")
540
628
  };
541
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");
542
634
  var toOpenApi = /* @__PURE__ */ __name((schema, opts) => {
543
635
  const built = buildToJsonSchemaOptions({
544
636
  registry: opts.registry,
@@ -549,7 +641,10 @@ var toOpenApi = /* @__PURE__ */ __name((schema, opts) => {
549
641
  });
550
642
  const raw = z.toJSONSchema(schema, built.options);
551
643
  built.consumeUnrepresentable();
552
- const result = postProcess(raw);
644
+ const result = postProcess(raw, {
645
+ rootId: resolveRootId(schema, opts.registry),
646
+ strict: opts.strict
647
+ });
553
648
  for (const [id] of opts.registry.getCollisions()) {
554
649
  if (!result.refs.has(id)) {
555
650
  continue;