Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

chore(ts/e2e): fix inconsistency in @e2e-tests/e2e-json-schema-to-zod-v*

jkomyno791c41f5183a
diff --git a/ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/e2e.test.ts b/ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/e2e.test.ts
index 84e59fb23..e21195a6b 100644
--- a/ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/e2e.test.ts
+++ b/ts/e2e-tests/runtimes/node/json-schema-to-zod-v3/e2e.test.ts
@@ -6,274 +6,281 @@
*/
import { jsonSchemaToZod, type JsonSchema } from '@composio/json-schema-to-zod';
+import { e2e } from '@e2e-tests/utils';
import { describe, it, expect } from 'bun:test';
import zodToJsonSchema from 'zod-to-json-schema';
-describe('json-schema-to-zod with Zod v3', () => {
- describe('Basic functionality', () => {
- it('converts basic string schema', () => {
- const schema: JsonSchema = { type: 'string' };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('hello')).toBe('hello');
- expect(() => zodSchema.parse(123)).toThrow();
- });
-
- it('converts object schema with validation', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- age: { type: 'number', minimum: 0 },
- },
- required: ['name'],
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(() => zodSchema.parse({ age: 30 })).toThrow();
- });
-
- it('converts array schema', () => {
- const schema: JsonSchema = {
- type: 'array',
- items: { type: 'string' },
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
- expect(() => zodSchema.parse(['one', 2])).toThrow();
- });
-
- it('validates email format', () => {
- const schema: JsonSchema = {
- type: 'string',
- format: 'email',
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('[email protected]')).toBe('[email protected]');
- expect(() => zodSchema.parse('invalid-email')).toThrow();
- });
-
- it('handles complex nested schemas', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- user: {
+e2e(import.meta.url, {
+ nodeVersions: ['current'],
+ defineTests: () => {
+ describe('json-schema-to-zod with Zod v3', () => {
+ describe('Basic functionality', () => {
+ it('converts basic string schema', () => {
+ const schema: JsonSchema = { type: 'string' };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('hello')).toBe('hello');
+ expect(() => zodSchema.parse(123)).toThrow();
+ });
+
+ it('converts object schema with validation', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
- contacts: {
- type: 'array',
- items: {
- type: 'object',
- properties: {
- type: { type: 'string' },
- value: { type: 'string' },
+ age: { type: 'number', minimum: 0 },
+ },
+ required: ['name'],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
+ });
+
+ it('converts array schema', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ items: { type: 'string' },
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
+ expect(() => zodSchema.parse(['one', 2])).toThrow();
+ });
+
+ it('validates email format', () => {
+ const schema: JsonSchema = {
+ type: 'string',
+ format: 'email',
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('[email protected]')).toBe('[email protected]');
+ expect(() => zodSchema.parse('invalid-email')).toThrow();
+ });
+
+ it('handles complex nested schemas', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ properties: {
+ user: {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
+ contacts: {
+ type: 'array',
+ items: {
+ type: 'object',
+ properties: {
+ type: { type: 'string' },
+ value: { type: 'string' },
+ },
+ required: ['type', 'value'],
+ },
},
- required: ['type', 'value'],
},
+ required: ['name'],
},
},
- required: ['name'],
- },
- },
- required: ['user'],
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- const validData = {
- user: {
- name: 'Jane Doe',
- contacts: [
- { type: 'email', value: '[email protected]' },
- { type: 'phone', value: '555-1234' },
- ],
- },
- };
- expect(zodSchema.parse(validData)).toEqual(validData);
- });
-
- it('handles anyOf schemas', () => {
- const schema: JsonSchema = {
- anyOf: [{ type: 'string' }, { type: 'number' }],
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('hello')).toBe('hello');
- expect(zodSchema.parse(42)).toBe(42);
- expect(() => zodSchema.parse(true)).toThrow();
- });
- });
-
- describe('Round-trip conversion', () => {
- it('preserves additionalProperties: true for empty objects', () => {
- const schema: JsonSchema = {
- type: 'object',
- additionalProperties: true,
- };
-
- const zodSchema = jsonSchemaToZod(schema);
- // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
- expect(convertedBack.additionalProperties).toBe(true);
- // @ts-expect-error Property 'type' does not exist on type
- expect(convertedBack.type).toBe('object');
-
- expect(zodSchema.parse({})).toEqual({});
- expect(zodSchema.parse({ any: 'value', number: 123 })).toEqual({ any: 'value', number: 123 });
- });
-
- it('preserves additionalProperties: true for objects with properties', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- age: { type: 'number' },
- },
- required: ['name'],
- additionalProperties: true,
- };
-
- const zodSchema = jsonSchemaToZod(schema);
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
-
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
- expect(convertedBack.additionalProperties).toBe(true);
- // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
- expect(convertedBack.properties).toBeDefined();
- // @ts-expect-error Property 'required' does not exist on type
- expect(convertedBack.required).toEqual(['name']);
-
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
- name: 'John',
- age: 30,
- extra: 'field',
+ required: ['user'],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ const validData = {
+ user: {
+ name: 'Jane Doe',
+ contacts: [
+ { type: 'email', value: '[email protected]' },
+ { type: 'phone', value: '555-1234' },
+ ],
+ },
+ };
+ expect(zodSchema.parse(validData)).toEqual(validData);
+ });
+
+ it('handles anyOf schemas', () => {
+ const schema: JsonSchema = {
+ anyOf: [{ type: 'string' }, { type: 'number' }],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('hello')).toBe('hello');
+ expect(zodSchema.parse(42)).toBe(42);
+ expect(() => zodSchema.parse(true)).toThrow();
+ });
});
- });
-
- it('preserves additionalProperties: false for empty objects', () => {
- const schema: JsonSchema = {
- type: 'object',
- additionalProperties: false,
- };
-
- const zodSchema = jsonSchemaToZod(schema);
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
-
- // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
- // which is semantically equivalent to additionalProperties: false
- const additionalPropsValid =
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
- convertedBack.additionalProperties === false ||
- // @ts-expect-error Property 'not' does not exist on type.
- (convertedBack.not && typeof convertedBack.not === 'object');
- expect(additionalPropsValid).toBe(true);
- // @ts-expect-error Property 'type' does not exist on type.
- expect(convertedBack.type).toBe('object');
-
- expect(zodSchema.parse({})).toEqual({});
- expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
- });
-
- it('preserves additionalProperties: false for objects with properties', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- },
- additionalProperties: false,
- };
-
- const zodSchema = jsonSchemaToZod(schema);
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
-
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
- expect(convertedBack.additionalProperties).toBe(false);
- // @ts-expect-error Property 'properties' does not exist on type.
- expect(convertedBack.properties).toBeDefined();
-
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
- });
-
- it('handles additionalProperties with type schema', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- },
- additionalProperties: { type: 'number' },
- };
-
- const zodSchema = jsonSchemaToZod(schema);
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
-
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
- expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
- // @ts-expect-error Property 'properties' does not exist on type.
- expect(convertedBack.properties).toBeDefined();
-
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
- });
-
- it('handles nested objects with different additionalProperties settings', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- strictChild: {
+
+ describe('Round-trip conversion', () => {
+ it('preserves additionalProperties: true for empty objects', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ additionalProperties: true,
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+ // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
+ expect(convertedBack.additionalProperties).toBe(true);
+ // @ts-expect-error Property 'type' does not exist on type
+ expect(convertedBack.type).toBe('object');
+
+ expect(zodSchema.parse({})).toEqual({});
+ expect(zodSchema.parse({ any: 'value', number: 123 })).toEqual({ any: 'value', number: 123 });
+ });
+
+ it('preserves additionalProperties: true for objects with properties', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
+ age: { type: 'number' },
},
+ required: ['name'],
+ additionalProperties: true,
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
+
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
+ expect(convertedBack.additionalProperties).toBe(true);
+ // @ts-expect-error Type instantiation is excessively deep and possibly infinite.
+ expect(convertedBack.properties).toBeDefined();
+ // @ts-expect-error Property 'required' does not exist on type
+ expect(convertedBack.required).toEqual(['name']);
+
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(zodSchema.parse({ name: 'John', age: 30, extra: 'field' })).toEqual({
+ name: 'John',
+ age: 30,
+ extra: 'field',
+ });
+ });
+
+ it('preserves additionalProperties: false for empty objects', () => {
+ const schema: JsonSchema = {
+ type: 'object',
additionalProperties: false,
- },
- flexibleChild: {
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
+
+ // zod-to-json-schema may convert z.object({}).strict() to { not: {} }
+ // which is semantically equivalent to additionalProperties: false
+ const additionalPropsValid =
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
+ convertedBack.additionalProperties === false ||
+ // @ts-expect-error Property 'not' does not exist on type.
+ (convertedBack.not && typeof convertedBack.not === 'object');
+ expect(additionalPropsValid).toBe(true);
+ // @ts-expect-error Property 'type' does not exist on type.
+ expect(convertedBack.type).toBe('object');
+
+ expect(zodSchema.parse({})).toEqual({});
+ expect(() => zodSchema.parse({ extra: 'field' })).toThrow();
+ });
+
+ it('preserves additionalProperties: false for objects with properties', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
- age: { type: 'number' },
+ name: { type: 'string' },
},
- additionalProperties: true,
- },
- },
- additionalProperties: { type: 'string' },
- };
-
- const zodSchema = jsonSchemaToZod(schema);
- const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
-
- // @ts-expect-error Property 'additionalProperties' does not exist on type.
- expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
- // @ts-expect-error Property 'properties' does not exist on type.
- expect((convertedBack.properties as Record<string, unknown>)?.strictChild).toBeDefined();
- // @ts-expect-error Property 'properties' does not exist on type.
- expect((convertedBack.properties as Record<string, unknown>)?.flexibleChild).toBeDefined();
-
- const validData = {
- strictChild: { name: 'John' },
- flexibleChild: { age: 30, extra: 'allowed' },
- extraString: 'this should be a string',
- };
- expect(zodSchema.parse(validData)).toEqual(validData);
-
- // Test invalid cases
- expect(() =>
- zodSchema.parse({
- strictChild: { name: 'John', extra: 'not allowed' },
- })
- ).toThrow();
-
- expect(() =>
- zodSchema.parse({
- extraNumber: 123, // should be string according to additionalProperties
- })
- ).toThrow();
+ additionalProperties: false,
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
+
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
+ expect(convertedBack.additionalProperties).toBe(false);
+ // @ts-expect-error Property 'properties' does not exist on type.
+ expect(convertedBack.properties).toBeDefined();
+
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
+ });
+
+ it('handles additionalProperties with type schema', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
+ },
+ additionalProperties: { type: 'number' },
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
+
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
+ expect(convertedBack.additionalProperties).toEqual({ type: 'number' });
+ // @ts-expect-error Property 'properties' does not exist on type.
+ expect(convertedBack.properties).toBeDefined();
+
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
+ });
+
+ it('handles nested objects with different additionalProperties settings', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ properties: {
+ strictChild: {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
+ },
+ additionalProperties: false,
+ },
+ flexibleChild: {
+ type: 'object',
+ properties: {
+ age: { type: 'number' },
+ },
+ additionalProperties: true,
+ },
+ },
+ additionalProperties: { type: 'string' },
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+ const convertedBack = zodToJsonSchema(zodSchema, { target: 'jsonSchema7' });
+
+ // @ts-expect-error Property 'additionalProperties' does not exist on type.
+ expect(convertedBack.additionalProperties).toEqual({ type: 'string' });
+ // @ts-expect-error Property 'properties' does not exist on type.
+ expect((convertedBack.properties as Record<string, unknown>)?.strictChild).toBeDefined();
+ // @ts-expect-error Property 'properties' does not exist on type.
+ expect((convertedBack.properties as Record<string, unknown>)?.flexibleChild).toBeDefined();
+
+ const validData = {
+ strictChild: { name: 'John' },
+ flexibleChild: { age: 30, extra: 'allowed' },
+ extraString: 'this should be a string',
+ };
+ expect(zodSchema.parse(validData)).toEqual(validData);
+
+ // Test invalid cases
+ expect(() =>
+ zodSchema.parse({
+ strictChild: { name: 'John', extra: 'not allowed' },
+ })
+ ).toThrow();
+
+ expect(() =>
+ zodSchema.parse({
+ extraNumber: 123, // should be string according to additionalProperties
+ })
+ ).toThrow();
+ });
+ });
});
- });
+ },
});
+
diff --git a/ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/e2e.test.ts b/ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/e2e.test.ts
index e97886506..3d081b8dd 100644
--- a/ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/e2e.test.ts
+++ b/ts/e2e-tests/runtimes/node/json-schema-to-zod-v4/e2e.test.ts
@@ -6,260 +6,267 @@
*/
import { jsonSchemaToZod, type JsonSchema } from '@composio/json-schema-to-zod';
+import { e2e } from '@e2e-tests/utils';
import { describe, it, expect } from 'bun:test';
-describe('json-schema-to-zod with Zod v4', () => {
- describe('Basic functionality', () => {
- it('converts basic string schema', () => {
- const schema: JsonSchema = { type: 'string' };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('hello')).toBe('hello');
- expect(() => zodSchema.parse(123)).toThrow();
- });
-
- it('converts object schema with validation', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- age: { type: 'number', minimum: 0 },
- },
- required: ['name'],
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(() => zodSchema.parse({ age: 30 })).toThrow();
- });
-
- it('converts array schema', () => {
- const schema: JsonSchema = {
- type: 'array',
- items: { type: 'string' },
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
- expect(() => zodSchema.parse(['one', 2])).toThrow();
- });
-
- it('validates email format', () => {
- const schema: JsonSchema = {
- type: 'string',
- format: 'email',
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('[email protected]')).toBe('[email protected]');
- expect(() => zodSchema.parse('invalid-email')).toThrow();
- });
-
- it('handles complex nested schemas', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- user: {
+e2e(import.meta.url, {
+ nodeVersions: ['current'],
+ defineTests: () => {
+ describe('json-schema-to-zod with Zod v4', () => {
+ describe('Basic functionality', () => {
+ it('converts basic string schema', () => {
+ const schema: JsonSchema = { type: 'string' };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('hello')).toBe('hello');
+ expect(() => zodSchema.parse(123)).toThrow();
+ });
+
+ it('converts object schema with validation', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
- contacts: {
- type: 'array',
- items: {
- type: 'object',
- properties: {
- type: { type: 'string' },
- value: { type: 'string' },
+ age: { type: 'number', minimum: 0 },
+ },
+ required: ['name'],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
+ });
+
+ it('converts array schema', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ items: { type: 'string' },
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
+ expect(() => zodSchema.parse(['one', 2])).toThrow();
+ });
+
+ it('validates email format', () => {
+ const schema: JsonSchema = {
+ type: 'string',
+ format: 'email',
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('[email protected]')).toBe('[email protected]');
+ expect(() => zodSchema.parse('invalid-email')).toThrow();
+ });
+
+ it('handles complex nested schemas', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ properties: {
+ user: {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
+ contacts: {
+ type: 'array',
+ items: {
+ type: 'object',
+ properties: {
+ type: { type: 'string' },
+ value: { type: 'string' },
+ },
+ required: ['type', 'value'],
+ },
},
- required: ['type', 'value'],
},
+ required: ['name'],
},
},
- required: ['name'],
- },
- },
- required: ['user'],
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- const validData = {
- user: {
- name: 'Jane Doe',
- contacts: [
- { type: 'email', value: '[email protected]' },
- { type: 'phone', value: '555-1234' },
- ],
- },
- };
- expect(zodSchema.parse(validData)).toEqual(validData);
- });
-
- it('handles anyOf schemas', () => {
- const schema: JsonSchema = {
- anyOf: [{ type: 'string' }, { type: 'number' }],
- };
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('hello')).toBe('hello');
- expect(zodSchema.parse(42)).toBe(42);
- expect(() => zodSchema.parse(true)).toThrow();
- });
- });
-
- describe('Parsing behavior', () => {
- it('converts basic schemas and verifies parsing behavior', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- age: { type: 'number' },
- },
- required: ['name'],
- additionalProperties: false,
- };
-
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
- expect(() => zodSchema.parse({ age: 30 })).toThrow();
- expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
- });
-
- it('handles additionalProperties: true', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- },
- additionalProperties: true,
- };
-
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(zodSchema.parse({ name: 'John', extra: 'allowed', another: 123 })).toEqual({
- name: 'John',
- extra: 'allowed',
- another: 123,
- });
- });
-
- it('handles additionalProperties with type constraint', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- name: { type: 'string' },
- },
- additionalProperties: { type: 'number' },
- };
-
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
- expect(zodSchema.parse({ name: 'John', age: 30, count: 5 })).toEqual({
- name: 'John',
- age: 30,
- count: 5,
+ required: ['user'],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ const validData = {
+ user: {
+ name: 'Jane Doe',
+ contacts: [
+ { type: 'email', value: '[email protected]' },
+ { type: 'phone', value: '555-1234' },
+ ],
+ },
+ };
+ expect(zodSchema.parse(validData)).toEqual(validData);
+ });
+
+ it('handles anyOf schemas', () => {
+ const schema: JsonSchema = {
+ anyOf: [{ type: 'string' }, { type: 'number' }],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('hello')).toBe('hello');
+ expect(zodSchema.parse(42)).toBe(42);
+ expect(() => zodSchema.parse(true)).toThrow();
+ });
});
- expect(() => zodSchema.parse({ name: 'John', extra: 'string' })).toThrow();
- });
-
- it('handles nested objects with different additionalProperties settings', () => {
- const schema: JsonSchema = {
- type: 'object',
- properties: {
- strictChild: {
+
+ describe('Parsing behavior', () => {
+ it('converts basic schemas and verifies parsing behavior', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
+ age: { type: 'number' },
},
+ required: ['name'],
additionalProperties: false,
- },
- flexibleChild: {
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
+ expect(() => zodSchema.parse({ age: 30 })).toThrow();
+ expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
+ });
+
+ it('handles additionalProperties: true', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
- age: { type: 'number' },
+ name: { type: 'string' },
},
additionalProperties: true,
- },
- },
- additionalProperties: { type: 'string' },
- };
-
- const zodSchema = jsonSchemaToZod(schema);
-
- const validData = {
- strictChild: { name: 'John' },
- flexibleChild: { age: 30, extra: 'allowed' },
- extraString: 'this should be a string',
- };
- expect(zodSchema.parse(validData)).toEqual(validData);
-
- expect(() =>
- zodSchema.parse({
- strictChild: { name: 'John', extra: 'not allowed' },
- })
- ).toThrow();
-
- expect(() =>
- zodSchema.parse({
- extraNumber: 123,
- })
- ).toThrow();
- });
-
- it('handles complex nested arrays', () => {
- const schema: JsonSchema = {
- type: 'array',
- items: {
- type: 'object',
- properties: {
- id: { type: 'number' },
- tags: {
- type: 'array',
- items: { type: 'string' },
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(zodSchema.parse({ name: 'John', extra: 'allowed', another: 123 })).toEqual({
+ name: 'John',
+ extra: 'allowed',
+ another: 123,
+ });
+ });
+
+ it('handles additionalProperties with type constraint', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
},
- },
- required: ['id'],
- },
- };
-
- const zodSchema = jsonSchemaToZod(schema);
-
- const validData = [{ id: 1, tags: ['a', 'b'] }, { id: 2, tags: ['c'] }, { id: 3 }];
- expect(zodSchema.parse(validData)).toEqual(validData);
-
- expect(() =>
- zodSchema.parse([{ tags: ['a', 'b'] }])
- ).toThrow();
- });
-
- it('handles union types with anyOf', () => {
- const schema: JsonSchema = {
- anyOf: [
- { type: 'string', minLength: 3 },
- { type: 'number', minimum: 0 },
- {
+ additionalProperties: { type: 'number' },
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
+ expect(zodSchema.parse({ name: 'John', age: 30, count: 5 })).toEqual({
+ name: 'John',
+ age: 30,
+ count: 5,
+ });
+ expect(() => zodSchema.parse({ name: 'John', extra: 'string' })).toThrow();
+ });
+
+ it('handles nested objects with different additionalProperties settings', () => {
+ const schema: JsonSchema = {
type: 'object',
properties: {
- type: { type: 'string' },
+ strictChild: {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
+ },
+ additionalProperties: false,
+ },
+ flexibleChild: {
+ type: 'object',
+ properties: {
+ age: { type: 'number' },
+ },
+ additionalProperties: true,
+ },
},
- required: ['type'],
- },
- ],
- };
-
- const zodSchema = jsonSchemaToZod(schema);
-
- expect(zodSchema.parse('hello')).toBe('hello');
- expect(zodSchema.parse(42)).toBe(42);
- expect(zodSchema.parse({ type: 'custom' })).toEqual({ type: 'custom' });
-
- expect(() => zodSchema.parse('ab')).toThrow();
- expect(() => zodSchema.parse(-1)).toThrow();
- expect(() => zodSchema.parse({ foo: 'bar' })).toThrow();
+ additionalProperties: { type: 'string' },
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ const validData = {
+ strictChild: { name: 'John' },
+ flexibleChild: { age: 30, extra: 'allowed' },
+ extraString: 'this should be a string',
+ };
+ expect(zodSchema.parse(validData)).toEqual(validData);
+
+ expect(() =>
+ zodSchema.parse({
+ strictChild: { name: 'John', extra: 'not allowed' },
+ })
+ ).toThrow();
+
+ expect(() =>
+ zodSchema.parse({
+ extraNumber: 123,
+ })
+ ).toThrow();
+ });
+
+ it('handles complex nested arrays', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ items: {
+ type: 'object',
+ properties: {
+ id: { type: 'number' },
+ tags: {
+ type: 'array',
+ items: { type: 'string' },
+ },
+ },
+ required: ['id'],
+ },
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ const validData = [{ id: 1, tags: ['a', 'b'] }, { id: 2, tags: ['c'] }, { id: 3 }];
+ expect(zodSchema.parse(validData)).toEqual(validData);
+
+ expect(() =>
+ zodSchema.parse([{ tags: ['a', 'b'] }])
+ ).toThrow();
+ });
+
+ it('handles union types with anyOf', () => {
+ const schema: JsonSchema = {
+ anyOf: [
+ { type: 'string', minLength: 3 },
+ { type: 'number', minimum: 0 },
+ {
+ type: 'object',
+ properties: {
+ type: { type: 'string' },
+ },
+ required: ['type'],
+ },
+ ],
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(zodSchema.parse('hello')).toBe('hello');
+ expect(zodSchema.parse(42)).toBe(42);
+ expect(zodSchema.parse({ type: 'custom' })).toEqual({ type: 'custom' });
+
+ expect(() => zodSchema.parse('ab')).toThrow();
+ expect(() => zodSchema.parse(-1)).toThrow();
+ expect(() => zodSchema.parse({ foo: 'bar' })).toThrow();
+ });
+ });
});
- });
+ }
});
+