@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 296 lines 9.75 KB
Raw
1 /**
2 * @composio/json-schema-to-zod + Zod v4 e2e test
3 *
4 * Verifies that @composio/json-schema-to-zod works correctly with zod@4,
5 * including basic schema conversion and parsing behavior tests.
6 */
7
8 import { jsonSchemaToZod, type JsonSchema } from '@composio/json-schema-to-zod';
9 import { jsonSchemaToZod as jsonSchemaToZodV4 } from '@composio/json-schema-to-zod/v4';
10 import { e2e } from '@e2e-tests/utils';
11 import { describe, it, expect } from 'bun:test';
12
13 e2e(import.meta.url, {
14 versions: { node: ['22.23.5', '24.22.3', '26.13.1'] },
15 defineTests: () => {
16 describe('json-schema-to-zod with Zod v4', () => {
17 describe('Basic functionality', () => {
18 it('converts basic string schema', () => {
19 const schema: JsonSchema = { type: 'string' };
20 const zodSchema = jsonSchemaToZod(schema);
21
22 expect(zodSchema.parse('hello')).toBe('hello');
23 expect(() => zodSchema.parse(123)).toThrow();
24 });
25
26 it('converts object schema with validation', () => {
27 const schema: JsonSchema = {
28 type: 'object',
29 properties: {
30 name: { type: 'string' },
31 age: { type: 'number', minimum: 0 },
32 },
33 required: ['name'],
34 };
35 const zodSchema = jsonSchemaToZod(schema);
36
37 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
38 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
39 expect(() => zodSchema.parse({ age: 30 })).toThrow();
40 });
41
42 it('converts array schema', () => {
43 const schema: JsonSchema = {
44 type: 'array',
45 items: { type: 'string' },
46 };
47 const zodSchema = jsonSchemaToZod(schema);
48
49 expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
50 expect(() => zodSchema.parse(['one', 2])).toThrow();
51 });
52
53 it('validates email format', () => {
54 const schema: JsonSchema = {
55 type: 'string',
56 format: 'email',
57 };
58 const zodSchema = jsonSchemaToZod(schema);
59
60 expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
61 expect(() => zodSchema.parse('invalid-email')).toThrow();
62 });
63
64 it('handles complex nested schemas', () => {
65 const schema: JsonSchema = {
66 type: 'object',
67 properties: {
68 user: {
69 type: 'object',
70 properties: {
71 name: { type: 'string' },
72 contacts: {
73 type: 'array',
74 items: {
75 type: 'object',
76 properties: {
77 type: { type: 'string' },
78 value: { type: 'string' },
79 },
80 required: ['type', 'value'],
81 },
82 },
83 },
84 required: ['name'],
85 },
86 },
87 required: ['user'],
88 };
89 const zodSchema = jsonSchemaToZod(schema);
90
91 const validData = {
92 user: {
93 name: 'Jane Doe',
94 contacts: [
95 { type: 'email', value: 'jane@example.com' },
96 { type: 'phone', value: '555-1234' },
97 ],
98 },
99 };
100 expect(zodSchema.parse(validData)).toEqual(validData);
101 });
102
103 it('handles anyOf schemas', () => {
104 const schema: JsonSchema = {
105 anyOf: [{ type: 'string' }, { type: 'number' }],
106 };
107 const zodSchema = jsonSchemaToZod(schema);
108
109 expect(zodSchema.parse('hello')).toBe('hello');
110 expect(zodSchema.parse(42)).toBe(42);
111 expect(() => zodSchema.parse(true)).toThrow();
112 });
113 });
114
115 describe('/v4 entry', () => {
116 it('builds a zod v4 schema on a copy that meets the floor', () => {
117 const schema: JsonSchema = {
118 type: 'object',
119 properties: {
120 name: { type: 'string', description: 'Name' },
121 count: { type: 'integer' },
122 },
123 required: ['name'],
124 };
125 const zodSchema = jsonSchemaToZodV4(schema);
126 const { version } = (
127 zodSchema as unknown as {
128 _zod: { version: { major: number; minor: number; patch: number } };
129 }
130 )._zod;
131
132 expect(version.major).toBe(4);
133 expect(version.minor > 1 || (version.minor === 1 && version.patch >= 13)).toBe(true);
134 expect(zodSchema.parse({ name: 'Ada', count: 2 })).toEqual({ name: 'Ada', count: 2 });
135 expect(() => zodSchema.parse({ count: 2 })).toThrow();
136 expect(() => zodSchema.parse({ name: 'Ada', count: 2.5 })).toThrow();
137 expect(zodSchema.description).toBeUndefined();
138 });
139 });
140
141 describe('Parsing behavior', () => {
142 it('converts basic schemas and verifies parsing behavior', () => {
143 const schema: JsonSchema = {
144 type: 'object',
145 properties: {
146 name: { type: 'string' },
147 age: { type: 'number' },
148 },
149 required: ['name'],
150 additionalProperties: false,
151 };
152
153 const zodSchema = jsonSchemaToZod(schema);
154
155 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
156 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
157 expect(() => zodSchema.parse({ age: 30 })).toThrow();
158 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
159 });
160
161 it('handles additionalProperties: true', () => {
162 const schema: JsonSchema = {
163 type: 'object',
164 properties: {
165 name: { type: 'string' },
166 },
167 additionalProperties: true,
168 };
169
170 const zodSchema = jsonSchemaToZod(schema);
171
172 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
173 expect(zodSchema.parse({ name: 'John', extra: 'allowed', another: 123 })).toEqual({
174 name: 'John',
175 extra: 'allowed',
176 another: 123,
177 });
178 });
179
180 it('handles additionalProperties with type constraint', () => {
181 const schema: JsonSchema = {
182 type: 'object',
183 properties: {
184 name: { type: 'string' },
185 },
186 additionalProperties: { type: 'number' },
187 };
188
189 const zodSchema = jsonSchemaToZod(schema);
190
191 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
192 expect(zodSchema.parse({ name: 'John', age: 30, count: 5 })).toEqual({
193 name: 'John',
194 age: 30,
195 count: 5,
196 });
197 expect(() => zodSchema.parse({ name: 'John', extra: 'string' })).toThrow();
198 });
199
200 it('handles nested objects with different additionalProperties settings', () => {
201 const schema: JsonSchema = {
202 type: 'object',
203 properties: {
204 strictChild: {
205 type: 'object',
206 properties: {
207 name: { type: 'string' },
208 },
209 additionalProperties: false,
210 },
211 flexibleChild: {
212 type: 'object',
213 properties: {
214 age: { type: 'number' },
215 },
216 additionalProperties: true,
217 },
218 },
219 additionalProperties: { type: 'string' },
220 };
221
222 const zodSchema = jsonSchemaToZod(schema);
223
224 const validData = {
225 strictChild: { name: 'John' },
226 flexibleChild: { age: 30, extra: 'allowed' },
227 extraString: 'this should be a string',
228 };
229 expect(zodSchema.parse(validData)).toEqual(validData);
230
231 expect(() =>
232 zodSchema.parse({
233 strictChild: { name: 'John', extra: 'not allowed' },
234 })
235 ).toThrow();
236
237 expect(() =>
238 zodSchema.parse({
239 extraNumber: 123,
240 })
241 ).toThrow();
242 });
243
244 it('handles complex nested arrays', () => {
245 const schema: JsonSchema = {
246 type: 'array',
247 items: {
248 type: 'object',
249 properties: {
250 id: { type: 'number' },
251 tags: {
252 type: 'array',
253 items: { type: 'string' },
254 },
255 },
256 required: ['id'],
257 },
258 };
259
260 const zodSchema = jsonSchemaToZod(schema);
261
262 const validData = [{ id: 1, tags: ['a', 'b'] }, { id: 2, tags: ['c'] }, { id: 3 }];
263 expect(zodSchema.parse(validData)).toEqual(validData);
264
265 expect(() => zodSchema.parse([{ tags: ['a', 'b'] }])).toThrow();
266 });
267
268 it('handles union types with anyOf', () => {
269 const schema: JsonSchema = {
270 anyOf: [
271 { type: 'string', minLength: 3 },
272 { type: 'number', minimum: 0 },
273 {
274 type: 'object',
275 properties: {
276 type: { type: 'string' },
277 },
278 required: ['type'],
279 },
280 ],
281 };
282
283 const zodSchema = jsonSchemaToZod(schema);
284
285 expect(zodSchema.parse('hello')).toBe('hello');
286 expect(zodSchema.parse(42)).toBe(42);
287 expect(zodSchema.parse({ type: 'custom' })).toEqual({ type: 'custom' });
288
289 expect(() => zodSchema.parse('ab')).toThrow();
290 expect(() => zodSchema.parse(-1)).toThrow();
291 expect(() => zodSchema.parse({ foo: 'bar' })).toThrow();
292 });
293 });
294 });
295 },
296 });