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

Function toJSONSchema

packages/zod/src/v4/core/json-schema-processors.ts:618–666  ·  view source on GitHub ↗
(
  input: schemas.$ZodType | $ZodRegistry<{ id?: string | undefined }>,
  params?: ToJSONSchemaParams | RegistryToJSONSchemaParams
)

Source from the content-addressed store, hash-verified

616 params?: RegistryToJSONSchemaParams
617): { schemas: Record<string, ZodStandardJSONSchemaPayload<schemas.$ZodType>> };
618export function toJSONSchema(
619 input: schemas.$ZodType | $ZodRegistry<{ id?: string | undefined }>,
620 params?: ToJSONSchemaParams | RegistryToJSONSchemaParams
621): any {
622 if ("_idmap" in input) {
623 // Registry case
624 const registry = input as $ZodRegistry<{ id?: string | undefined }>;
625 const ctx = initializeContext({ ...params, processors: allProcessors });
626 const defs: any = {};
627
628 // First pass: process all schemas to build the seen map
629 for (const entry of registry._idmap.entries()) {
630 const [_, schema] = entry;
631 process(schema, ctx as any);
632 }
633
634 const schemas: Record<string, JSONSchema.BaseSchema> = {};
635 const external = {
636 registry,
637 uri: (params as RegistryToJSONSchemaParams)?.uri,
638 defs,
639 };
640
641 // Update the context with external configuration
642 ctx.external = external;
643
644 // Second pass: emit each schema
645 for (const entry of registry._idmap.entries()) {
646 const [key, schema] = entry;
647 extractDefs(ctx as any, schema);
648 schemas[key] = finalize(ctx as any, schema);
649 }
650
651 if (Object.keys(defs).length > 0) {
652 const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
653 schemas.__shared = {
654 [defsSegment]: defs,
655 };
656 }
657
658 return { schemas };
659 }
660
661 // Single schema case
662 const ctx = initializeContext({ ...params, processors: allProcessors });
663 process(input, ctx as any);
664 extractDefs(ctx as any, input);
665 return finalize(ctx as any, input);
666}

Callers

nothing calls this directly

Calls 4

initializeContextFunction · 0.90
processFunction · 0.90
extractDefsFunction · 0.90
finalizeFunction · 0.90

Tested by

no test coverage detected