MCPcopy Create free account
hub / github.com/ShipSecAI/studio / zodToJsonSchema

Function zodToJsonSchema

packages/component-sdk/src/json-schema.ts:65–184  ·  view source on GitHub ↗

* Convert Zod schema to JSON Schema format * Simplified implementation - may use zod-to-json-schema for complex cases

(schema: z.ZodTypeAny)

Source from the content-addressed store, hash-verified

63 * Simplified implementation - may use zod-to-json-schema for complex cases
64 */
65function zodToJsonSchema(schema: z.ZodTypeAny): Record<string, unknown> {
66 const def = (schema as any)._def as ZodDef | undefined;
67 const typeName = getDefType(def);
68
69 // Handle ZodObject
70 if (typeName === 'object') {
71 const properties: Record<string, unknown> = {};
72 const required: string[] = [];
73
74 for (const [key, field] of Object.entries(getObjectShape(schema))) {
75 const fieldSchema = field as z.ZodTypeAny;
76 properties[key] = zodToJsonSchema(fieldSchema);
77
78 // Check if field is required
79 if (!isOptional(fieldSchema)) {
80 required.push(key);
81 }
82 }
83
84 return {
85 type: 'object',
86 properties,
87 ...(required.length > 0 ? { required } : {}),
88 };
89 }
90
91 // Handle ZodString
92 if (typeName === 'string') {
93 return { type: 'string' };
94 }
95
96 // Handle ZodNumber
97 if (typeName === 'number') {
98 return { type: 'number' };
99 }
100
101 // Handle ZodBoolean
102 if (typeName === 'boolean') {
103 return { type: 'boolean' };
104 }
105
106 // Handle ZodArray
107 if (typeName === 'array') {
108 const element = (def as any).element ?? (def as any).type;
109 return {
110 type: 'array',
111 items: element ? zodToJsonSchema(element) : {},
112 };
113 }
114
115 // Handle ZodRecord
116 if (typeName === 'record') {
117 const valueType = (def as any).valueType ?? (def as any).value ?? (def as any).keyType;
118 return {
119 type: 'object',
120 additionalProperties: valueType ? zodToJsonSchema(valueType) : {},
121 };
122 }

Callers 1

generateJsonSchemaFunction · 0.85

Calls 4

getDefTypeFunction · 0.70
getObjectShapeFunction · 0.70
isOptionalFunction · 0.70
pushMethod · 0.65

Tested by

no test coverage detected