@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 288 lines 10.8 KB
Raw
1 /**
2 * @composio/json-schema-to-zod + Zod v3 e2e test
3 *
4 * Verifies that @composio/json-schema-to-zod works correctly with zod@3.25.76,
5 * including basic schema conversion and round-trip tests with zod-to-json-schema.
6 */
7
8 import { jsonSchemaToZod, type JsonSchema } from '@composio/json-schema-to-zod';
9 import { e2e } from '@e2e-tests/utils';
10 import { describe, it, expect } from 'bun:test';
11 import zodToJsonSchema from 'zod-to-json-schema';
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 v3', () => {
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('Round-trip conversion', () => {
116 it('preserves additionalProperties: true for empty objects', () => {
117 const schema: JsonSchema = {
118 type: 'object',
119 additionalProperties: true,
120 };
121
122 const zodSchema = jsonSchemaToZod(schema);
123 // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
124 const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
125 // @ts-expect-error Property 'additionalProperties' does not exist on type.
126 expect(convertedBack.additionalProperties).toBe(true);
127 // @ts-expect-error Property 'type' does not exist on type
128 expect(convertedBack.type).toBe('object');
129
130 expect(zodSchema.parse({})).toEqual({});
131 expect(zodSchema.parse({ any: 'value', number: 123 })).toEqual({
132 any: 'value',
133 number: 123,
134 });
135 });
136
137 it('preserves additionalProperties: true for objects with properties', () => {
138 const schema: JsonSchema = {
139 type: 'object',
140 properties: {
141 name: { type: 'string' },
142 age: { type: 'number' },
143 },
144 required: ['name'],
145 additionalProperties: true,
146 };
147
148 const zodSchema = jsonSchemaToZod(schema);
149 const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
150
151 // @ts-expect-error Property 'additionalProperties' does not exist on type.
152 expect(convertedBack.additionalProperties).toBe(true);
153 // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
154 expect(convertedBack.properties).toBeDefined();
155 // @ts-expect-error Property 'required' does not exist on type
156 expect(convertedBack.required).toEqual(['name']);
157
158 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
159 expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
160 name: 'John',
161 age: 30,
162 extra: 'field',
163 });
164 });
165
166 it('preserves additionalProperties: false for empty objects', () => {
167 const schema: JsonSchema = {
168 type: 'object',
169 additionalProperties: false,
170 };
171
172 const zodSchema = jsonSchemaToZod(schema);
173 const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
174
175 // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
176 // which is semantically equivalent to additionalProperties: false
177 const additionalPropsValid =
178 // @ts-expect-error Property 'additionalProperties' does not exist on type.
179 convertedBack.additionalProperties === false ||
180 // @ts-expect-error Property 'not' does not exist on type.
181 (convertedBack.not && typeof convertedBack.not === 'object');
182 expect(additionalPropsValid).toBe(true);
183 // @ts-expect-error Property 'type' does not exist on type.
184 expect(convertedBack.type).toBe('object');
185
186 expect(zodSchema.parse({})).toEqual({});
187 expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
188 });
189
190 it('preserves additionalProperties: false for objects with properties', () => {
191 const schema: JsonSchema = {
192 type: 'object',
193 properties: {
194 name: { type: 'string' },
195 },
196 additionalProperties: false,
197 };
198
199 const zodSchema = jsonSchemaToZod(schema);
200 const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
201
202 // @ts-expect-error Property 'additionalProperties' does not exist on type.
203 expect(convertedBack.additionalProperties).toBe(false);
204 // @ts-expect-error Property 'properties' does not exist on type.
205 expect(convertedBack.properties).toBeDefined();
206
207 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
208 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
209 });
210
211 it('handles additionalProperties with type schema', () => {
212 const schema: JsonSchema = {
213 type: 'object',
214 properties: {
215 name: { type: 'string' },
216 },
217 additionalProperties: { type: 'number' },
218 };
219
220 const zodSchema = jsonSchemaToZod(schema);
221 const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
222
223 // @ts-expect-error Property 'additionalProperties' does not exist on type.
224 expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
225 // @ts-expect-error Property 'properties' does not exist on type.
226 expect(convertedBack.properties).toBeDefined();
227
228 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
229 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
230 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
231 });
232
233 it('handles nested objects with different additionalProperties settings', () => {
234 const schema: JsonSchema = {
235 type: 'object',
236 properties: {
237 strictChild: {
238 type: 'object',
239 properties: {
240 name: { type: 'string' },
241 },
242 additionalProperties: false,
243 },
244 flexibleChild: {
245 type: 'object',
246 properties: {
247 age: { type: 'number' },
248 },
249 additionalProperties: true,
250 },
251 },
252 additionalProperties: { type: 'string' },
253 };
254
255 const zodSchema = jsonSchemaToZod(schema);
256 const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
257
258 // @ts-expect-error Property 'additionalProperties' does not exist on type.
259 expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
260 // @ts-expect-error Property 'properties' does not exist on type.
261 const convertedProperties = convertedBack.properties as Record<string, unknown>;
262 expect(convertedProperties?.strictChild).toBeDefined();
263 expect(convertedProperties?.flexibleChild).toBeDefined();
264
265 const validData = {
266 strictChild: { name: 'John' },
267 flexibleChild: { age: 30, extra: 'allowed' },
268 extraString: 'this should be a string',
269 };
270 expect(zodSchema.parse(validData)).toEqual(validData);
271
272 // Test invalid cases
273 expect(() =>
274 zodSchema.parse({
275 strictChild: { name: 'John', extra: 'not allowed' },
276 })
277 ).toThrow();
278
279 expect(() =>
280 zodSchema.parse({
281 extraNumber: 123, // should be string according to additionalProperties
282 })
283 ).toThrow();
284 });
285 });
286 });
287 },
288 });