mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(Call n8n Sub-Workflow Tool Node): Fix json type when using $fromAI (#13102)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import {
|
||||
extractFromAICalls,
|
||||
type FromAIArgument,
|
||||
traverseNodeParameters,
|
||||
type FromAIArgument,
|
||||
generateZodSchema,
|
||||
} from '@/FromAIParseUtils';
|
||||
|
||||
// Note that for historic reasons a lot of testing of this file happens indirectly in `packages/core/test/CreateNodeAsTool.test.ts`
|
||||
@@ -85,3 +86,61 @@ describe('traverseNodeParameters', () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('JSON Type Parsing via generateZodSchema', () => {
|
||||
it('should correctly parse a JSON parameter without default', () => {
|
||||
// Use an actual $fromAI call string via extractFromAICalls:
|
||||
const [arg] = extractFromAICalls(
|
||||
'$fromAI("jsonWithoutDefault", "JSON parameter without default", "json")',
|
||||
);
|
||||
const schema = generateZodSchema(arg);
|
||||
|
||||
// Valid non-empty JSON objects should pass.
|
||||
expect(() => schema.parse({ key: 'value' })).not.toThrow();
|
||||
expect(schema.parse({ key: 'value' })).toEqual({ key: 'value' });
|
||||
|
||||
// Parsing an empty object should throw a validation error.
|
||||
expect(() => schema.parse({})).toThrowError(
|
||||
/Value must be a non-empty object or a non-empty array/,
|
||||
);
|
||||
});
|
||||
|
||||
it('should correctly parse a JSON parameter with a valid default', () => {
|
||||
const [arg] = extractFromAICalls(
|
||||
'$fromAI("jsonWithValidDefault", "JSON parameter with valid default", "json", "{"key": "defaultValue"}")',
|
||||
);
|
||||
const schema = generateZodSchema(arg);
|
||||
|
||||
// The default value is now stored as a parsed object.
|
||||
expect(schema._def.defaultValue()).toEqual({ key: 'defaultValue' });
|
||||
});
|
||||
|
||||
it('should parse a JSON parameter with an empty default', () => {
|
||||
const [arg] = extractFromAICalls(
|
||||
'$fromAI("jsonEmptyDefault", "JSON parameter with empty default", "json", "{}")',
|
||||
);
|
||||
const schema = generateZodSchema(arg);
|
||||
|
||||
// The default value is stored as an empty object.
|
||||
expect(schema._def.defaultValue()).toEqual({});
|
||||
|
||||
// Parsing an empty object should throw a validation error.
|
||||
expect(() => schema.parse({})).toThrowError(
|
||||
/Value must be a non-empty object or a non-empty array/,
|
||||
);
|
||||
});
|
||||
|
||||
it('should use provided JSON value over the default value', () => {
|
||||
const [arg] = extractFromAICalls(
|
||||
'$fromAI("jsonParamCustom", "JSON parameter with custom default", "json", "{"initial": "value"}")',
|
||||
);
|
||||
const schema = generateZodSchema(arg);
|
||||
|
||||
// Check that the stored default value parses to the expected object.
|
||||
expect(schema._def.defaultValue()).toEqual({ initial: 'value' });
|
||||
|
||||
// When a new valid value is provided, the schema should use it.
|
||||
const newValue = { newKey: 'newValue' };
|
||||
expect(schema.parse(newValue)).toEqual(newValue);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user