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, 06:00 UTC
| 1 | import Ajv from 'ajv'; |
| 2 | import { describe, expect, it } from 'vitest'; |
| 3 | import * as z4 from 'zod/v4'; |
| 4 | |
| 5 | import { jsonSchemaToZod } from '../../src/v4'; |
| 6 | import { zodV4Backend } from '../../src/backend/zod-v4'; |
| 7 | import type { JsonSchema } from '../../src/types'; |
| 8 | |
| 9 | type Json = Record<string, unknown>; |
| 10 | |
| 11 | const render = (schema: JsonSchema, io: 'input' | 'output' = 'input'): Json => |
| 12 | z4.toJSONSchema(jsonSchemaToZod(schema), { io, target: 'draft-7' }) as Json; |
| 13 | |
| 14 | const property = (json: Json, key: string): Json => (json.properties as Record<string, Json>)[key]; |
| 15 | |
| 16 | const ajv = new Ajv({ strict: false, validateFormats: false }); |
| 17 | |
| 18 | describe('v4 annotation forwarding', () => { |
| 19 | it.each(['input', 'output'] as const)( |
| 20 | 'keeps the source format, encoding, and media type (io %s)', |
| 21 | io => { |
| 22 | const json = render( |
| 23 | { |
| 24 | type: 'object', |
| 25 | properties: { |
| 26 | file: { type: 'string', format: 'binary' }, |
| 27 | hex: { type: 'string', contentEncoding: 'base16' }, |
| 28 | image: { type: 'string', contentEncoding: 'BASE64', contentMediaType: 'image/png' }, |
| 29 | at: { type: 'string', format: 'time' }, |
| 30 | big: { type: 'number', format: 'int64' }, |
| 31 | when: { type: 'string', format: 'date-time', description: 'When' }, |
| 32 | }, |
| 33 | }, |
| 34 | io |
| 35 | ); |
| 36 | expect(property(json, 'file')).toEqual({ type: 'string', format: 'binary' }); |
| 37 | expect(property(json, 'hex')).toEqual({ type: 'string', contentEncoding: 'base16' }); |
| 38 | expect(property(json, 'image')).toMatchObject({ |
| 39 | type: 'string', |
| 40 | contentEncoding: 'BASE64', |
| 41 | contentMediaType: 'image/png', |
| 42 | }); |
| 43 | // zod adds its own `pattern` for the ISO formats; the source `format` rides along. |
| 44 | expect(property(json, 'at')).toMatchObject({ type: 'string', format: 'time' }); |
| 45 | expect(property(json, 'big')).toEqual({ type: 'integer', format: 'int64' }); |
| 46 | expect(property(json, 'when')).toMatchObject({ |
| 47 | type: 'string', |
| 48 | format: 'date-time', |
| 49 | description: 'When', |
| 50 | }); |
| 51 | } |
| 52 | ); |
| 53 | |
| 54 | it('forwards annotations on nested properties and under a guarded root', () => { |
| 55 | const json = render({ |
| 56 | type: 'object', |
| 57 | properties: { |
| 58 | inner: { |
| 59 | type: 'object', |
| 60 | properties: { blob: { type: 'string', format: 'binary' } }, |
| 61 | }, |
| 62 | linked: { $ref: '#/$defs/count' }, |
| 63 | total: { type: 'integer' }, |
| 64 | }, |
| 65 | $defs: { count: { type: 'integer' } }, |
| 66 | }); |
| 67 | expect(property(property(json, 'inner'), 'blob')).toEqual({ type: 'string', format: 'binary' }); |
| 68 | expect(property(json, 'total')).toEqual({ type: 'integer' }); |
| 69 | }); |
| 70 | |
| 71 | it('leaves the annotations out with withoutAnnotations', () => { |
| 72 | const json = z4.toJSONSchema( |
| 73 | jsonSchemaToZod( |
| 74 | { |
| 75 | type: 'object', |
| 76 | properties: { |
| 77 | file: { type: 'string', format: 'binary', description: 'A file' }, |
| 78 | count: { type: 'integer' }, |
| 79 | }, |
| 80 | }, |
| 81 | { withoutAnnotations: true } |
| 82 | ), |
| 83 | { io: 'input', target: 'draft-7' } |
| 84 | ) as Json; |
| 85 | expect(property(json, 'file')).toEqual({ type: 'string', description: 'A file' }); |
| 86 | expect(property(json, 'count')).toEqual({ type: 'number' }); |
| 87 | }); |
| 88 | |
| 89 | it.each([ |
| 90 | { |
| 91 | name: 'a nullable integer', |
| 92 | schema: { type: 'integer', nullable: true, description: 'N' }, |
| 93 | accepted: 7, |
| 94 | rejected: 7.5, |
| 95 | }, |
| 96 | { |
| 97 | name: 'a nullable date-time', |
| 98 | schema: { type: 'string', format: 'date-time', nullable: true, description: 'D' }, |
| 99 | accepted: '2024-03-20T12:00:00Z', |
| 100 | rejected: 12, |
| 101 | }, |
| 102 | ])('keeps $name nullable in the rendered schema', ({ schema, accepted, rejected }) => { |
| 103 | const converted = jsonSchemaToZod(schema as unknown as JsonSchema); |
| 104 | const json = z4.toJSONSchema(converted, { io: 'input', target: 'draft-7' }) as Json; |
| 105 | // The annotations sit on the inner node: a scalar `type` or `format` |
| 106 | // next to the wrapper's own rendering would reject `null`. zod 4.5 |
| 107 | // folds `anyOf: [inner, null]` into a type array when the inner node |
| 108 | // carries only a `type`; 4.1 keeps the `anyOf`. |
| 109 | expect(typeof json.type).not.toBe('string'); |
| 110 | expect(json).not.toHaveProperty('format'); |
| 111 | expect(json.description).toBe(schema.description); |
| 112 | expect(converted.description).toBe(schema.description); |
| 113 | const { $schema: _schema, ...plain } = json; |
| 114 | const validate = ajv.compile(plain); |
| 115 | expect(validate(null)).toBe(true); |
| 116 | expect(validate(accepted)).toBe(true); |
| 117 | expect(validate(rejected)).toBe(false); |
| 118 | expect(converted.safeParse(null).success).toBe(true); |
| 119 | expect(converted.safeParse(accepted).success).toBe(true); |
| 120 | expect(converted.safeParse(rejected).success).toBe(false); |
| 121 | }); |
| 122 | |
| 123 | it('keeps type integer next to bounds', () => { |
| 124 | const json = render({ type: 'integer', minimum: 1, maximum: 10 }); |
| 125 | expect(json).toMatchObject({ type: 'integer', minimum: 1, maximum: 10 }); |
| 126 | expect(render({ type: 'number', multipleOf: 1 })).toMatchObject({ type: 'integer' }); |
| 127 | }); |
| 128 | |
| 129 | it('forwards annotations on a constrained value', () => { |
| 130 | const json = render({ type: 'string', enum: ['a', 'b'], format: 'custom', description: 'E' }); |
| 131 | expect(json.format).toBe('custom'); |
| 132 | expect(json.description).toBe('E'); |
| 133 | }); |
| 134 | |
| 135 | it('forwards nothing from the keywords next to a $ref', () => { |
| 136 | const schema: JsonSchema = { |
| 137 | type: 'object', |
| 138 | $defs: { text: { type: 'string' } }, |
| 139 | properties: { |
| 140 | x: { $ref: '#/$defs/text', type: 'integer', format: 'int64', contentEncoding: 'base64' }, |
| 141 | }, |
| 142 | required: ['x'], |
| 143 | }; |
| 144 | expect(jsonSchemaToZod(schema).safeParse({ x: 'hi' }).success).toBe(true); |
| 145 | expect(property(render(schema), 'x')).toEqual({}); |
| 146 | }); |
| 147 | |
| 148 | it('writes the description together with the annotations', () => { |
| 149 | const converted = jsonSchemaToZod({ type: 'string', format: 'binary', description: 'Bytes' }); |
| 150 | expect(converted.description).toBe('Bytes'); |
| 151 | expect(converted.meta()).toEqual({ description: 'Bytes', format: 'binary' }); |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | describe('v4 decorate across wrapper kinds', () => { |
| 156 | /** Every wrapper kind `annotateInnermost` can receive, built through the backend itself. */ |
| 157 | it.each([ |
| 158 | ['optional', (inner: z4.ZodString) => zodV4Backend.optional(inner)], |
| 159 | ['nullable', (inner: z4.ZodString) => zodV4Backend.nullable(inner)], |
| 160 | ['prefault', (inner: z4.ZodString) => zodV4Backend.withDefault(inner, 'fallback')], |
| 161 | ['readonly', (inner: z4.ZodString) => zodV4Backend.readonly(inner)], |
| 162 | ] as const)( |
| 163 | 'puts forwarded annotations on the innermost node under a %s wrapper', |
| 164 | (_name, make) => { |
| 165 | const inner = z4.string(); |
| 166 | const decorated = zodV4Backend.decorate(make(inner).meta({ title: 'Wrapper' }), { |
| 167 | annotations: { format: 'binary' }, |
| 168 | }); |
| 169 | |
| 170 | // A validation-shaped annotation on the wrapper would sit beside the |
| 171 | // wrapper's own rendering (`anyOf`, `default`, `readOnly`) and change |
| 172 | // what the schema accepts; it must land on the innermost node. |
| 173 | let deepest = decorated; |
| 174 | for (;;) { |
| 175 | const def = (deepest as unknown as { _zod: { def: { innerType?: z4.ZodType } } })._zod.def; |
| 176 | if (!def.innerType) break; |
| 177 | deepest = def.innerType; |
| 178 | } |
| 179 | expect(z4.globalRegistry.get(deepest)).toMatchObject({ format: 'binary' }); |
| 180 | // The wrapper keeps the metadata it already carried through the rewrap. |
| 181 | expect(z4.globalRegistry.get(decorated)).toMatchObject({ title: 'Wrapper' }); |
| 182 | expect(decorated.safeParse('data').success).toBe(true); |
| 183 | } |
| 184 | ); |
| 185 | }); |