Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import * as z4 from 'zod/v4'; |
| 2 | import * as zodToJsonSchema from 'zod-to-json-schema'; |
| 3 | import type { AnyZodSchema } from '../types/customTool.types'; |
| 4 | |
| 5 | type JsonSchemaObject = Record<string, unknown>; |
| 6 | type ObjectJsonSchema = { |
| 7 | type: 'object'; |
| 8 | properties: Record<string, unknown>; |
| 9 | required?: string[]; |
| 10 | }; |
| 11 | type ZodDef = { |
| 12 | // Zod v4 stores a string literal ('object', 'array', ...) here. Zod v3 leaves it unset for |
| 13 | // most types, but for `z.array()` / `z.promise()` it holds the element/inner ZodType (an object), |
| 14 | // never a string — which is exactly what lets `isZodV4Schema` tell the two runtimes apart. |
| 15 | type?: unknown; |
| 16 | typeName?: string; |
| 17 | }; |
| 18 | type ZodJsonSchemaMode = 'input' | 'output'; |
| 19 | |
| 20 | const getZodDef = (value: unknown): ZodDef | undefined => |
| 21 | typeof value === 'object' && value !== null ? (value as { _def?: ZodDef })._def : undefined; |
| 22 | |
| 23 | // Discriminate Zod v4 from the bundled v3 compat layer. v4-native exposes `_def.type` as a string |
| 24 | // literal; v3 exposes `_def.typeName` and, when `_def.type` is present (arrays/promises), it is a |
| 25 | // ZodType object, not a string. Re-verify this invariant on a Zod major bump. |
| 26 | const isZodV4Schema = (schema: AnyZodSchema): schema is z4.ZodType => |
| 27 | typeof getZodDef(schema)?.type === 'string'; |
| 28 | |
| 29 | const stripSchemaKeyword = (schema: JsonSchemaObject): JsonSchemaObject => { |
| 30 | const { $schema: _schema, ...rest } = schema; |
| 31 | return rest; |
| 32 | }; |
| 33 | |
| 34 | const getNamedDefinition = (schema: JsonSchemaObject, name: string): JsonSchemaObject => { |
| 35 | const definitions = schema.definitions as Record<string, JsonSchemaObject> | undefined; |
| 36 | const definition = definitions?.[name]; |
| 37 | // zod-to-json-schema with `{ name }` always nests the schema under `definitions[name]`. If it's |
| 38 | // missing, the conversion didn't match the expected contract — fail loudly rather than returning |
| 39 | // the wrapper document, which downstream would silently degrade to an empty object schema. |
| 40 | if (typeof definition !== 'object' || definition === null) { |
| 41 | throw new Error(`Expected definition "${name}" in the converted JSON schema.`); |
| 42 | } |
| 43 | |
| 44 | return definition; |
| 45 | }; |
| 46 | |
| 47 | export const isZodObjectSchema = (value: unknown): value is AnyZodSchema => { |
| 48 | const def = getZodDef(value); |
| 49 | return def?.typeName === 'ZodObject' || def?.type === 'object'; |
| 50 | }; |
| 51 | |
| 52 | export const zodSchemaToJsonSchema = ( |
| 53 | schema: AnyZodSchema, |
| 54 | name: string = 'schema', |
| 55 | mode: ZodJsonSchemaMode = 'output' |
| 56 | ): JsonSchemaObject => { |
| 57 | if (isZodV4Schema(schema)) { |
| 58 | return stripSchemaKeyword(z4.toJSONSchema(schema, { io: mode })); |
| 59 | } |
| 60 | |
| 61 | // Mirror the v4 `io` mode on the v3 converter so the two runtimes serialize `.default()`, |
| 62 | // `.transform()` and `.pipe()` schemas consistently at this boundary. |
| 63 | const converted = zodToJsonSchema.default(schema, { |
| 64 | name, |
| 65 | pipeStrategy: mode, |
| 66 | effectStrategy: mode === 'input' ? 'input' : 'any', |
| 67 | }); |
| 68 | |
| 69 | return stripSchemaKeyword(getNamedDefinition(converted, name)); |
| 70 | }; |
| 71 | |
| 72 | export const zodObjectSchemaToJsonSchema = ( |
| 73 | schema: AnyZodSchema, |
| 74 | name: string = 'schema', |
| 75 | mode: ZodJsonSchemaMode = 'input' |
| 76 | ): ObjectJsonSchema => { |
| 77 | if (!isZodObjectSchema(schema)) { |
| 78 | throw new Error('Expected a z.object() schema.'); |
| 79 | } |
| 80 | |
| 81 | // The schema is a confirmed object and getNamedDefinition guarantees a well-formed conversion, |
| 82 | // so the result always carries `properties` (and `required` only when there are required keys). |
| 83 | const jsonSchema = zodSchemaToJsonSchema(schema, name, mode); |
| 84 | |
| 85 | return { |
| 86 | type: 'object', |
| 87 | properties: (jsonSchema.properties as Record<string, unknown>) ?? {}, |
| 88 | ...(jsonSchema.required ? { required: jsonSchema.required as string[] } : {}), |
| 89 | }; |
| 90 | }; |