MCPcopy
hub / github.com/colinhacks/zod / resolveRef

Function resolveRef

packages/zod/src/v4/classic/from-json-schema.ts:121–144  ·  view source on GitHub ↗
(ref: string, ctx: ConversionContext)

Source from the content-addressed store, hash-verified

119}
120
121function resolveRef(ref: string, ctx: ConversionContext): JSONSchema.JSONSchema {
122 if (!ref.startsWith("#")) {
123 throw new Error("External $ref is not supported, only local refs (#/...) are allowed");
124 }
125
126 const path = ref.slice(1).split("/").filter(Boolean);
127
128 // Handle root reference "#"
129 if (path.length === 0) {
130 return ctx.rootSchema;
131 }
132
133 const defsKey = ctx.version === "draft-2020-12" ? "$defs" : "definitions";
134
135 if (path[0] === defsKey) {
136 const key = path[1];
137 if (!key || !ctx.defs[key]) {
138 throw new Error(`Reference not found: ${ref}`);
139 }
140 return ctx.defs[key]!;
141 }
142
143 throw new Error(`Reference not found: ${ref}`);
144}
145
146function convertBaseSchema(schema: JSONSchema.JSONSchema, ctx: ConversionContext): ZodType {
147 // Handle unsupported features

Callers 1

convertBaseSchemaFunction · 0.85

Calls 1

startsWithMethod · 0.65

Tested by

no test coverage detected