@devries-cameron20 / composio Mirror · read-only / commits / 4ddf75403
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 22, 19:30 UTC

Fix array parsing in JSON Schema to zod conversion (#2068)

Musthaq Ahamad committed Oct 17, 2025 at 15:30 UTC 4ddf75403fea6f4cb37dcaaf7c117a2caee48530
4 files changed +404 -12
ts/examples/tools/src/tool-conversion.ts
+3 -1
@@ -5,7 +5,9 @@ const composio = new Composio({
5 apiKey: process.env.COMPOSIO_API_KEY,
6 });
7
8 -const tool = await composio.tools.getRawComposioToolBySlug('HUBSPOT_CREATE_LINE_ITEMS');
8 +const tool = await composio.tools.getRawComposioToolBySlug(
9 + 'GOOGLESHEETS_SPREADSHEETS_VALUES_APPEND'
10 +);
11 const inputParams = tool.inputParameters;
12 console.log(`---------- Original Input Parameters ----------`);
13 console.log(JSON.stringify(inputParams, null, 2));
ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
+14 -10
@@ -7,7 +7,7 @@ import { parseSchema } from './parse-schema';
7 import type { JsonSchemaObject, Refs, JsonSchema } from '../types';
8 import { its } from '../utils/its';
9
10 -function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object' }, refs: Refs) {
10 +function parseObjectProperties(objectSchema: JsonSchemaObject & { type?: 'object' }, refs: Refs) {
11 if (!objectSchema.properties) {
12 // leave it as an empty object or else this will break openai responses
13 return z.object({});
@@ -79,30 +79,34 @@ function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object'
79 }
80
81 export function parseObject(
82 - objectSchema: JsonSchemaObject & { type: 'object' },
82 + objectSchema: JsonSchemaObject & { type?: 'object' },
83 refs: Refs
84 ): z.ZodTypeAny {
85 const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
86
87 + // Ensure type is set to 'object' if not already
88 + const normalizedSchema =
89 + objectSchema.type === 'object' ? objectSchema : { ...objectSchema, type: 'object' as const };
90 +
91 const propertiesSchema:
92 | z.ZodObject<Record<string, z.ZodTypeAny>, 'strip', z.ZodTypeAny>
89 - | undefined = parseObjectProperties(objectSchema, refs);
93 + | undefined = parseObjectProperties(normalizedSchema, refs);
94 let zodSchema: z.ZodTypeAny | undefined = propertiesSchema;
95
96 const additionalProperties =
93 - objectSchema.additionalProperties !== undefined
94 - ? parseSchema(objectSchema.additionalProperties, {
97 + normalizedSchema.additionalProperties !== undefined
98 + ? parseSchema(normalizedSchema.additionalProperties, {
99 ...refs,
100 path: [...refs.path, 'additionalProperties'],
101 })
102 : undefined;
103
104 // Track if additionalProperties was explicitly set to true
101 - const isAdditionalPropertiesTrue = objectSchema.additionalProperties === true;
105 + const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
106
103 - if (objectSchema.patternProperties) {
107 + if (normalizedSchema.patternProperties) {
108 const parsedPatternProperties = Object.fromEntries(
105 - Object.entries(objectSchema.patternProperties).map(([key, value]) => {
109 + Object.entries(normalizedSchema.patternProperties).map(([key, value]) => {
110 return [
111 key,
112 parseSchema(value, {
@@ -138,12 +142,12 @@ export function parseObject(
142 }
143 }
144
141 - const objectPropertyKeys = new Set(Object.keys(objectSchema.properties ?? {}));
145 + const objectPropertyKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
146 zodSchema = zodSchema.superRefine((value: Record<string, unknown>, ctx) => {
147 for (const key in value) {
148 let wasMatched = objectPropertyKeys.has(key);
149
146 - for (const patternPropertyKey in objectSchema.patternProperties) {
150 + for (const patternPropertyKey in normalizedSchema.patternProperties) {
151 const regex = new RegExp(patternPropertyKey);
152 if (key.match(regex)) {
153 wasMatched = true;
ts/packages/json-schema-to-zod/src/utils/its.ts
+5 -1
@@ -3,7 +3,11 @@ import type { JsonSchema, JsonSchemaObject, Serializable } from '../types';
3 export const its = {
4 an: {
5 object: (x: JsonSchemaObject): x is JsonSchemaObject & { type: 'object' } =>
6 - x.type === 'object',
6 + x.type === 'object' ||
7 + (!x.type &&
8 + (x.properties !== undefined ||
9 + x.additionalProperties !== undefined ||
10 + x.patternProperties !== undefined)),
11 array: (x: JsonSchemaObject): x is JsonSchemaObject & { type: 'array' } => x.type === 'array',
12 anyOf: (
13 x: JsonSchemaObject
ts/packages/json-schema-to-zod/test/json-to-zod.test.ts
+382
@@ -1386,4 +1386,386 @@ describe('jsonSchemaToZod', () => {
1386 ).toThrow();
1387 });
1388 });
1389 +
1390 + describe('Object Detection Without Explicit Type', () => {
1391 + it('should detect object schema with only properties defined', () => {
1392 + const schema: JsonSchema = {
1393 + properties: {
1394 + name: { type: 'string' },
1395 + age: { type: 'number' },
1396 + },
1397 + required: ['name'],
1398 + };
1399 + const zodSchema = jsonSchemaToZod(schema);
1400 +
1401 + // Should parse as an object
1402 + expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1403 + expect(zodSchema.parse({ name: 'Jane' })).toEqual({ name: 'Jane' });
1404 + expect(() => zodSchema.parse({ age: 30 })).toThrow(); // Missing required field
1405 + expect(() => zodSchema.parse('not an object')).toThrow();
1406 + });
1407 +
1408 + it('should detect object schema with only additionalProperties defined', () => {
1409 + const schema: JsonSchema = {
1410 + additionalProperties: { type: 'string' },
1411 + };
1412 + const zodSchema = jsonSchemaToZod(schema);
1413 +
1414 + // Should parse as an object with string additional properties
1415 + expect(zodSchema.parse({})).toEqual({});
1416 + expect(zodSchema.parse({ key1: 'value1', key2: 'value2' })).toEqual({
1417 + key1: 'value1',
1418 + key2: 'value2',
1419 + });
1420 + expect(() => zodSchema.parse({ key1: 123 })).toThrow(); // Wrong type
1421 + expect(() => zodSchema.parse('not an object')).toThrow();
1422 + });
1423 +
1424 + it('should detect object schema with only patternProperties defined', () => {
1425 + const schema: JsonSchema = {
1426 + patternProperties: {
1427 + '^prefix_': { type: 'string' },
1428 + '^num_': { type: 'number' },
1429 + },
1430 + };
1431 + const zodSchema = jsonSchemaToZod(schema);
1432 +
1433 + // Should parse as an object
1434 + expect(zodSchema.parse({})).toEqual({});
1435 + expect(zodSchema.parse({ prefix_one: 'value', num_one: 123 })).toEqual({
1436 + prefix_one: 'value',
1437 + num_one: 123,
1438 + });
1439 + expect(() => zodSchema.parse({ prefix_one: 123 })).toThrow(); // Wrong type for pattern
1440 + expect(() => zodSchema.parse('not an object')).toThrow();
1441 + });
1442 +
1443 + it('should detect object schema with properties and additionalProperties but no type', () => {
1444 + const schema: JsonSchema = {
1445 + properties: {
1446 + name: { type: 'string' },
1447 + },
1448 + additionalProperties: { type: 'number' },
1449 + };
1450 + const zodSchema = jsonSchemaToZod(schema);
1451 +
1452 + expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
1453 + expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1454 + expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
1455 + });
1456 +
1457 + it('should detect object schema with properties and patternProperties but no type', () => {
1458 + const schema: JsonSchema = {
1459 + properties: {
1460 + name: { type: 'string' },
1461 + },
1462 + patternProperties: {
1463 + '^data_': { type: 'number' },
1464 + },
1465 + additionalProperties: false,
1466 + };
1467 + const zodSchema = jsonSchemaToZod(schema);
1468 +
1469 + expect(zodSchema.parse({ name: 'John', data_age: 30 })).toEqual({
1470 + name: 'John',
1471 + data_age: 30,
1472 + });
1473 + expect(() => zodSchema.parse({ name: 'John', data_age: 'not a number' })).toThrow();
1474 + expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
1475 + });
1476 +
1477 + it('should detect nested objects without explicit type', () => {
1478 + const schema: JsonSchema = {
1479 + type: 'object',
1480 + properties: {
1481 + user: {
1482 + // No type specified, but has properties
1483 + properties: {
1484 + name: { type: 'string' },
1485 + email: { type: 'string', format: 'email' },
1486 + },
1487 + required: ['name'],
1488 + },
1489 + metadata: {
1490 + // No type specified, but has additionalProperties
1491 + additionalProperties: { type: 'string' },
1492 + },
1493 + },
1494 + };
1495 + const zodSchema = jsonSchemaToZod(schema);
1496 +
1497 + const validData = {
1498 + user: { name: 'John', email: 'john@example.com' },
1499 + metadata: { key1: 'value1', key2: 'value2' },
1500 + };
1501 + expect(zodSchema.parse(validData)).toEqual(validData);
1502 +
1503 + // Test nested validation
1504 + expect(() =>
1505 + zodSchema.parse({
1506 + user: { email: 'john@example.com' }, // Missing required name
1507 + })
1508 + ).toThrow();
1509 +
1510 + expect(() =>
1511 + zodSchema.parse({
1512 + metadata: { key1: 123 }, // Wrong type
1513 + })
1514 + ).toThrow();
1515 + });
1516 +
1517 + it('should handle arrays containing objects without explicit type', () => {
1518 + const schema: JsonSchema = {
1519 + type: 'array',
1520 + items: {
1521 + // No type specified, but has properties
1522 + properties: {
1523 + id: { type: 'number' },
1524 + name: { type: 'string' },
1525 + },
1526 + required: ['id'],
1527 + },
1528 + };
1529 + const zodSchema = jsonSchemaToZod(schema);
1530 +
1531 + expect(zodSchema.parse([{ id: 1, name: 'Item 1' }, { id: 2 }])).toEqual([
1532 + { id: 1, name: 'Item 1' },
1533 + { id: 2 },
1534 + ]);
1535 +
1536 + expect(() => zodSchema.parse([{ name: 'Item without ID' }])).toThrow();
1537 + });
1538 +
1539 + it('should handle complex nested structure without explicit types', () => {
1540 + const schema: JsonSchema = {
1541 + type: 'object',
1542 + properties: {
1543 + config: {
1544 + // No type, but has properties
1545 + properties: {
1546 + settings: {
1547 + // No type, but has additionalProperties
1548 + additionalProperties: {
1549 + // No type, but has properties
1550 + properties: {
1551 + enabled: { type: 'boolean' },
1552 + value: { type: 'string' },
1553 + },
1554 + },
1555 + },
1556 + tags: {
1557 + type: 'array',
1558 + items: {
1559 + // No type, but has properties
1560 + properties: {
1561 + key: { type: 'string' },
1562 + value: { type: 'string' },
1563 + },
1564 + required: ['key'],
1565 + },
1566 + },
1567 + },
1568 + },
1569 + },
1570 + };
1571 + const zodSchema = jsonSchemaToZod(schema);
1572 +
1573 + const validData = {
1574 + config: {
1575 + settings: {
1576 + feature1: { enabled: true, value: 'on' },
1577 + feature2: { enabled: false, value: 'off' },
1578 + },
1579 + tags: [{ key: 'env', value: 'production' }, { key: 'region' }],
1580 + },
1581 + };
1582 + expect(zodSchema.parse(validData)).toEqual(validData);
1583 + });
1584 +
1585 + it('should handle anyOf with objects without explicit type', () => {
1586 + const schema: JsonSchema = {
1587 + anyOf: [
1588 + {
1589 + // No type, but has properties
1590 + properties: {
1591 + name: { type: 'string' },
1592 + },
1593 + },
1594 + {
1595 + type: 'string',
1596 + },
1597 + ],
1598 + };
1599 + const zodSchema = jsonSchemaToZod(schema);
1600 +
1601 + expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
1602 + expect(zodSchema.parse('just a string')).toBe('just a string');
1603 + });
1604 +
1605 + it('should handle oneOf with objects without explicit type', () => {
1606 + const schema: JsonSchema = {
1607 + oneOf: [
1608 + {
1609 + // No type, but has properties
1610 + properties: {
1611 + type: { type: 'string', enum: ['user'] },
1612 + name: { type: 'string' },
1613 + },
1614 + required: ['type'],
1615 + },
1616 + {
1617 + // No type, but has properties
1618 + properties: {
1619 + type: { type: 'string', enum: ['admin'] },
1620 + role: { type: 'string' },
1621 + },
1622 + required: ['type'],
1623 + },
1624 + ],
1625 + };
1626 + const zodSchema = jsonSchemaToZod(schema);
1627 +
1628 + expect(zodSchema.parse({ type: 'user', name: 'John' })).toEqual({
1629 + type: 'user',
1630 + name: 'John',
1631 + });
1632 + expect(zodSchema.parse({ type: 'admin', role: 'superadmin' })).toEqual({
1633 + type: 'admin',
1634 + role: 'superadmin',
1635 + });
1636 + });
1637 +
1638 + it('should handle allOf with explicit object types (requires additionalProperties for merging)', () => {
1639 + // Note: allOf with strict objects (without additionalProperties) creates strict intersections
1640 + // Each object must allow additional properties for the intersection to accept merged fields
1641 + const schema: JsonSchema = {
1642 + allOf: [
1643 + {
1644 + type: 'object',
1645 + properties: {
1646 + name: { type: 'string' },
1647 + },
1648 + required: ['name'],
1649 + additionalProperties: true, // Required for allOf to work with merged fields
1650 + },
1651 + {
1652 + type: 'object',
1653 + properties: {
1654 + age: { type: 'number' },
1655 + },
1656 + additionalProperties: true, // Required for allOf to work with merged fields
1657 + },
1658 + ],
1659 + };
1660 + const zodSchema = jsonSchemaToZod(schema);
1661 +
1662 + expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1663 + expect(zodSchema.parse({ name: 'Jane' })).toEqual({ name: 'Jane' });
1664 + expect(() => zodSchema.parse({ age: 30 })).toThrow(); // Missing required name
1665 + });
1666 +
1667 + it('should handle allOf with implicit object detection (note: creates strict intersections)', () => {
1668 + // When objects in allOf don't have explicit types, they are detected as objects
1669 + // but the intersection behavior is strict
1670 + const schema: JsonSchema = {
1671 + allOf: [
1672 + {
1673 + // No type, but has properties - will be detected as object
1674 + properties: {
1675 + name: { type: 'string' },
1676 + },
1677 + required: ['name'],
1678 + additionalProperties: true, // Need to allow additional properties for intersection to work
1679 + },
1680 + {
1681 + // No type, but has properties - will be detected as object
1682 + properties: {
1683 + age: { type: 'number' },
1684 + },
1685 + additionalProperties: true, // Need to allow additional properties for intersection to work
1686 + },
1687 + ],
1688 + };
1689 + const zodSchema = jsonSchemaToZod(schema);
1690 +
1691 + // With additionalProperties: true, the intersection allows all properties
1692 + expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1693 + expect(zodSchema.parse({ name: 'Jane' })).toEqual({ name: 'Jane' });
1694 + expect(() => zodSchema.parse({ age: 30 })).toThrow(); // Missing required name
1695 + });
1696 +
1697 + it('should correctly identify objects in its.an.object utility', () => {
1698 + const schema: JsonSchema = {
1699 + type: 'object',
1700 + properties: {
1701 + explicit: {
1702 + type: 'object',
1703 + properties: { a: { type: 'string' } },
1704 + },
1705 + implicitFromProperties: {
1706 + properties: { b: { type: 'number' } },
1707 + },
1708 + implicitFromAdditionalProperties: {
1709 + additionalProperties: { type: 'boolean' },
1710 + },
1711 + implicitFromPatternProperties: {
1712 + patternProperties: {
1713 + '^[a-z]+$': { type: 'string' },
1714 + },
1715 + },
1716 + notAnObject: {
1717 + type: 'string',
1718 + },
1719 + },
1720 + };
1721 + const zodSchema = jsonSchemaToZod(schema);
1722 +
1723 + const validData = {
1724 + explicit: { a: 'test' },
1725 + implicitFromProperties: { b: 123 },
1726 + implicitFromAdditionalProperties: { any: true, keys: false },
1727 + implicitFromPatternProperties: { abc: 'value' },
1728 + notAnObject: 'just a string',
1729 + };
1730 + expect(zodSchema.parse(validData)).toEqual(validData);
1731 +
1732 + // Test each property type validation
1733 + expect(() => zodSchema.parse({ ...validData, explicit: 'not an object' })).toThrow();
1734 + expect(() =>
1735 + zodSchema.parse({ ...validData, implicitFromProperties: { b: 'wrong' } })
1736 + ).toThrow();
1737 + expect(() =>
1738 + zodSchema.parse({ ...validData, implicitFromAdditionalProperties: { key: 'wrong' } })
1739 + ).toThrow();
1740 + expect(() =>
1741 + zodSchema.parse({ ...validData, implicitFromPatternProperties: { abc: 123 } })
1742 + ).toThrow();
1743 + });
1744 +
1745 + it('should handle empty object schemas without explicit type', () => {
1746 + const schema: JsonSchema = {
1747 + properties: {},
1748 + };
1749 + const zodSchema = jsonSchemaToZod(schema);
1750 +
1751 + // Should be treated as an object
1752 + expect(zodSchema.parse({})).toEqual({});
1753 + expect(() => zodSchema.parse('not an object')).toThrow();
1754 + });
1755 +
1756 + it('should handle objects with only required field but no type', () => {
1757 + const schema: JsonSchema = {
1758 + properties: {
1759 + name: { type: 'string' },
1760 + age: { type: 'number' },
1761 + },
1762 + required: ['name', 'age'],
1763 + };
1764 + const zodSchema = jsonSchemaToZod(schema);
1765 +
1766 + expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1767 + expect(() => zodSchema.parse({ name: 'John' })).toThrow();
1768 + expect(() => zodSchema.parse({ age: 30 })).toThrow();
1769 + });
1770 + });
1771 });