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 22, 13:15 UTC
chore(ts/e2e): refactored json-schema-to-zod e2e tests into unified ts/e2e-tests location
jkomyno committed
Jan 27, 2026 at 01:51 UTC
a815c73084bb2ebb6738d7bf82f2eac38b6b63fb
19 files changed
+684
-616
pnpm-lock.yaml
+30
-30
@@ -390,6 +390,35 @@ importers:
390
specifier: workspace:*
391
version: link:../../../_utils
392
393
+ ts/e2e-tests/runtimes/node/json-schema-to-zod-v3:
394
+ dependencies:
395
+ '@composio/json-schema-to-zod':
396
+ specifier: workspace:*
397
+ version: link:../../../../packages/json-schema-to-zod
398
+ zod:
399
+ specifier: 3.25.76
400
+ version: 3.25.76
401
+ zod-to-json-schema:
402
+ specifier: ^3.25.1
403
+ version: 3.25.1(zod@3.25.76)
404
+ devDependencies:
405
+ '@e2e-tests/utils':
406
+ specifier: workspace:*
407
+ version: link:../../../_utils
408
+
409
+ ts/e2e-tests/runtimes/node/json-schema-to-zod-v4:
410
+ dependencies:
411
+ '@composio/json-schema-to-zod':
412
+ specifier: workspace:*
413
+ version: link:../../../../packages/json-schema-to-zod
414
+ zod:
415
+ specifier: ^4.1.0
416
+ version: 4.3.6
417
+ devDependencies:
418
+ '@e2e-tests/utils':
419
+ specifier: workspace:*
420
+ version: link:../../../_utils
421
+
422
ts/e2e-tests/runtimes/node/openai-zod4-compat:
423
devDependencies:
424
'@e2e-tests/utils':
@@ -1149,35 +1178,6 @@ importers:
1178
specifier: 'catalog:'
1179
version: 4.3.6
1180
1152
- ts/packages/json-schema-to-zod/test/e2e/zod-v3:
1153
- dependencies:
1154
- '@composio/json-schema-to-zod':
1155
- specifier: workspace:*
1156
- version: link:../../..
1157
- zod:
1158
- specifier: 3.25.76
1159
- version: 3.25.76
1160
- zod-to-json-schema:
1161
- specifier: 'catalog:'
1162
- version: 3.25.1(zod@3.25.76)
1163
- devDependencies:
1164
- vitest:
1165
- specifier: 'catalog:'
1166
- version: 3.2.4(@types/node@24.10.9)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.21.0)(yaml@2.8.2)
1167
-
1168
- ts/packages/json-schema-to-zod/test/e2e/zod-v4:
1169
- dependencies:
1170
- '@composio/json-schema-to-zod':
1171
- specifier: workspace:*
1172
- version: link:../../..
1173
- zod:
1174
- specifier: ^4.1.0
1175
- version: 4.3.6
1176
- devDependencies:
1177
- vitest:
1178
- specifier: 'catalog:'
1179
- version: 3.2.4(@types/node@24.10.9)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.21.0)(yaml@2.8.2)
1180
-
1181
ts/packages/providers/anthropic:
1182
dependencies:
1183
'@anthropic-ai/sdk':
@@ -10232,7 +10232,7 @@ snapshots:
10232
10233
'@openrouter/sdk@0.1.27':
10234
dependencies:
10235
- zod: 4.3.6
10235
+ zod: 3.25.76
10236
10237
'@opentelemetry/api-logs@0.203.0':
10238
dependencies:
pnpm-workspace.yaml
-1
@@ -6,7 +6,6 @@ packages:
6
- ts/packages/cli
7
- ts/packages/core
8
- ts/packages/json-schema-to-zod
9
- - ts/packages/json-schema-to-zod/test/e2e/*
9
- ts/packages/providers/*
10
- ts/packages/ts-builders
11
- ts/packages/wrappers/*
ts/e2e-tests/README.md
+2
@@ -25,6 +25,8 @@ ts/e2e-tests/
25
├── node/ # Node.js runtime tests
26
│ ├── cjs-basic/ # CommonJS compatibility tests
27
│ ├── esm-basic/ # ESM compatibility tests
28
+ │ ├── json-schema-to-zod-v3/ # @composio/json-schema-to-zod + Zod v3 tests
29
+ │ ├── json-schema-to-zod-v4/ # @composio/json-schema-to-zod + Zod v4 tests
30
│ ├── openai-zod4-compat/ # OpenAI + Zod v4 compatibility tests
31
│ └── typescript-mjs-import-nodenext/ # TypeScript moduleResolution: nodenext tests
32
└── cloudflare/ # Cloudflare runtime tests
ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/README.md
new
+30
@@ -0,0 +1,30 @@
1
+# json-schema-to-zod + Zod v3 Compatibility Test
2
+
3
+Verifies that `@composio/json-schema-to-zod` works correctly with `zod@3.25.76`.
4
+
5
+## Why This Exists
6
+
7
+The `@composio/json-schema-to-zod` package must support both Zod v3 and v4. This suite ensures:
8
+
9
+- JSON Schema to Zod conversion works with Zod v3
10
+- All schema types (string, object, array, anyOf) convert correctly
11
+- Round-trip conversion (JSON Schema -> Zod -> JSON Schema) preserves semantics
12
+- `additionalProperties` handling works correctly
13
+
14
+## What It Tests
15
+
16
+| Test | Description |
17
+| --------------------- | ---------------------------------------------------------------- |
18
+| Basic string schema | Converts `{ type: 'string' }` and validates |
19
+| Object schema | Required fields, nested properties, validation constraints |
20
+| Array schema | Typed array items with validation |
21
+| Email format | Format validation for email strings |
22
+| Nested schemas | Complex nested objects and arrays |
23
+| anyOf schemas | Union type conversion |
24
+| Round-trip conversion | JSON Schema -> Zod -> JSON Schema preserves `additionalProperties` |
25
+
26
+## Running
27
+
28
+```bash
29
+pnpm test:e2e
30
+```
ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/e2e.test.ts
new
+264
@@ -0,0 +1,264 @@
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 { describe, it, expect } from 'bun:test';
10
+import zodToJsonSchema from 'zod-to-json-schema';
11
+
12
+describe('json-schema-to-zod with Zod v3', () => {
13
+ describe('Basic functionality', () => {
14
+ it('converts basic string schema', () => {
15
+ const schema: JsonSchema = { type: 'string' };
16
+ const zodSchema = jsonSchemaToZod(schema);
17
+
18
+ expect(zodSchema.parse('hello')).toBe('hello');
19
+ expect(() => zodSchema.parse(123)).toThrow();
20
+ });
21
+
22
+ it('converts object schema with validation', () => {
23
+ const schema: JsonSchema = {
24
+ type: 'object',
25
+ properties: {
26
+ name: { type: 'string' },
27
+ age: { type: 'number', minimum: 0 },
28
+ },
29
+ required: ['name'],
30
+ };
31
+ const zodSchema = jsonSchemaToZod(schema);
32
+
33
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
34
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
35
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
36
+ });
37
+
38
+ it('converts array schema', () => {
39
+ const schema: JsonSchema = {
40
+ type: 'array',
41
+ items: { type: 'string' },
42
+ };
43
+ const zodSchema = jsonSchemaToZod(schema);
44
+
45
+ expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
46
+ expect(() => zodSchema.parse(['one', 2])).toThrow();
47
+ });
48
+
49
+ it('validates email format', () => {
50
+ const schema: JsonSchema = {
51
+ type: 'string',
52
+ format: 'email',
53
+ };
54
+ const zodSchema = jsonSchemaToZod(schema);
55
+
56
+ expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
57
+ expect(() => zodSchema.parse('invalid-email')).toThrow();
58
+ });
59
+
60
+ it('handles complex nested schemas', () => {
61
+ const schema: JsonSchema = {
62
+ type: 'object',
63
+ properties: {
64
+ user: {
65
+ type: 'object',
66
+ properties: {
67
+ name: { type: 'string' },
68
+ contacts: {
69
+ type: 'array',
70
+ items: {
71
+ type: 'object',
72
+ properties: {
73
+ type: { type: 'string' },
74
+ value: { type: 'string' },
75
+ },
76
+ required: ['type', 'value'],
77
+ },
78
+ },
79
+ },
80
+ required: ['name'],
81
+ },
82
+ },
83
+ required: ['user'],
84
+ };
85
+ const zodSchema = jsonSchemaToZod(schema);
86
+
87
+ const validData = {
88
+ user: {
89
+ name: 'Jane Doe',
90
+ contacts: [
91
+ { type: 'email', value: 'jane@example.com' },
92
+ { type: 'phone', value: '555-1234' },
93
+ ],
94
+ },
95
+ };
96
+ expect(zodSchema.parse(validData)).toEqual(validData);
97
+ });
98
+
99
+ it('handles anyOf schemas', () => {
100
+ const schema: JsonSchema = {
101
+ anyOf: [{ type: 'string' }, { type: 'number' }],
102
+ };
103
+ const zodSchema = jsonSchemaToZod(schema);
104
+
105
+ expect(zodSchema.parse('hello')).toBe('hello');
106
+ expect(zodSchema.parse(42)).toBe(42);
107
+ expect(() => zodSchema.parse(true)).toThrow();
108
+ });
109
+ });
110
+
111
+ describe('Round-trip conversion', () => {
112
+ it('preserves additionalProperties: true for empty objects', () => {
113
+ const schema: JsonSchema = {
114
+ type: 'object',
115
+ additionalProperties: true,
116
+ };
117
+
118
+ const zodSchema = jsonSchemaToZod(schema);
119
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
120
+
121
+ expect(convertedBack.additionalProperties).toBe(true);
122
+ expect(convertedBack.type).toBe('object');
123
+
124
+ expect(zodSchema.parse({})).toEqual({});
125
+ expect(zodSchema.parse({ any: 'value', number: 123 })).toEqual({ any: 'value', number: 123 });
126
+ });
127
+
128
+ it('preserves additionalProperties: true for objects with properties', () => {
129
+ const schema: JsonSchema = {
130
+ type: 'object',
131
+ properties: {
132
+ name: { type: 'string' },
133
+ age: { type: 'number' },
134
+ },
135
+ required: ['name'],
136
+ additionalProperties: true,
137
+ };
138
+
139
+ const zodSchema = jsonSchemaToZod(schema);
140
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
141
+
142
+ expect(convertedBack.additionalProperties).toBe(true);
143
+ expect(convertedBack.properties).toBeDefined();
144
+ expect(convertedBack.required).toEqual(['name']);
145
+
146
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
147
+ expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
148
+ name: 'John',
149
+ age: 30,
150
+ extra: 'field',
151
+ });
152
+ });
153
+
154
+ it('preserves additionalProperties: false for empty objects', () => {
155
+ const schema: JsonSchema = {
156
+ type: 'object',
157
+ additionalProperties: false,
158
+ };
159
+
160
+ const zodSchema = jsonSchemaToZod(schema);
161
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
162
+
163
+ // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
164
+ // which is semantically equivalent to additionalProperties: false
165
+ const additionalPropsValid =
166
+ convertedBack.additionalProperties === false ||
167
+ (convertedBack.not && typeof convertedBack.not === 'object');
168
+ expect(additionalPropsValid).toBe(true);
169
+ expect(convertedBack.type).toBe('object');
170
+
171
+ expect(zodSchema.parse({})).toEqual({});
172
+ expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
173
+ });
174
+
175
+ it('preserves additionalProperties: false for objects with properties', () => {
176
+ const schema: JsonSchema = {
177
+ type: 'object',
178
+ properties: {
179
+ name: { type: 'string' },
180
+ },
181
+ additionalProperties: false,
182
+ };
183
+
184
+ const zodSchema = jsonSchemaToZod(schema);
185
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
186
+
187
+ expect(convertedBack.additionalProperties).toBe(false);
188
+ expect(convertedBack.properties).toBeDefined();
189
+
190
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
191
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
192
+ });
193
+
194
+ it('handles additionalProperties with type schema', () => {
195
+ const schema: JsonSchema = {
196
+ type: 'object',
197
+ properties: {
198
+ name: { type: 'string' },
199
+ },
200
+ additionalProperties: { type: 'number' },
201
+ };
202
+
203
+ const zodSchema = jsonSchemaToZod(schema);
204
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
205
+
206
+ expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
207
+ expect(convertedBack.properties).toBeDefined();
208
+
209
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
210
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
211
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
212
+ });
213
+
214
+ it('handles nested objects with different additionalProperties settings', () => {
215
+ const schema: JsonSchema = {
216
+ type: 'object',
217
+ properties: {
218
+ strictChild: {
219
+ type: 'object',
220
+ properties: {
221
+ name: { type: 'string' },
222
+ },
223
+ additionalProperties: false,
224
+ },
225
+ flexibleChild: {
226
+ type: 'object',
227
+ properties: {
228
+ age: { type: 'number' },
229
+ },
230
+ additionalProperties: true,
231
+ },
232
+ },
233
+ additionalProperties: { type: 'string' },
234
+ };
235
+
236
+ const zodSchema = jsonSchemaToZod(schema);
237
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
238
+
239
+ expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
240
+ expect((convertedBack.properties as Record<string, unknown>)?.strictChild).toBeDefined();
241
+ expect((convertedBack.properties as Record<string, unknown>)?.flexibleChild).toBeDefined();
242
+
243
+ const validData = {
244
+ strictChild: { name: 'John' },
245
+ flexibleChild: { age: 30, extra: 'allowed' },
246
+ extraString: 'this should be a string',
247
+ };
248
+ expect(zodSchema.parse(validData)).toEqual(validData);
249
+
250
+ // Test invalid cases
251
+ expect(() =>
252
+ zodSchema.parse({
253
+ strictChild: { name: 'John', extra: 'not allowed' },
254
+ })
255
+ ).toThrow();
256
+
257
+ expect(() =>
258
+ zodSchema.parse({
259
+ extraNumber: 123, // should be string according to additionalProperties
260
+ })
261
+ ).toThrow();
262
+ });
263
+ });
264
+});
ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/package.json
new
+19
@@ -0,0 +1,19 @@
1
+{
2
+ "name": "@e2e-tests/node-json-schema-to-zod-v3",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "scripts": {
7
+ "typecheck": "tsc --noEmit",
8
+ "test:e2e": "bun test e2e.test.ts",
9
+ "test:e2e:node": "bun test e2e.test.ts"
10
+ },
11
+ "dependencies": {
12
+ "@composio/json-schema-to-zod": "workspace:*",
13
+ "zod": "3.25.76",
14
+ "zod-to-json-schema": "^3.25.1"
15
+ },
16
+ "devDependencies": {
17
+ "@e2e-tests/utils": "workspace:*"
18
+ }
19
+}
ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/tsconfig.json
new
+12
@@ -0,0 +1,12 @@
1
+{
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "target": "es2022",
5
+ "module": "esnext",
6
+ "moduleResolution": "bundler",
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "noEmit": true
10
+ },
11
+ "include": ["e2e.test.ts"]
12
+}
ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/README.md
new
+32
@@ -0,0 +1,32 @@
1
+# json-schema-to-zod + Zod v4 Compatibility Test
2
+
3
+Verifies that `@composio/json-schema-to-zod` works correctly with `zod@4`.
4
+
5
+## Why This Exists
6
+
7
+The `@composio/json-schema-to-zod` package must support both Zod v3 and v4. This suite ensures:
8
+
9
+- JSON Schema to Zod conversion works with Zod v4
10
+- All schema types (string, object, array, anyOf) convert correctly
11
+- `additionalProperties` handling works correctly with Zod v4's API
12
+- Complex nested structures and union types parse correctly
13
+
14
+## What It Tests
15
+
16
+| Test | Description |
17
+| ---------------------- | ---------------------------------------------------------- |
18
+| Basic string schema | Converts `{ type: 'string' }` and validates |
19
+| Object schema | Required fields, nested properties, validation constraints |
20
+| Array schema | Typed array items with validation |
21
+| Email format | Format validation for email strings |
22
+| Nested schemas | Complex nested objects and arrays |
23
+| anyOf schemas | Union type conversion |
24
+| additionalProperties | Strict, passthrough, and typed catchall modes |
25
+| Complex nested arrays | Arrays of objects with nested arrays |
26
+| Union with constraints | anyOf with minLength, minimum, and required fields |
27
+
28
+## Running
29
+
30
+```bash
31
+pnpm test:e2e
32
+```
ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/e2e.test.ts
new
+265
@@ -0,0 +1,265 @@
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 { describe, it, expect } from 'bun:test';
10
+
11
+describe('json-schema-to-zod with Zod v4', () => {
12
+ describe('Basic functionality', () => {
13
+ it('converts basic string schema', () => {
14
+ const schema: JsonSchema = { type: 'string' };
15
+ const zodSchema = jsonSchemaToZod(schema);
16
+
17
+ expect(zodSchema.parse('hello')).toBe('hello');
18
+ expect(() => zodSchema.parse(123)).toThrow();
19
+ });
20
+
21
+ it('converts object schema with validation', () => {
22
+ const schema: JsonSchema = {
23
+ type: 'object',
24
+ properties: {
25
+ name: { type: 'string' },
26
+ age: { type: 'number', minimum: 0 },
27
+ },
28
+ required: ['name'],
29
+ };
30
+ const zodSchema = jsonSchemaToZod(schema);
31
+
32
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
33
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
34
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
35
+ });
36
+
37
+ it('converts array schema', () => {
38
+ const schema: JsonSchema = {
39
+ type: 'array',
40
+ items: { type: 'string' },
41
+ };
42
+ const zodSchema = jsonSchemaToZod(schema);
43
+
44
+ expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
45
+ expect(() => zodSchema.parse(['one', 2])).toThrow();
46
+ });
47
+
48
+ it('validates email format', () => {
49
+ const schema: JsonSchema = {
50
+ type: 'string',
51
+ format: 'email',
52
+ };
53
+ const zodSchema = jsonSchemaToZod(schema);
54
+
55
+ expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
56
+ expect(() => zodSchema.parse('invalid-email')).toThrow();
57
+ });
58
+
59
+ it('handles complex nested schemas', () => {
60
+ const schema: JsonSchema = {
61
+ type: 'object',
62
+ properties: {
63
+ user: {
64
+ type: 'object',
65
+ properties: {
66
+ name: { type: 'string' },
67
+ contacts: {
68
+ type: 'array',
69
+ items: {
70
+ type: 'object',
71
+ properties: {
72
+ type: { type: 'string' },
73
+ value: { type: 'string' },
74
+ },
75
+ required: ['type', 'value'],
76
+ },
77
+ },
78
+ },
79
+ required: ['name'],
80
+ },
81
+ },
82
+ required: ['user'],
83
+ };
84
+ const zodSchema = jsonSchemaToZod(schema);
85
+
86
+ const validData = {
87
+ user: {
88
+ name: 'Jane Doe',
89
+ contacts: [
90
+ { type: 'email', value: 'jane@example.com' },
91
+ { type: 'phone', value: '555-1234' },
92
+ ],
93
+ },
94
+ };
95
+ expect(zodSchema.parse(validData)).toEqual(validData);
96
+ });
97
+
98
+ it('handles anyOf schemas', () => {
99
+ const schema: JsonSchema = {
100
+ anyOf: [{ type: 'string' }, { type: 'number' }],
101
+ };
102
+ const zodSchema = jsonSchemaToZod(schema);
103
+
104
+ expect(zodSchema.parse('hello')).toBe('hello');
105
+ expect(zodSchema.parse(42)).toBe(42);
106
+ expect(() => zodSchema.parse(true)).toThrow();
107
+ });
108
+ });
109
+
110
+ describe('Parsing behavior', () => {
111
+ it('converts basic schemas and verifies parsing behavior', () => {
112
+ const schema: JsonSchema = {
113
+ type: 'object',
114
+ properties: {
115
+ name: { type: 'string' },
116
+ age: { type: 'number' },
117
+ },
118
+ required: ['name'],
119
+ additionalProperties: false,
120
+ };
121
+
122
+ const zodSchema = jsonSchemaToZod(schema);
123
+
124
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
125
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
126
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
127
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
128
+ });
129
+
130
+ it('handles additionalProperties: true', () => {
131
+ const schema: JsonSchema = {
132
+ type: 'object',
133
+ properties: {
134
+ name: { type: 'string' },
135
+ },
136
+ additionalProperties: true,
137
+ };
138
+
139
+ const zodSchema = jsonSchemaToZod(schema);
140
+
141
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
142
+ expect(zodSchema.parse({ name: 'John', extra: 'allowed', another: 123 })).toEqual({
143
+ name: 'John',
144
+ extra: 'allowed',
145
+ another: 123,
146
+ });
147
+ });
148
+
149
+ it('handles additionalProperties with type constraint', () => {
150
+ const schema: JsonSchema = {
151
+ type: 'object',
152
+ properties: {
153
+ name: { type: 'string' },
154
+ },
155
+ additionalProperties: { type: 'number' },
156
+ };
157
+
158
+ const zodSchema = jsonSchemaToZod(schema);
159
+
160
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
161
+ expect(zodSchema.parse({ name: 'John', age: 30, count: 5 })).toEqual({
162
+ name: 'John',
163
+ age: 30,
164
+ count: 5,
165
+ });
166
+ expect(() => zodSchema.parse({ name: 'John', extra: 'string' })).toThrow();
167
+ });
168
+
169
+ it('handles nested objects with different additionalProperties settings', () => {
170
+ const schema: JsonSchema = {
171
+ type: 'object',
172
+ properties: {
173
+ strictChild: {
174
+ type: 'object',
175
+ properties: {
176
+ name: { type: 'string' },
177
+ },
178
+ additionalProperties: false,
179
+ },
180
+ flexibleChild: {
181
+ type: 'object',
182
+ properties: {
183
+ age: { type: 'number' },
184
+ },
185
+ additionalProperties: true,
186
+ },
187
+ },
188
+ additionalProperties: { type: 'string' },
189
+ };
190
+
191
+ const zodSchema = jsonSchemaToZod(schema);
192
+
193
+ const validData = {
194
+ strictChild: { name: 'John' },
195
+ flexibleChild: { age: 30, extra: 'allowed' },
196
+ extraString: 'this should be a string',
197
+ };
198
+ expect(zodSchema.parse(validData)).toEqual(validData);
199
+
200
+ expect(() =>
201
+ zodSchema.parse({
202
+ strictChild: { name: 'John', extra: 'not allowed' },
203
+ })
204
+ ).toThrow();
205
+
206
+ expect(() =>
207
+ zodSchema.parse({
208
+ extraNumber: 123,
209
+ })
210
+ ).toThrow();
211
+ });
212
+
213
+ it('handles complex nested arrays', () => {
214
+ const schema: JsonSchema = {
215
+ type: 'array',
216
+ items: {
217
+ type: 'object',
218
+ properties: {
219
+ id: { type: 'number' },
220
+ tags: {
221
+ type: 'array',
222
+ items: { type: 'string' },
223
+ },
224
+ },
225
+ required: ['id'],
226
+ },
227
+ };
228
+
229
+ const zodSchema = jsonSchemaToZod(schema);
230
+
231
+ const validData = [{ id: 1, tags: ['a', 'b'] }, { id: 2, tags: ['c'] }, { id: 3 }];
232
+ expect(zodSchema.parse(validData)).toEqual(validData);
233
+
234
+ expect(() =>
235
+ zodSchema.parse([{ tags: ['a', 'b'] }])
236
+ ).toThrow();
237
+ });
238
+
239
+ it('handles union types with anyOf', () => {
240
+ const schema: JsonSchema = {
241
+ anyOf: [
242
+ { type: 'string', minLength: 3 },
243
+ { type: 'number', minimum: 0 },
244
+ {
245
+ type: 'object',
246
+ properties: {
247
+ type: { type: 'string' },
248
+ },
249
+ required: ['type'],
250
+ },
251
+ ],
252
+ };
253
+
254
+ const zodSchema = jsonSchemaToZod(schema);
255
+
256
+ expect(zodSchema.parse('hello')).toBe('hello');
257
+ expect(zodSchema.parse(42)).toBe(42);
258
+ expect(zodSchema.parse({ type: 'custom' })).toEqual({ type: 'custom' });
259
+
260
+ expect(() => zodSchema.parse('ab')).toThrow();
261
+ expect(() => zodSchema.parse(-1)).toThrow();
262
+ expect(() => zodSchema.parse({ foo: 'bar' })).toThrow();
263
+ });
264
+ });
265
+});
ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/package.json
new
+18
@@ -0,0 +1,18 @@
1
+{
2
+ "name": "@e2e-tests/node-json-schema-to-zod-v4",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "scripts": {
7
+ "typecheck": "tsc --noEmit",
8
+ "test:e2e": "bun test e2e.test.ts",
9
+ "test:e2e:node": "bun test e2e.test.ts"
10
+ },
11
+ "dependencies": {
12
+ "@composio/json-schema-to-zod": "workspace:*",
13
+ "zod": "^4.1.0"
14
+ },
15
+ "devDependencies": {
16
+ "@e2e-tests/utils": "workspace:*"
17
+ }
18
+}
ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/tsconfig.json
new
+12
@@ -0,0 +1,12 @@
1
+{
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "target": "es2022",
5
+ "module": "esnext",
6
+ "moduleResolution": "bundler",
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "noEmit": true
10
+ },
11
+ "include": ["e2e.test.ts"]
12
+}
ts/packages/json-schema-to-zod/test/e2e/.gitignore
deleted
-1
@@ -1 +0,0 @@
1
-node_modules/
ts/packages/json-schema-to-zod/test/e2e/README.md
deleted
-8
@@ -1,8 +0,0 @@
1
-# E2E Tests
2
-
3
-End-to-end tests that verify `@composio/json-schema-to-zod` works correctly with different versions of zod.
4
-
5
-## Structure
6
-
7
-- `zod-v3/` - Tests with zod 3.25.76 and `zod-to-json-schema` for round-trip conversion
8
-- `zod-v4/` - Tests with zod ^4.0.0 and native `z.toJSONSchema()` for validation
ts/packages/json-schema-to-zod/test/e2e/zod-v3/basic.test.ts
deleted
-102
@@ -1,102 +0,0 @@
1
-import { describe, it, expect } from 'vitest';
2
-import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
3
-import type { JsonSchema } from '@composio/json-schema-to-zod';
4
-
5
-describe('E2E with Zod v3 - Basic functionality', () => {
6
- it('should convert basic string schema', () => {
7
- const schema: JsonSchema = { type: 'string' };
8
- const zodSchema = jsonSchemaToZod(schema);
9
-
10
- // Note: instanceof check doesn't work reliably across zod versions
11
- expect(zodSchema.parse('hello')).toBe('hello');
12
- expect(() => zodSchema.parse(123)).toThrow();
13
- });
14
-
15
- it('should convert object schema with validation', () => {
16
- const schema: JsonSchema = {
17
- type: 'object',
18
- properties: {
19
- name: { type: 'string' },
20
- age: { type: 'number', minimum: 0 },
21
- },
22
- required: ['name'],
23
- };
24
- const zodSchema = jsonSchemaToZod(schema);
25
-
26
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
27
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
28
- expect(() => zodSchema.parse({ age: 30 })).toThrow();
29
- });
30
-
31
- it('should convert array schema', () => {
32
- const schema: JsonSchema = {
33
- type: 'array',
34
- items: { type: 'string' },
35
- };
36
- const zodSchema = jsonSchemaToZod(schema);
37
-
38
- expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
39
- expect(() => zodSchema.parse(['one', 2])).toThrow();
40
- });
41
-
42
- it('should validate email format', () => {
43
- const schema: JsonSchema = {
44
- type: 'string',
45
- format: 'email',
46
- };
47
- const zodSchema = jsonSchemaToZod(schema);
48
-
49
- expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
50
- expect(() => zodSchema.parse('invalid-email')).toThrow();
51
- });
52
-
53
- it('should handle complex nested schemas', () => {
54
- const schema: JsonSchema = {
55
- type: 'object',
56
- properties: {
57
- user: {
58
- type: 'object',
59
- properties: {
60
- name: { type: 'string' },
61
- contacts: {
62
- type: 'array',
63
- items: {
64
- type: 'object',
65
- properties: {
66
- type: { type: 'string' },
67
- value: { type: 'string' },
68
- },
69
- required: ['type', 'value'],
70
- },
71
- },
72
- },
73
- required: ['name'],
74
- },
75
- },
76
- required: ['user'],
77
- };
78
- const zodSchema = jsonSchemaToZod(schema);
79
-
80
- const validData = {
81
- user: {
82
- name: 'Jane Doe',
83
- contacts: [
84
- { type: 'email', value: 'jane@example.com' },
85
- { type: 'phone', value: '555-1234' },
86
- ],
87
- },
88
- };
89
- expect(zodSchema.parse(validData)).toEqual(validData);
90
- });
91
-
92
- it('should handle anyOf schemas', () => {
93
- const schema: JsonSchema = {
94
- anyOf: [{ type: 'string' }, { type: 'number' }],
95
- };
96
- const zodSchema = jsonSchemaToZod(schema);
97
-
98
- expect(zodSchema.parse('hello')).toBe('hello');
99
- expect(zodSchema.parse(42)).toBe(42);
100
- expect(() => zodSchema.parse(true)).toThrow();
101
- });
102
-});
ts/packages/json-schema-to-zod/test/e2e/zod-v3/package.json
deleted
-17
@@ -1,17 +0,0 @@
1
-{
2
- "name": "@test-e2e/json-schema-to-zod-e2e-zod-v3",
3
- "version": "1.0.0",
4
- "private": true,
5
- "type": "module",
6
- "scripts": {
7
- "test": "vitest run"
8
- },
9
- "dependencies": {
10
- "@composio/json-schema-to-zod": "workspace:*",
11
- "zod": "3.25.76",
12
- "zod-to-json-schema": "catalog:"
13
- },
14
- "devDependencies": {
15
- "vitest": "catalog:"
16
- }
17
-}
ts/packages/json-schema-to-zod/test/e2e/zod-v3/round-trip.test.ts
deleted
-173
@@ -1,173 +0,0 @@
1
-import { describe, it, expect } from 'vitest';
2
-import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
3
-import zodToJsonSchema from 'zod-to-json-schema';
4
-import type { JsonSchema } from '@composio/json-schema-to-zod';
5
-
6
-describe('E2E with Zod v3 - Round-trip conversion', () => {
7
- const testRoundTrip = (originalSchema: JsonSchema, expectedAdditionalProperties: any) => {
8
- const zodSchema = jsonSchemaToZod(originalSchema);
9
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' }) as any;
10
-
11
- const { $schema, ...cleanConverted } = convertedBack;
12
-
13
- return {
14
- zodSchema,
15
- convertedBack: cleanConverted,
16
- additionalPropertiesMatch:
17
- convertedBack.additionalProperties === expectedAdditionalProperties,
18
- };
19
- };
20
-
21
- it('should preserve additionalProperties: true for empty objects', () => {
22
- const schema: JsonSchema = {
23
- type: 'object',
24
- additionalProperties: true,
25
- };
26
-
27
- const { zodSchema, convertedBack, additionalPropertiesMatch } = testRoundTrip(schema, true);
28
-
29
- expect(additionalPropertiesMatch).toBe(true);
30
- expect(convertedBack.additionalProperties).toBe(true);
31
- expect(convertedBack.type).toBe('object');
32
- expect(convertedBack.properties).toEqual({});
33
-
34
- expect(zodSchema.parse({})).toEqual({});
35
- expect(zodSchema.parse({ any: 'value', number: 123 })).toEqual({ any: 'value', number: 123 });
36
- });
37
-
38
- it('should preserve additionalProperties: true for objects with properties', () => {
39
- const schema: JsonSchema = {
40
- type: 'object',
41
- properties: {
42
- name: { type: 'string' },
43
- age: { type: 'number' },
44
- },
45
- required: ['name'],
46
- additionalProperties: true,
47
- };
48
-
49
- const { zodSchema, convertedBack, additionalPropertiesMatch } = testRoundTrip(schema, true);
50
-
51
- expect(additionalPropertiesMatch).toBe(true);
52
- expect(convertedBack.additionalProperties).toBe(true);
53
- expect(convertedBack.properties).toBeDefined();
54
- expect(convertedBack.required).toEqual(['name']);
55
-
56
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
57
- expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
58
- name: 'John',
59
- age: 30,
60
- extra: 'field',
61
- });
62
- });
63
-
64
- it('should preserve additionalProperties: false for empty objects', () => {
65
- const schema: JsonSchema = {
66
- type: 'object',
67
- additionalProperties: false,
68
- };
69
-
70
- const zodSchema = jsonSchemaToZod(schema);
71
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' }) as any;
72
-
73
- // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
74
- // which is semantically equivalent to additionalProperties: false
75
- expect(
76
- convertedBack.additionalProperties === false ||
77
- (convertedBack.not && typeof convertedBack.not === 'object')
78
- ).toBe(true);
79
- expect(convertedBack.type).toBe('object');
80
-
81
- expect(zodSchema.parse({})).toEqual({});
82
- expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
83
- });
84
-
85
- it('should preserve additionalProperties: false for objects with properties', () => {
86
- const schema: JsonSchema = {
87
- type: 'object',
88
- properties: {
89
- name: { type: 'string' },
90
- },
91
- additionalProperties: false,
92
- };
93
-
94
- const zodSchema = jsonSchemaToZod(schema);
95
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' }) as any;
96
-
97
- expect(convertedBack.additionalProperties).toBe(false);
98
- expect(convertedBack.properties).toBeDefined();
99
-
100
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
101
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
102
- });
103
-
104
- it('should handle additionalProperties with type schema', () => {
105
- const schema: JsonSchema = {
106
- type: 'object',
107
- properties: {
108
- name: { type: 'string' },
109
- },
110
- additionalProperties: { type: 'number' },
111
- };
112
-
113
- const zodSchema = jsonSchemaToZod(schema);
114
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' }) as any;
115
-
116
- expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
117
- expect(convertedBack.properties).toBeDefined();
118
-
119
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
120
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
121
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
122
- });
123
-
124
- it('should handle nested objects with different additionalProperties settings', () => {
125
- const schema: JsonSchema = {
126
- type: 'object',
127
- properties: {
128
- strictChild: {
129
- type: 'object',
130
- properties: {
131
- name: { type: 'string' },
132
- },
133
- additionalProperties: false,
134
- },
135
- flexibleChild: {
136
- type: 'object',
137
- properties: {
138
- age: { type: 'number' },
139
- },
140
- additionalProperties: true,
141
- },
142
- },
143
- additionalProperties: { type: 'string' },
144
- };
145
-
146
- const zodSchema = jsonSchemaToZod(schema);
147
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' }) as any;
148
-
149
- expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
150
- expect(convertedBack.properties?.strictChild).toBeDefined();
151
- expect(convertedBack.properties?.flexibleChild).toBeDefined();
152
-
153
- const validData = {
154
- strictChild: { name: 'John' },
155
- flexibleChild: { age: 30, extra: 'allowed' },
156
- extraString: 'this should be a string',
157
- };
158
- expect(zodSchema.parse(validData)).toEqual(validData);
159
-
160
- // Test invalid cases
161
- expect(() =>
162
- zodSchema.parse({
163
- strictChild: { name: 'John', extra: 'not allowed' },
164
- })
165
- ).toThrow();
166
-
167
- expect(() =>
168
- zodSchema.parse({
169
- extraNumber: 123, // should be string according to additionalProperties
170
- })
171
- ).toThrow();
172
- });
173
-});
ts/packages/json-schema-to-zod/test/e2e/zod-v4/basic.test.ts
deleted
-102
@@ -1,102 +0,0 @@
1
-import { describe, it, expect } from 'vitest';
2
-import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
3
-import type { JsonSchema } from '@composio/json-schema-to-zod';
4
-
5
-describe('E2E with Zod v4 - Basic functionality', () => {
6
- it('should convert basic string schema', () => {
7
- const schema: JsonSchema = { type: 'string' };
8
- const zodSchema = jsonSchemaToZod(schema);
9
-
10
- // Note: instanceof check doesn't work reliably across zod versions
11
- expect(zodSchema.parse('hello')).toBe('hello');
12
- expect(() => zodSchema.parse(123)).toThrow();
13
- });
14
-
15
- it('should convert object schema with validation', () => {
16
- const schema: JsonSchema = {
17
- type: 'object',
18
- properties: {
19
- name: { type: 'string' },
20
- age: { type: 'number', minimum: 0 },
21
- },
22
- required: ['name'],
23
- };
24
- const zodSchema = jsonSchemaToZod(schema);
25
-
26
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
27
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
28
- expect(() => zodSchema.parse({ age: 30 })).toThrow();
29
- });
30
-
31
- it('should convert array schema', () => {
32
- const schema: JsonSchema = {
33
- type: 'array',
34
- items: { type: 'string' },
35
- };
36
- const zodSchema = jsonSchemaToZod(schema);
37
-
38
- expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
39
- expect(() => zodSchema.parse(['one', 2])).toThrow();
40
- });
41
-
42
- it('should validate email format', () => {
43
- const schema: JsonSchema = {
44
- type: 'string',
45
- format: 'email',
46
- };
47
- const zodSchema = jsonSchemaToZod(schema);
48
-
49
- expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
50
- expect(() => zodSchema.parse('invalid-email')).toThrow();
51
- });
52
-
53
- it('should handle complex nested schemas', () => {
54
- const schema: JsonSchema = {
55
- type: 'object',
56
- properties: {
57
- user: {
58
- type: 'object',
59
- properties: {
60
- name: { type: 'string' },
61
- contacts: {
62
- type: 'array',
63
- items: {
64
- type: 'object',
65
- properties: {
66
- type: { type: 'string' },
67
- value: { type: 'string' },
68
- },
69
- required: ['type', 'value'],
70
- },
71
- },
72
- },
73
- required: ['name'],
74
- },
75
- },
76
- required: ['user'],
77
- };
78
- const zodSchema = jsonSchemaToZod(schema);
79
-
80
- const validData = {
81
- user: {
82
- name: 'Jane Doe',
83
- contacts: [
84
- { type: 'email', value: 'jane@example.com' },
85
- { type: 'phone', value: '555-1234' },
86
- ],
87
- },
88
- };
89
- expect(zodSchema.parse(validData)).toEqual(validData);
90
- });
91
-
92
- it('should handle anyOf schemas', () => {
93
- const schema: JsonSchema = {
94
- anyOf: [{ type: 'string' }, { type: 'number' }],
95
- };
96
- const zodSchema = jsonSchemaToZod(schema);
97
-
98
- expect(zodSchema.parse('hello')).toBe('hello');
99
- expect(zodSchema.parse(42)).toBe(42);
100
- expect(() => zodSchema.parse(true)).toThrow();
101
- });
102
-});
ts/packages/json-schema-to-zod/test/e2e/zod-v4/package.json
deleted
-16
@@ -1,16 +0,0 @@
1
-{
2
- "name": "@test-e2e/json-schema-to-zod-e2e-zod-v4",
3
- "version": "1.0.0",
4
- "private": true,
5
- "type": "module",
6
- "scripts": {
7
- "test": "vitest run"
8
- },
9
- "dependencies": {
10
- "@composio/json-schema-to-zod": "workspace:*",
11
- "zod": "^4.1.0"
12
- },
13
- "devDependencies": {
14
- "vitest": "catalog:"
15
- }
16
-}
ts/packages/json-schema-to-zod/test/e2e/zod-v4/round-trip.test.ts
deleted
-166
@@ -1,166 +0,0 @@
1
-import { describe, it, expect } from 'vitest';
2
-import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
3
-import type { JsonSchema } from '@composio/json-schema-to-zod';
4
-
5
-describe('E2E with Zod v4 - Round-trip conversion with native toJSONSchema', () => {
6
- it('should convert basic schemas and verify parsing behavior', () => {
7
- const schema: JsonSchema = {
8
- type: 'object',
9
- properties: {
10
- name: { type: 'string' },
11
- age: { type: 'number' },
12
- },
13
- required: ['name'],
14
- additionalProperties: false,
15
- };
16
-
17
- const zodSchema = jsonSchemaToZod(schema);
18
-
19
- // Test parsing behavior
20
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
21
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
22
- expect(() => zodSchema.parse({ age: 30 })).toThrow();
23
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
24
- });
25
-
26
- it('should handle additionalProperties: true', () => {
27
- const schema: JsonSchema = {
28
- type: 'object',
29
- properties: {
30
- name: { type: 'string' },
31
- },
32
- additionalProperties: true,
33
- };
34
-
35
- const zodSchema = jsonSchemaToZod(schema);
36
-
37
- // Test parsing behavior
38
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
39
- expect(zodSchema.parse({ name: 'John', extra: 'allowed', another: 123 })).toEqual({
40
- name: 'John',
41
- extra: 'allowed',
42
- another: 123,
43
- });
44
- });
45
-
46
- it('should handle additionalProperties with type constraint', () => {
47
- const schema: JsonSchema = {
48
- type: 'object',
49
- properties: {
50
- name: { type: 'string' },
51
- },
52
- additionalProperties: { type: 'number' },
53
- };
54
-
55
- const zodSchema = jsonSchemaToZod(schema);
56
-
57
- // Test parsing behavior
58
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
59
- expect(zodSchema.parse({ name: 'John', age: 30, count: 5 })).toEqual({
60
- name: 'John',
61
- age: 30,
62
- count: 5,
63
- });
64
- expect(() => zodSchema.parse({ name: 'John', extra: 'string' })).toThrow();
65
- });
66
-
67
- it('should handle nested objects with different additionalProperties settings', () => {
68
- const schema: JsonSchema = {
69
- type: 'object',
70
- properties: {
71
- strictChild: {
72
- type: 'object',
73
- properties: {
74
- name: { type: 'string' },
75
- },
76
- additionalProperties: false,
77
- },
78
- flexibleChild: {
79
- type: 'object',
80
- properties: {
81
- age: { type: 'number' },
82
- },
83
- additionalProperties: true,
84
- },
85
- },
86
- additionalProperties: { type: 'string' },
87
- };
88
-
89
- const zodSchema = jsonSchemaToZod(schema);
90
-
91
- // Test parsing behavior
92
- const validData = {
93
- strictChild: { name: 'John' },
94
- flexibleChild: { age: 30, extra: 'allowed' },
95
- extraString: 'this should be a string',
96
- };
97
- expect(zodSchema.parse(validData)).toEqual(validData);
98
-
99
- // Test invalid cases
100
- expect(() =>
101
- zodSchema.parse({
102
- strictChild: { name: 'John', extra: 'not allowed' },
103
- })
104
- ).toThrow();
105
-
106
- expect(() =>
107
- zodSchema.parse({
108
- extraNumber: 123, // should be string according to additionalProperties
109
- })
110
- ).toThrow();
111
- });
112
-
113
- it('should handle complex nested arrays', () => {
114
- const schema: JsonSchema = {
115
- type: 'array',
116
- items: {
117
- type: 'object',
118
- properties: {
119
- id: { type: 'number' },
120
- tags: {
121
- type: 'array',
122
- items: { type: 'string' },
123
- },
124
- },
125
- required: ['id'],
126
- },
127
- };
128
-
129
- const zodSchema = jsonSchemaToZod(schema);
130
-
131
- const validData = [{ id: 1, tags: ['a', 'b'] }, { id: 2, tags: ['c'] }, { id: 3 }];
132
- expect(zodSchema.parse(validData)).toEqual(validData);
133
-
134
- expect(() =>
135
- zodSchema.parse([
136
- { tags: ['a', 'b'] }, // missing required id
137
- ])
138
- ).toThrow();
139
- });
140
-
141
- it('should handle union types with anyOf', () => {
142
- const schema: JsonSchema = {
143
- anyOf: [
144
- { type: 'string', minLength: 3 },
145
- { type: 'number', minimum: 0 },
146
- {
147
- type: 'object',
148
- properties: {
149
- type: { type: 'string' },
150
- },
151
- required: ['type'],
152
- },
153
- ],
154
- };
155
-
156
- const zodSchema = jsonSchemaToZod(schema);
157
-
158
- expect(zodSchema.parse('hello')).toBe('hello');
159
- expect(zodSchema.parse(42)).toBe(42);
160
- expect(zodSchema.parse({ type: 'custom' })).toEqual({ type: 'custom' });
161
-
162
- expect(() => zodSchema.parse('ab')).toThrow(); // too short
163
- expect(() => zodSchema.parse(-1)).toThrow(); // negative
164
- expect(() => zodSchema.parse({ foo: 'bar' })).toThrow(); // missing required type
165
- });
166
-});