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 type { ZodType, ZodTypeAny } from 'zod/v3'; |
| 2 | |
| 3 | import { zodV3Backend } from './backend/zod-v3'; |
| 4 | import { createConverter } from './convert'; |
| 5 | import type { JsonSchemaToZodOptions, JsonSchema } from './types'; |
| 6 | |
| 7 | const converter = createConverter(zodV3Backend); |
| 8 | |
| 9 | export const jsonSchemaToZod = ( |
| 10 | schema: JsonSchema, |
| 11 | options: JsonSchemaToZodOptions = {} |
| 12 | ): ZodType => converter.jsonSchemaToZod(schema, options); |
| 13 | |
| 14 | /** |
| 15 | * A simplified type for Zod raw shapes that avoids deep type instantiation issues. |
| 16 | * This is compatible with ZodRawShape but uses a simpler type signature. |
| 17 | */ |
| 18 | export type SimpleZodRawShape = Record<string, ZodTypeAny>; |
| 19 | |
| 20 | /** |
| 21 | * Converts a JSON Schema object to a Zod raw shape. |
| 22 | * This is useful when you need the shape object (e.g., `{ name: z.string(), age: z.number() }`) |
| 23 | * rather than a complete ZodObject schema. |
| 24 | * |
| 25 | * This is particularly useful for APIs that expect ZodRawShape instead of ZodType, |
| 26 | * such as the Claude Agent SDK's `tool()` function. |
| 27 | * |
| 28 | * @param schema - The JSON Schema to convert (must be an object schema) |
| 29 | * @param options - Optional configuration for the conversion |
| 30 | * @returns A Zod raw shape object with property keys mapped to Zod types |
| 31 | */ |
| 32 | export const jsonSchemaToZodShape = ( |
| 33 | schema: JsonSchema, |
| 34 | options: JsonSchemaToZodOptions = {} |
| 35 | ): SimpleZodRawShape => converter.jsonSchemaToZodShape(schema, options); |