@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 281 lines 10 KB
Raw
1 /**
2 * Pins the shape of every Zod v3 schema the root converter builds, as a JSON
3 * Schema rendering plus a walk over `_def`. The snapshot is the reference
4 * that structural refactors of the converter are checked against: a change
5 * here is a change in what consumers receive, not an internal detail.
6 */
7 import { describe, expect, it } from 'vitest';
8 import type { ZodTypeAny } from 'zod/v3';
9 import { zodToJsonSchema } from 'zod-to-json-schema';
10
11 import { jsonSchemaToZod } from '../src/index';
12 import type { JsonSchema } from '../src/types';
13 import { loadObjectCases, loadStrictCases } from './fixtures/corpus';
14
15 type Fingerprint = Record<string, unknown>;
16
17 const checkFingerprint = (check: Record<string, unknown>): Fingerprint => {
18 const { regex, ...rest } = check;
19 return regex instanceof RegExp ? { ...rest, regex: regex.source } : rest;
20 };
21
22 const defFingerprint = (schema: ZodTypeAny): Fingerprint => {
23 const def = schema._def as Record<string, unknown> & { typeName: string };
24 const node: Fingerprint = { typeName: def.typeName };
25
26 if (def.description !== undefined) node.description = def.description;
27 if ('wholeSchemaGuard' in def) node.guarded = true;
28 if (Array.isArray(def.checks)) {
29 node.checks = def.checks.map(check => checkFingerprint(check as Record<string, unknown>));
30 }
31
32 switch (def.typeName) {
33 case 'ZodObject': {
34 const shape = (def.shape as () => Record<string, ZodTypeAny>)();
35 node.shape = Object.fromEntries(
36 Object.entries(shape).map(([key, value]) => [key, defFingerprint(value)])
37 );
38 node.unknownKeys = def.unknownKeys;
39 node.catchall = defFingerprint(def.catchall as ZodTypeAny);
40 break;
41 }
42 case 'ZodArray':
43 node.element = defFingerprint(def.type as ZodTypeAny);
44 if (def.minLength) node.minLength = def.minLength;
45 if (def.maxLength) node.maxLength = def.maxLength;
46 if (def.exactLength) node.exactLength = def.exactLength;
47 break;
48 case 'ZodTuple':
49 node.items = (def.items as ZodTypeAny[]).map(defFingerprint);
50 node.rest = def.rest ? defFingerprint(def.rest as ZodTypeAny) : null;
51 break;
52 case 'ZodUnion':
53 node.options = (def.options as ZodTypeAny[]).map(defFingerprint);
54 break;
55 case 'ZodIntersection':
56 node.left = defFingerprint(def.left as ZodTypeAny);
57 node.right = defFingerprint(def.right as ZodTypeAny);
58 break;
59 case 'ZodRecord':
60 node.keyType = defFingerprint(def.keyType as ZodTypeAny);
61 node.valueType = defFingerprint(def.valueType as ZodTypeAny);
62 break;
63 case 'ZodOptional':
64 case 'ZodNullable':
65 case 'ZodReadonly':
66 node.innerType = defFingerprint(def.innerType as ZodTypeAny);
67 break;
68 case 'ZodDefault':
69 node.innerType = defFingerprint(def.innerType as ZodTypeAny);
70 node.defaultValue = (def.defaultValue as () => unknown)();
71 break;
72 case 'ZodEffects':
73 node.effect = (def.effect as { type: string }).type;
74 node.schema = defFingerprint(def.schema as ZodTypeAny);
75 break;
76 case 'ZodLiteral':
77 node.value = def.value;
78 break;
79 case 'ZodEnum':
80 node.values = def.values;
81 break;
82 default:
83 break;
84 }
85
86 return node;
87 };
88
89 const fingerprint = (schema: JsonSchema): Fingerprint => {
90 let converted: ZodTypeAny;
91 try {
92 converted = jsonSchemaToZod(schema);
93 } catch (error) {
94 return { conversionError: error instanceof Error ? error.name : String(error) };
95 }
96 return {
97 jsonSchema: zodToJsonSchema(converted, { target: 'jsonSchema7' }),
98 def: defFingerprint(converted),
99 };
100 };
101
102 const localCases: ReadonlyArray<{ id: string; schema: JsonSchema }> = [
103 {
104 id: 'string-formats',
105 schema: {
106 type: 'object',
107 properties: {
108 email: { type: 'string', format: 'email' },
109 ip: { type: 'string', format: 'ip' },
110 ipv4: { type: 'string', format: 'ipv4' },
111 ipv6: { type: 'string', format: 'ipv6' },
112 uri: { type: 'string', format: 'uri' },
113 uuid: { type: 'string', format: 'uuid' },
114 dateTime: { type: 'string', format: 'date-time' },
115 time: { type: 'string', format: 'time' },
116 date: { type: 'string', format: 'date' },
117 duration: { type: 'string', format: 'duration' },
118 binary: { type: 'string', format: 'binary' },
119 byte: { type: 'string', format: 'byte' },
120 base64: { type: 'string', contentEncoding: 'base64', contentMediaType: 'image/png' },
121 hex: { type: 'string', contentEncoding: 'base16' },
122 pattern: { type: 'string', pattern: '^[a-z\\_]+$', minLength: 1, maxLength: 8 },
123 aliases: { type: 'string', min: 2, max: 4 },
124 },
125 required: ['email'],
126 },
127 },
128 {
129 id: 'number-constraints',
130 schema: {
131 type: 'object',
132 properties: {
133 integer: { type: 'integer', minimum: 1, maximum: 10 },
134 int64: { type: 'number', format: 'int64' },
135 unit: { type: 'number', multipleOf: 1 },
136 decimal: { type: 'number', multipleOf: 0.1 },
137 exclusive: { type: 'number', exclusiveMinimum: 0, exclusiveMaximum: 100 },
138 draft4: { type: 'number', minimum: 0, exclusiveMinimum: true, maximum: 1 },
139 aliases: { type: 'number', min: 0, max: 1 },
140 },
141 } as unknown as JsonSchema,
142 },
143 {
144 id: 'metadata-and-wrappers',
145 schema: {
146 type: 'object',
147 title: 'Request',
148 description: 'A request',
149 properties: {
150 described: { type: 'string', description: 'Named', example: 'x' },
151 titled: { type: 'string', title: 'Title only', examples: ['a', 'b'] },
152 nullable: { type: 'integer', nullable: true, description: 'Maybe' },
153 nullDefault: { type: 'string', default: null },
154 defaulted: { type: 'string', default: 'x' },
155 readOnly: { type: 'string', readOnly: true },
156 nullableNullDefault: { type: 'string', nullable: true, default: null },
157 anyOfNull: { anyOf: [{ type: 'string' }, { type: 'null' }], default: null },
158 } as Record<string, JsonSchema>,
159 },
160 },
161 {
162 id: 'combinators',
163 schema: {
164 type: 'object',
165 properties: {
166 oneOf: { oneOf: [{ type: 'string' }, { type: 'number' }] },
167 anyOf: { anyOf: [{ type: 'string' }, { type: 'number' }] },
168 allOf: {
169 allOf: [
170 { type: 'number', minimum: 0 },
171 { type: 'number', maximum: 1 },
172 ],
173 },
174 not: { not: { type: 'string' } },
175 conditional: { if: { type: 'string' }, then: { minLength: 1 }, else: { type: 'number' } },
176 multiple: { type: ['string', 'null'] },
177 typeless: { minLength: 2 },
178 enumObject: { enum: [{ a: 1 }, [1]] },
179 constObject: { const: { a: 1 } },
180 enumMixed: { enum: ['a', 1, true, null] },
181 enumStrings: { type: 'string', enum: ['a', 'b'] },
182 constString: { const: 'a' },
183 },
184 },
185 },
186 {
187 id: 'containers',
188 schema: {
189 type: 'object',
190 properties: {
191 tuple: { type: 'array', items: [{ type: 'string' }, { type: 'integer' }] },
192 closedTuple: {
193 type: 'array',
194 items: [{ type: 'string' }],
195 additionalItems: false,
196 minItems: 1,
197 maxItems: 1,
198 },
199 typedRest: {
200 type: 'array',
201 items: [{ type: 'string' }],
202 additionalItems: { type: 'number' },
203 },
204 list: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 3 },
205 aliases: { type: 'array', items: { type: 'string' }, min: 1, max: 3 },
206 bare: { type: 'array' },
207 anyOfArray: {
208 type: 'array',
209 anyOf: [{ items: { type: 'string' } }, { items: { type: 'number' } }],
210 },
211 strict: {
212 type: 'object',
213 properties: { a: { type: 'string' } },
214 additionalProperties: false,
215 },
216 open: { type: 'object', properties: { a: { type: 'string' } }, additionalProperties: true },
217 typed: {
218 type: 'object',
219 properties: { a: { type: 'string' } },
220 additionalProperties: { type: 'number' },
221 },
222 record: { type: 'object', additionalProperties: { type: 'number' } },
223 emptyStrict: { type: 'object', additionalProperties: false },
224 emptyOpen: { type: 'object' },
225 patterns: {
226 type: 'object',
227 properties: { a: { type: 'string' } },
228 patternProperties: { '^p_': { type: 'number' } },
229 additionalProperties: false,
230 },
231 patternsOnly: { type: 'object', patternProperties: { '^p_': { type: 'number' } } },
232 implicit: { properties: { a: { type: 'string' } }, required: ['a'] },
233 },
234 },
235 },
236 {
237 id: 'references-and-guards',
238 schema: {
239 type: 'object',
240 properties: {
241 ref: { $ref: '#/$defs/name' },
242 nested: { type: 'object', properties: { ref: { $ref: '#/$defs/name' } } },
243 unique: { type: 'array', uniqueItems: true },
244 bounded: { type: 'object', minProperties: 1 },
245 },
246 required: ['ref'],
247 $defs: { name: { type: 'string', pattern: '^[a-z]+$' } },
248 },
249 },
250 { id: 'boolean-true', schema: true },
251 { id: 'boolean-false', schema: false },
252 { id: 'root-string', schema: { type: 'string', description: 'root' } },
253 {
254 id: 'root-nullable',
255 schema: { type: 'string', nullable: true, default: null } as unknown as JsonSchema,
256 },
257 { id: 'root-ref', schema: { $defs: { n: { type: 'number' } }, $ref: '#/$defs/n' } },
258 ];
259
260 describe('v3 output fingerprint', () => {
261 it('matches the pinned snapshot for the shared object corpus', () => {
262 const cases = loadObjectCases().map(({ id, schema }) => ({
263 id,
264 fingerprint: fingerprint(schema as JsonSchema),
265 }));
266 expect(cases).toMatchSnapshot();
267 });
268
269 it('matches the pinned snapshot for the shared strict corpus', () => {
270 const cases = loadStrictCases().map(({ id, schema }) => ({
271 id,
272 fingerprint: fingerprint(schema as JsonSchema),
273 }));
274 expect(cases).toMatchSnapshot();
275 });
276
277 it('matches the pinned snapshot for the local cases', () => {
278 const cases = localCases.map(({ id, schema }) => ({ id, fingerprint: fingerprint(schema) }));
279 expect(cases).toMatchSnapshot();
280 });
281 });