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

Function convertBaseSchema

packages/zod/src/v4/classic/from-json-schema.ts:146–531  ·  view source on GitHub ↗
(schema: JSONSchema.JSONSchema, ctx: ConversionContext)

Source from the content-addressed store, hash-verified

144}
145
146function convertBaseSchema(schema: JSONSchema.JSONSchema, ctx: ConversionContext): ZodType {
147 // Handle unsupported features
148 if (schema.not !== undefined) {
149 // Special case: { not: {} } represents never
150 if (typeof schema.not === "object" && Object.keys(schema.not).length === 0) {
151 return z.never();
152 }
153 throw new Error("not is not supported in Zod (except { not: {} } for never)");
154 }
155 if (schema.unevaluatedItems !== undefined) {
156 throw new Error("unevaluatedItems is not supported");
157 }
158 if (schema.unevaluatedProperties !== undefined) {
159 throw new Error("unevaluatedProperties is not supported");
160 }
161 if (schema.if !== undefined || schema.then !== undefined || schema.else !== undefined) {
162 throw new Error("Conditional schemas (if/then/else) are not supported");
163 }
164 if (schema.dependentSchemas !== undefined || schema.dependentRequired !== undefined) {
165 throw new Error("dependentSchemas and dependentRequired are not supported");
166 }
167
168 // Handle $ref
169 if (schema.$ref) {
170 const refPath = schema.$ref;
171 if (ctx.refs.has(refPath)) {
172 return ctx.refs.get(refPath)!;
173 }
174
175 if (ctx.processing.has(refPath)) {
176 // Circular reference - use lazy
177 return z.lazy(() => {
178 if (!ctx.refs.has(refPath)) {
179 throw new Error(`Circular reference not resolved: ${refPath}`);
180 }
181 return ctx.refs.get(refPath)!;
182 });
183 }
184
185 ctx.processing.add(refPath);
186 const resolved = resolveRef(refPath, ctx);
187 const zodSchema = convertSchema(resolved, ctx);
188 ctx.refs.set(refPath, zodSchema);
189 ctx.processing.delete(refPath);
190 return zodSchema;
191 }
192
193 // Handle enum
194 if (schema.enum !== undefined) {
195 const enumValues = schema.enum;
196
197 // Special case: OpenAPI 3.0 null representation { type: "string", nullable: true, enum: [null] }
198 if (
199 ctx.version === "openapi-3.0" &&
200 schema.nullable === true &&
201 enumValues.length === 1 &&
202 enumValues[0] === null
203 ) {

Callers 1

convertSchemaFunction · 0.85

Calls 15

resolveRefFunction · 0.85
convertSchemaFunction · 0.85
hasMethod · 0.80
getMethod · 0.80
addMethod · 0.80
nullMethod · 0.80
stringMethod · 0.80
ipv4Method · 0.80
ipv6Method · 0.80
cidrv4Method · 0.80
cidrv6Method · 0.80
e164Method · 0.80

Tested by

no test coverage detected