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.

fix: log transform validation failures as warnings with field details

Ratnaditya-Jac4ef5b616d9
diff --git a/ts/packages/core/src/utils/transform.ts b/ts/packages/core/src/utils/transform.ts
index 9f0d2020a..60a78ebb4 100644
--- a/ts/packages/core/src/utils/transform.ts
+++ b/ts/packages/core/src/utils/transform.ts
@@ -12,7 +12,6 @@
* ```
*/
import { ZodTypeAny, z } from 'zod/v3';
-import { ValidationError } from '../errors';
import { logger } from '..';
export function transform<RawInput>(raw: RawInput) {
@@ -27,14 +26,11 @@ export function transform<RawInput>(raw: RawInput) {
const result = schema.safeParse(transformed);
if (!result.success) {
- // @TODO:send telemetry here
- // throw new ValidationError(
- // `Failed to transform${options?.label ? ` ${options.label}` : ''}`,
- // {
- // cause: result.error,
- // }
- // );
- logger.error(result.error);
+ const label = options?.label ? ` ${options.label}` : '';
+ const issues = result.error.issues
+ .map(issue => ` - ${issue.path.join('.')}: ${issue.message}`)
+ .join('\n');
+ logger.warn(`Transform validation failed${label}:\n${issues}`);
return transformed;
}
diff --git a/ts/packages/core/test/utils/transform.test.ts b/ts/packages/core/test/utils/transform.test.ts
new file mode 100644
index 000000000..843e068f5
--- /dev/null
+++ b/ts/packages/core/test/utils/transform.test.ts
@@ -0,0 +1,114 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { z } from 'zod/v3';
+
+const mockLogger = vi.hoisted(() => ({
+ warn: vi.fn(),
+ error: vi.fn(),
+ info: vi.fn(),
+ debug: vi.fn(),
+}));
+
+vi.mock('../../src/utils/logger', () => ({
+ default: mockLogger,
+}));
+
+vi.mock('../../src/index', () => ({
+ logger: mockLogger,
+}));
+
+import { transform } from '../../src/utils/transform';
+
+describe('transform', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('should return validated data when schema matches', () => {
+ const schema = z.object({
+ name: z.string(),
+ age: z.number(),
+ });
+
+ const result = transform({ raw_name: 'Alice', raw_age: 30 })
+ .with(schema)
+ .using(raw => ({ name: raw.raw_name, age: raw.raw_age }));
+
+ expect(result).toEqual({ name: 'Alice', age: 30 });
+ });
+
+ it('should strip extra fields via Zod parsing on valid data', () => {
+ const schema = z.object({
+ name: z.string(),
+ });
+
+ const result = transform({ name: 'Alice', extra: 'field' })
+ .with(schema)
+ .using(raw => raw as any);
+
+ expect(result).toEqual({ name: 'Alice' });
+ expect(result).not.toHaveProperty('extra');
+ });
+
+ it('should return untransformed data and log a warning on validation failure', () => {
+ const schema = z.object({
+ name: z.string(),
+ age: z.number(),
+ });
+
+ const result = transform({ name: 123, age: 'not a number' })
+ .with(schema)
+ .using(raw => raw as any);
+
+ expect(result).toEqual({ name: 123, age: 'not a number' });
+
+ expect(mockLogger.warn).toHaveBeenCalledTimes(1);
+ expect(mockLogger.error).not.toHaveBeenCalled();
+
+ const warnMessage = mockLogger.warn.mock.calls[0][0] as string;
+ expect(warnMessage).toContain('Transform validation failed');
+ expect(warnMessage).toContain('name');
+ expect(warnMessage).toContain('age');
+ });
+
+ it('should include label in the warning message when provided', () => {
+ const schema = z.object({ id: z.string() });
+
+ transform({ id: 42 })
+ .with(schema)
+ .using(raw => raw as any, { label: 'ConnectedAccount' });
+
+ expect(mockLogger.warn).toHaveBeenCalledTimes(1);
+ const warnMessage = mockLogger.warn.mock.calls[0][0] as string;
+ expect(warnMessage).toContain('ConnectedAccount');
+ });
+
+ it('should not log anything when validation succeeds', () => {
+ const schema = z.object({ id: z.string() });
+
+ const result = transform({ id: 'abc' })
+ .with(schema)
+ .using(raw => raw as any);
+
+ expect(result).toEqual({ id: 'abc' });
+ expect(mockLogger.warn).not.toHaveBeenCalled();
+ expect(mockLogger.error).not.toHaveBeenCalled();
+ });
+
+ it('should handle nested schema validation failures', () => {
+ const schema = z.object({
+ user: z.object({
+ email: z.string().email(),
+ }),
+ });
+
+ const result = transform({ user: { email: 'not-an-email' } })
+ .with(schema)
+ .using(raw => raw as any);
+
+ expect(result).toEqual({ user: { email: 'not-an-email' } });
+ expect(mockLogger.warn).toHaveBeenCalledTimes(1);
+
+ const warnMessage = mockLogger.warn.mock.calls[0][0] as string;
+ expect(warnMessage).toContain('user.email');
+ });
+});