Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 07:15 UTC
| 1 | import { interopZodObjectStrict } from '@langchain/core/utils/types'; |
| 2 | import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | import * as z4 from 'zod/v4'; |
| 4 | |
| 5 | import { jsonSchemaToZod, jsonSchemaToZodShape } from '../../src/v4'; |
| 6 | import type { JsonSchema } from '../../src/types'; |
| 7 | |
| 8 | const guarded: JsonSchema = { |
| 9 | type: 'object', |
| 10 | properties: { |
| 11 | q: { type: 'string' }, |
| 12 | limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] }, |
| 13 | }, |
| 14 | required: ['q'], |
| 15 | }; |
| 16 | |
| 17 | const rejects = (schema: z4.ZodType, value: unknown) => { |
| 18 | const result = schema.safeParse(value); |
| 19 | expect(result.success).toBe(false); |
| 20 | return result.success ? [] : result.error.issues; |
| 21 | }; |
| 22 | |
| 23 | describe('v4 kind and guard survival', () => { |
| 24 | afterEach(() => { |
| 25 | z4.config({ jitless: false }); |
| 26 | }); |
| 27 | |
| 28 | it('keeps a guarded object root a ZodObject with its shape', () => { |
| 29 | const parsed = jsonSchemaToZod(guarded); |
| 30 | expect(parsed).toBeInstanceOf(z4.ZodObject); |
| 31 | expect(Object.keys((parsed as z4.ZodObject).shape)).toEqual(['q', 'limit']); |
| 32 | expect(parsed.safeParse({ q: 'cats', limit: null }).success).toBe(true); |
| 33 | rejects(parsed, { q: 'cats', limit: 'ten' }); |
| 34 | }); |
| 35 | |
| 36 | it('keeps the guard through describe, meta, and a definition clone', () => { |
| 37 | const parsed = jsonSchemaToZod({ ...guarded, description: 'Search' }) as z4.ZodObject; |
| 38 | const copies: z4.ZodType[] = [ |
| 39 | parsed.describe('Renamed'), |
| 40 | parsed.meta({ title: 'Search' }), |
| 41 | z4.core.clone(parsed, { ...parsed.def }), |
| 42 | z4.core.clone(parsed, { ...parsed.def, catchall: z4.never() }), |
| 43 | ]; |
| 44 | for (const copy of copies) { |
| 45 | expect(copy).toBeInstanceOf(z4.ZodObject); |
| 46 | rejects(copy, { q: 'cats', limit: 'ten' }); |
| 47 | expect(copy.safeParse({ q: 'cats' }).success).toBe(true); |
| 48 | } |
| 49 | expect(copies[0].description).toBe('Renamed'); |
| 50 | expect(copies[1].description).toBe('Search'); |
| 51 | expect(copies[1].meta()?.title).toBe('Search'); |
| 52 | }); |
| 53 | |
| 54 | it('keeps the guard through the LangChain strict clone', () => { |
| 55 | const parsed = jsonSchemaToZod(guarded) as z4.ZodObject; |
| 56 | const strict = interopZodObjectStrict(parsed, true) as unknown as z4.ZodType; |
| 57 | expect(strict).toBeInstanceOf(z4.ZodObject); |
| 58 | rejects(strict, { q: 'cats', limit: 'ten' }); |
| 59 | rejects(strict, { q: 'cats', extra: 1 }); |
| 60 | expect(strict.safeParse({ q: 'cats' }).success).toBe(true); |
| 61 | }); |
| 62 | |
| 63 | it.each([{ jitless: true }, { jitless: false }])( |
| 64 | 'runs a guarded property under the object parser with jitless $jitless', |
| 65 | ({ jitless }) => { |
| 66 | z4.config({ jitless }); |
| 67 | // A raw shape guards each property on its own, so the enclosing object |
| 68 | // is the plain `z.object()` a framework builds and the guard runs from |
| 69 | // inside its property loop. |
| 70 | const shape = jsonSchemaToZodShape({ |
| 71 | type: 'object', |
| 72 | properties: { |
| 73 | handle: { $ref: '#/$defs/handle' }, |
| 74 | count: { type: 'integer' }, |
| 75 | }, |
| 76 | $defs: { handle: { type: 'string', pattern: '^[a-z]+$' } }, |
| 77 | }); |
| 78 | const parsed = z4.object(shape); |
| 79 | for (let round = 0; round < 3; round++) { |
| 80 | expect(parsed.safeParse({ handle: 'ok', count: 1 }).success).toBe(true); |
| 81 | const issues = rejects(parsed, { handle: 'NO', count: 1 }); |
| 82 | expect(issues.map(issue => [issue.code, issue.path])).toEqual([['custom', ['handle']]]); |
| 83 | } |
| 84 | } |
| 85 | ); |
| 86 | |
| 87 | it('reports exactly one issue when the guard fails', () => { |
| 88 | const parsed = jsonSchemaToZod({ |
| 89 | type: 'object', |
| 90 | properties: { x: { type: 'string', default: 'generated' } }, |
| 91 | required: ['x'], |
| 92 | }); |
| 93 | const issues = rejects(parsed, {}); |
| 94 | expect(issues).toHaveLength(1); |
| 95 | expect(issues[0].code).toBe('custom'); |
| 96 | expect(issues[0].path).toEqual([]); |
| 97 | }); |
| 98 | |
| 99 | it('keeps a nested guard when the parent object is cloned', () => { |
| 100 | const parsed = jsonSchemaToZod({ |
| 101 | type: 'object', |
| 102 | properties: { ref: { $ref: '#/$defs/n' } }, |
| 103 | $defs: { n: { type: 'number', minimum: 1 } }, |
| 104 | }) as z4.ZodObject; |
| 105 | const extended = parsed.extend({ more: z4.string().optional() }); |
| 106 | rejects(extended, { ref: 0 }); |
| 107 | expect(extended.safeParse({ ref: 2, more: 'x' }).success).toBe(true); |
| 108 | }); |
| 109 | |
| 110 | it('does not fire the guard when zod runs the schema backward', () => { |
| 111 | const parsed = jsonSchemaToZod(guarded); |
| 112 | const forward = rejects(parsed, { q: 'cats', limit: 'ten' }); |
| 113 | expect(forward.some(issue => issue.code === 'custom')).toBe(true); |
| 114 | |
| 115 | // `.safeEncode()` runs zod's parse with direction `backward`. The |
| 116 | // whole-schema guard steps aside on that axis; the schema's own checks |
| 117 | // still reject the value, without the guard's custom issue on top. |
| 118 | const backward = parsed.safeEncode({ q: 'cats', limit: 'ten' }); |
| 119 | expect(backward.success).toBe(false); |
| 120 | const issues = backward.success ? [] : backward.error.issues; |
| 121 | expect(issues.some(issue => issue.code === 'custom')).toBe(false); |
| 122 | }); |
| 123 | }); |