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): fix inconsistency in @e2e-tests/e2e-json-schema-to-zod-v*
jkomyno committed
Jan 27, 2026 at 03:34 UTC
791c41f5183aadb0f057ddd2ee9d967f06cdbf66
2 files changed
+496
-482
ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/e2e.test.ts
+257
-250
@@ -6,274 +6,281 @@
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
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: {
13
+e2e(import.meta.url, {
14
+ nodeVersions: ['current'],
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' },
68
- contacts: {
69
- type: 'array',
70
- items: {
71
- type: 'object',
72
- properties: {
73
- type: { type: 'string' },
74
- value: { 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
},
76
- required: ['type', 'value'],
83
},
84
+ required: ['name'],
85
},
86
},
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
- // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
120
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
121
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
122
- expect(convertedBack.additionalProperties).toBe(true);
123
- // @ts-expect-error Property 'type' does not exist on type
124
- expect(convertedBack.type).toBe('object');
125
-
126
- expect(zodSchema.parse({})).toEqual({});
127
- expect(zodSchema.parse({ any: 'value', number: 123 })).toEqual({ any: 'value', number: 123 });
128
- });
129
-
130
- it('preserves additionalProperties: true for objects with properties', () => {
131
- const schema: JsonSchema = {
132
- type: 'object',
133
- properties: {
134
- name: { type: 'string' },
135
- age: { type: 'number' },
136
- },
137
- required: ['name'],
138
- additionalProperties: true,
139
- };
140
-
141
- const zodSchema = jsonSchemaToZod(schema);
142
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
143
-
144
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
145
- expect(convertedBack.additionalProperties).toBe(true);
146
- // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
147
- expect(convertedBack.properties).toBeDefined();
148
- // @ts-expect-error Property 'required' does not exist on type
149
- expect(convertedBack.required).toEqual(['name']);
150
-
151
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
152
- expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
153
- name: 'John',
154
- age: 30,
155
- extra: 'field',
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
});
157
- });
158
-
159
- it('preserves additionalProperties: false for empty objects', () => {
160
- const schema: JsonSchema = {
161
- type: 'object',
162
- additionalProperties: false,
163
- };
164
-
165
- const zodSchema = jsonSchemaToZod(schema);
166
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
167
-
168
- // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
169
- // which is semantically equivalent to additionalProperties: false
170
- const additionalPropsValid =
171
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
172
- convertedBack.additionalProperties === false ||
173
- // @ts-expect-error Property 'not' does not exist on type.
174
- (convertedBack.not && typeof convertedBack.not === 'object');
175
- expect(additionalPropsValid).toBe(true);
176
- // @ts-expect-error Property 'type' does not exist on type.
177
- expect(convertedBack.type).toBe('object');
178
-
179
- expect(zodSchema.parse({})).toEqual({});
180
- expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
181
- });
182
-
183
- it('preserves additionalProperties: false for objects with properties', () => {
184
- const schema: JsonSchema = {
185
- type: 'object',
186
- properties: {
187
- name: { type: 'string' },
188
- },
189
- additionalProperties: false,
190
- };
191
-
192
- const zodSchema = jsonSchemaToZod(schema);
193
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
194
-
195
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
196
- expect(convertedBack.additionalProperties).toBe(false);
197
- // @ts-expect-error Property 'properties' does not exist on type.
198
- expect(convertedBack.properties).toBeDefined();
199
-
200
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
201
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
202
- });
203
-
204
- it('handles additionalProperties with type schema', () => {
205
- const schema: JsonSchema = {
206
- type: 'object',
207
- properties: {
208
- name: { type: 'string' },
209
- },
210
- additionalProperties: { type: 'number' },
211
- };
212
-
213
- const zodSchema = jsonSchemaToZod(schema);
214
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
215
-
216
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
217
- expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
218
- // @ts-expect-error Property 'properties' does not exist on type.
219
- expect(convertedBack.properties).toBeDefined();
220
-
221
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
222
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
223
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
224
- });
225
-
226
- it('handles nested objects with different additionalProperties settings', () => {
227
- const schema: JsonSchema = {
228
- type: 'object',
229
- properties: {
230
- strictChild: {
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({ any: 'value', number: 123 });
132
+ });
133
+
134
+ it('preserves additionalProperties: true for objects with properties', () => {
135
+ const schema: JsonSchema = {
136
type: 'object',
137
properties: {
138
name: { type: 'string' },
139
+ age: { type: 'number' },
140
},
141
+ required: ['name'],
142
+ additionalProperties: true,
143
+ };
144
+
145
+ const zodSchema = jsonSchemaToZod(schema);
146
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
147
+
148
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
149
+ expect(convertedBack.additionalProperties).toBe(true);
150
+ // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
151
+ expect(convertedBack.properties).toBeDefined();
152
+ // @ts-expect-error Property 'required' does not exist on type
153
+ expect(convertedBack.required).toEqual(['name']);
154
+
155
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
156
+ expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
157
+ name: 'John',
158
+ age: 30,
159
+ extra: 'field',
160
+ });
161
+ });
162
+
163
+ it('preserves additionalProperties: false for empty objects', () => {
164
+ const schema: JsonSchema = {
165
+ type: 'object',
166
additionalProperties: false,
236
- },
237
- flexibleChild: {
167
+ };
168
+
169
+ const zodSchema = jsonSchemaToZod(schema);
170
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
171
+
172
+ // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
173
+ // which is semantically equivalent to additionalProperties: false
174
+ const additionalPropsValid =
175
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
176
+ convertedBack.additionalProperties === false ||
177
+ // @ts-expect-error Property 'not' does not exist on type.
178
+ (convertedBack.not && typeof convertedBack.not === 'object');
179
+ expect(additionalPropsValid).toBe(true);
180
+ // @ts-expect-error Property 'type' does not exist on type.
181
+ expect(convertedBack.type).toBe('object');
182
+
183
+ expect(zodSchema.parse({})).toEqual({});
184
+ expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
185
+ });
186
+
187
+ it('preserves additionalProperties: false for objects with properties', () => {
188
+ const schema: JsonSchema = {
189
type: 'object',
190
properties: {
240
- age: { type: 'number' },
191
+ name: { type: 'string' },
192
},
242
- additionalProperties: true,
243
- },
244
- },
245
- additionalProperties: { type: 'string' },
246
- };
247
-
248
- const zodSchema = jsonSchemaToZod(schema);
249
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
250
-
251
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
252
- expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
253
- // @ts-expect-error Property 'properties' does not exist on type.
254
- expect((convertedBack.properties as Record<string, unknown>)?.strictChild).toBeDefined();
255
- // @ts-expect-error Property 'properties' does not exist on type.
256
- expect((convertedBack.properties as Record<string, unknown>)?.flexibleChild).toBeDefined();
257
-
258
- const validData = {
259
- strictChild: { name: 'John' },
260
- flexibleChild: { age: 30, extra: 'allowed' },
261
- extraString: 'this should be a string',
262
- };
263
- expect(zodSchema.parse(validData)).toEqual(validData);
264
-
265
- // Test invalid cases
266
- expect(() =>
267
- zodSchema.parse({
268
- strictChild: { name: 'John', extra: 'not allowed' },
269
- })
270
- ).toThrow();
271
-
272
- expect(() =>
273
- zodSchema.parse({
274
- extraNumber: 123, // should be string according to additionalProperties
275
- })
276
- ).toThrow();
193
+ additionalProperties: false,
194
+ };
195
+
196
+ const zodSchema = jsonSchemaToZod(schema);
197
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
198
+
199
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
200
+ expect(convertedBack.additionalProperties).toBe(false);
201
+ // @ts-expect-error Property 'properties' does not exist on type.
202
+ expect(convertedBack.properties).toBeDefined();
203
+
204
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
205
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
206
+ });
207
+
208
+ it('handles additionalProperties with type schema', () => {
209
+ const schema: JsonSchema = {
210
+ type: 'object',
211
+ properties: {
212
+ name: { type: 'string' },
213
+ },
214
+ additionalProperties: { type: 'number' },
215
+ };
216
+
217
+ const zodSchema = jsonSchemaToZod(schema);
218
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
219
+
220
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
221
+ expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
222
+ // @ts-expect-error Property 'properties' does not exist on type.
223
+ expect(convertedBack.properties).toBeDefined();
224
+
225
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
226
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
227
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
228
+ });
229
+
230
+ it('handles nested objects with different additionalProperties settings', () => {
231
+ const schema: JsonSchema = {
232
+ type: 'object',
233
+ properties: {
234
+ strictChild: {
235
+ type: 'object',
236
+ properties: {
237
+ name: { type: 'string' },
238
+ },
239
+ additionalProperties: false,
240
+ },
241
+ flexibleChild: {
242
+ type: 'object',
243
+ properties: {
244
+ age: { type: 'number' },
245
+ },
246
+ additionalProperties: true,
247
+ },
248
+ },
249
+ additionalProperties: { type: 'string' },
250
+ };
251
+
252
+ const zodSchema = jsonSchemaToZod(schema);
253
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
254
+
255
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
256
+ expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
257
+ // @ts-expect-error Property 'properties' does not exist on type.
258
+ expect((convertedBack.properties as Record<string, unknown>)?.strictChild).toBeDefined();
259
+ // @ts-expect-error Property 'properties' does not exist on type.
260
+ expect((convertedBack.properties as Record<string, unknown>)?.flexibleChild).toBeDefined();
261
+
262
+ const validData = {
263
+ strictChild: { name: 'John' },
264
+ flexibleChild: { age: 30, extra: 'allowed' },
265
+ extraString: 'this should be a string',
266
+ };
267
+ expect(zodSchema.parse(validData)).toEqual(validData);
268
+
269
+ // Test invalid cases
270
+ expect(() =>
271
+ zodSchema.parse({
272
+ strictChild: { name: 'John', extra: 'not allowed' },
273
+ })
274
+ ).toThrow();
275
+
276
+ expect(() =>
277
+ zodSchema.parse({
278
+ extraNumber: 123, // should be string according to additionalProperties
279
+ })
280
+ ).toThrow();
281
+ });
282
+ });
283
});
278
- });
284
+ },
285
});
286
+
ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/e2e.test.ts
+239
-232
@@ -6,260 +6,267 @@
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
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: {
12
+e2e(import.meta.url, {
13
+ nodeVersions: ['current'],
14
+ defineTests: () => {
15
+ describe('json-schema-to-zod with Zod v4', () => {
16
+ describe('Basic functionality', () => {
17
+ it('converts basic string schema', () => {
18
+ const schema: JsonSchema = { type: 'string' };
19
+ const zodSchema = jsonSchemaToZod(schema);
20
+
21
+ expect(zodSchema.parse('hello')).toBe('hello');
22
+ expect(() => zodSchema.parse(123)).toThrow();
23
+ });
24
+
25
+ it('converts object schema with validation', () => {
26
+ const schema: JsonSchema = {
27
type: 'object',
28
properties: {
29
name: { type: 'string' },
67
- contacts: {
68
- type: 'array',
69
- items: {
70
- type: 'object',
71
- properties: {
72
- type: { type: 'string' },
73
- value: { type: 'string' },
30
+ age: { type: 'number', minimum: 0 },
31
+ },
32
+ required: ['name'],
33
+ };
34
+ const zodSchema = jsonSchemaToZod(schema);
35
+
36
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
37
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
38
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
39
+ });
40
+
41
+ it('converts array schema', () => {
42
+ const schema: JsonSchema = {
43
+ type: 'array',
44
+ items: { type: 'string' },
45
+ };
46
+ const zodSchema = jsonSchemaToZod(schema);
47
+
48
+ expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
49
+ expect(() => zodSchema.parse(['one', 2])).toThrow();
50
+ });
51
+
52
+ it('validates email format', () => {
53
+ const schema: JsonSchema = {
54
+ type: 'string',
55
+ format: 'email',
56
+ };
57
+ const zodSchema = jsonSchemaToZod(schema);
58
+
59
+ expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
60
+ expect(() => zodSchema.parse('invalid-email')).toThrow();
61
+ });
62
+
63
+ it('handles complex nested schemas', () => {
64
+ const schema: JsonSchema = {
65
+ type: 'object',
66
+ properties: {
67
+ user: {
68
+ type: 'object',
69
+ properties: {
70
+ name: { type: 'string' },
71
+ contacts: {
72
+ type: 'array',
73
+ items: {
74
+ type: 'object',
75
+ properties: {
76
+ type: { type: 'string' },
77
+ value: { type: 'string' },
78
+ },
79
+ required: ['type', 'value'],
80
+ },
81
},
75
- required: ['type', 'value'],
82
},
83
+ required: ['name'],
84
},
85
},
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,
86
+ required: ['user'],
87
+ };
88
+ const zodSchema = jsonSchemaToZod(schema);
89
+
90
+ const validData = {
91
+ user: {
92
+ name: 'Jane Doe',
93
+ contacts: [
94
+ { type: 'email', value: 'jane@example.com' },
95
+ { type: 'phone', value: '555-1234' },
96
+ ],
97
+ },
98
+ };
99
+ expect(zodSchema.parse(validData)).toEqual(validData);
100
+ });
101
+
102
+ it('handles anyOf schemas', () => {
103
+ const schema: JsonSchema = {
104
+ anyOf: [{ type: 'string' }, { type: 'number' }],
105
+ };
106
+ const zodSchema = jsonSchemaToZod(schema);
107
+
108
+ expect(zodSchema.parse('hello')).toBe('hello');
109
+ expect(zodSchema.parse(42)).toBe(42);
110
+ expect(() => zodSchema.parse(true)).toThrow();
111
+ });
112
});
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: {
113
+
114
+ describe('Parsing behavior', () => {
115
+ it('converts basic schemas and verifies parsing behavior', () => {
116
+ const schema: JsonSchema = {
117
type: 'object',
118
properties: {
119
name: { type: 'string' },
120
+ age: { type: 'number' },
121
},
122
+ required: ['name'],
123
additionalProperties: false,
179
- },
180
- flexibleChild: {
124
+ };
125
+
126
+ const zodSchema = jsonSchemaToZod(schema);
127
+
128
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
129
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
130
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
131
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
132
+ });
133
+
134
+ it('handles additionalProperties: true', () => {
135
+ const schema: JsonSchema = {
136
type: 'object',
137
properties: {
183
- age: { type: 'number' },
138
+ name: { type: 'string' },
139
},
140
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' },
141
+ };
142
+
143
+ const zodSchema = jsonSchemaToZod(schema);
144
+
145
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
146
+ expect(zodSchema.parse({ name: 'John', extra: 'allowed', another: 123 })).toEqual({
147
+ name: 'John',
148
+ extra: 'allowed',
149
+ another: 123,
150
+ });
151
+ });
152
+
153
+ it('handles additionalProperties with type constraint', () => {
154
+ const schema: JsonSchema = {
155
+ type: 'object',
156
+ properties: {
157
+ name: { type: 'string' },
158
},
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
- {
159
+ additionalProperties: { type: 'number' },
160
+ };
161
+
162
+ const zodSchema = jsonSchemaToZod(schema);
163
+
164
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
165
+ expect(zodSchema.parse({ name: 'John', age: 30, count: 5 })).toEqual({
166
+ name: 'John',
167
+ age: 30,
168
+ count: 5,
169
+ });
170
+ expect(() => zodSchema.parse({ name: 'John', extra: 'string' })).toThrow();
171
+ });
172
+
173
+ it('handles nested objects with different additionalProperties settings', () => {
174
+ const schema: JsonSchema = {
175
type: 'object',
176
properties: {
247
- type: { type: 'string' },
177
+ strictChild: {
178
+ type: 'object',
179
+ properties: {
180
+ name: { type: 'string' },
181
+ },
182
+ additionalProperties: false,
183
+ },
184
+ flexibleChild: {
185
+ type: 'object',
186
+ properties: {
187
+ age: { type: 'number' },
188
+ },
189
+ additionalProperties: true,
190
+ },
191
},
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();
192
+ additionalProperties: { type: 'string' },
193
+ };
194
+
195
+ const zodSchema = jsonSchemaToZod(schema);
196
+
197
+ const validData = {
198
+ strictChild: { name: 'John' },
199
+ flexibleChild: { age: 30, extra: 'allowed' },
200
+ extraString: 'this should be a string',
201
+ };
202
+ expect(zodSchema.parse(validData)).toEqual(validData);
203
+
204
+ expect(() =>
205
+ zodSchema.parse({
206
+ strictChild: { name: 'John', extra: 'not allowed' },
207
+ })
208
+ ).toThrow();
209
+
210
+ expect(() =>
211
+ zodSchema.parse({
212
+ extraNumber: 123,
213
+ })
214
+ ).toThrow();
215
+ });
216
+
217
+ it('handles complex nested arrays', () => {
218
+ const schema: JsonSchema = {
219
+ type: 'array',
220
+ items: {
221
+ type: 'object',
222
+ properties: {
223
+ id: { type: 'number' },
224
+ tags: {
225
+ type: 'array',
226
+ items: { type: 'string' },
227
+ },
228
+ },
229
+ required: ['id'],
230
+ },
231
+ };
232
+
233
+ const zodSchema = jsonSchemaToZod(schema);
234
+
235
+ const validData = [{ id: 1, tags: ['a', 'b'] }, { id: 2, tags: ['c'] }, { id: 3 }];
236
+ expect(zodSchema.parse(validData)).toEqual(validData);
237
+
238
+ expect(() =>
239
+ zodSchema.parse([{ tags: ['a', 'b'] }])
240
+ ).toThrow();
241
+ });
242
+
243
+ it('handles union types with anyOf', () => {
244
+ const schema: JsonSchema = {
245
+ anyOf: [
246
+ { type: 'string', minLength: 3 },
247
+ { type: 'number', minimum: 0 },
248
+ {
249
+ type: 'object',
250
+ properties: {
251
+ type: { type: 'string' },
252
+ },
253
+ required: ['type'],
254
+ },
255
+ ],
256
+ };
257
+
258
+ const zodSchema = jsonSchemaToZod(schema);
259
+
260
+ expect(zodSchema.parse('hello')).toBe('hello');
261
+ expect(zodSchema.parse(42)).toBe(42);
262
+ expect(zodSchema.parse({ type: 'custom' })).toEqual({ type: 'custom' });
263
+
264
+ expect(() => zodSchema.parse('ab')).toThrow();
265
+ expect(() => zodSchema.parse(-1)).toThrow();
266
+ expect(() => zodSchema.parse({ foo: 'bar' })).toThrow();
267
+ });
268
+ });
269
});
264
- });
270
+ }
271
});
272
+