zod-nest 2.1.2 → 2.1.3

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
@@ -434,6 +434,60 @@ var rewriteRefs = /* @__PURE__ */ __name((node, selfRef) => {
434
434
  rewriteRefs(value, selfRef);
435
435
  }
436
436
  }, "rewriteRefs");
437
+ var refsIn = /* @__PURE__ */ __name(function* (node) {
438
+ if (Array.isArray(node)) {
439
+ for (const item of node) {
440
+ yield* refsIn(item);
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;
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 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;
479
+ }
480
+ refs.delete(id);
481
+ return body;
482
+ }, "liftRoot");
483
+ var stripEnvelope = /* @__PURE__ */ __name((raw) => {
484
+ const root = {
485
+ ...raw
486
+ };
487
+ delete root.$schema;
488
+ delete root.$defs;
489
+ return root;
490
+ }, "stripEnvelope");
437
491
  var postProcess = /* @__PURE__ */ __name((raw) => {
438
492
  const refs = /* @__PURE__ */ new Map();
439
493
  const rawDefs = raw.$defs;
@@ -442,11 +496,7 @@ var postProcess = /* @__PURE__ */ __name((raw) => {
442
496
  refs.set(id, body);
443
497
  }
444
498
  }
445
- const root = {
446
- ...raw
447
- };
448
- delete root.$schema;
449
- delete root.$defs;
499
+ const root = liftRoot(raw, refs) ?? stripEnvelope(raw);
450
500
  rewriteRefs(root, void 0);
451
501
  for (const [id, body] of refs) {
452
502
  rewriteRefs(body, `${COMPONENTS_SCHEMAS_PREFIX}${id}`);