mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 11:01:15 +00:00
fix(core)!: Type coercion of $fromAI default values (#19128)
This commit is contained in:
@@ -104,19 +104,32 @@ export function generateZodSchema(placeholder: FromAIArgument): z.ZodTypeAny {
|
||||
return schema;
|
||||
}
|
||||
|
||||
function isFromAIArgumentType(value: string): value is FromAIArgumentType {
|
||||
return ['string', 'number', 'boolean', 'json'].includes(value.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the default value, preserving its original type.
|
||||
* @param value The default value as a string.
|
||||
* @param type The expected type of the default value.
|
||||
* @returns The parsed default value in its appropriate type.
|
||||
*/
|
||||
function parseDefaultValue(
|
||||
value: string | undefined,
|
||||
type: FromAIArgumentType = 'string',
|
||||
): string | number | boolean | Record<string, unknown> | undefined {
|
||||
if (value === undefined || value === '') return undefined;
|
||||
if (value === undefined) return value;
|
||||
|
||||
const lowerValue = value.toLowerCase();
|
||||
if (lowerValue === 'true') return true;
|
||||
if (lowerValue === 'false') return false;
|
||||
if (!isNaN(Number(value))) return Number(value);
|
||||
if (type === 'string') {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
if (type === 'boolean' && (lowerValue === 'true' || lowerValue === 'false'))
|
||||
return lowerValue === 'true';
|
||||
if (type === 'number' && !isNaN(Number(value))) return Number(value);
|
||||
|
||||
// For type 'json' or any other case, attempt to parse as JSON
|
||||
try {
|
||||
return jsonParse(value);
|
||||
} catch {
|
||||
@@ -197,17 +210,17 @@ function parseArguments(argsString: string): FromAIArgument {
|
||||
return trimmed;
|
||||
});
|
||||
|
||||
const type = cleanArgs?.[2] || 'string';
|
||||
const type = cleanArgs?.[2] ?? 'string';
|
||||
|
||||
if (!['string', 'number', 'boolean', 'json'].includes(type.toLowerCase())) {
|
||||
if (!isFromAIArgumentType(type)) {
|
||||
throw new ParseError(`Invalid type: ${type}`);
|
||||
}
|
||||
|
||||
return {
|
||||
key: cleanArgs[0] || '',
|
||||
description: cleanArgs[1],
|
||||
type: (cleanArgs?.[2] ?? 'string') as FromAIArgumentType,
|
||||
defaultValue: parseDefaultValue(cleanArgs[3]),
|
||||
type,
|
||||
defaultValue: parseDefaultValue(cleanArgs[3], type),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user