fix(Call n8n Sub-Workflow Tool Node): Fix json type when using $fromAI (#13102)

This commit is contained in:
oleg
2025-02-06 17:58:43 +01:00
committed by GitHub
parent 577d5a1bff
commit 9e4e1ca1f4
3 changed files with 115 additions and 4 deletions

View File

@@ -34,9 +34,61 @@ export function generateZodSchema(placeholder: FromAIArgument): z.ZodTypeAny {
case 'boolean':
schema = z.boolean();
break;
case 'json':
schema = z.record(z.any());
case 'json': {
interface CustomSchemaDef extends z.ZodTypeDef {
jsonSchema?: {
anyOf: [
{
type: 'object';
minProperties: number;
additionalProperties: boolean;
},
{
type: 'array';
minItems: number;
},
];
};
}
// Create a custom schema to validate that the incoming data is either a non-empty object or a non-empty array.
const customSchema = z.custom<Record<string, unknown> | unknown[]>(
(data: unknown) => {
if (data === null || typeof data !== 'object') return false;
if (Array.isArray(data)) {
return data.length > 0;
}
return Object.keys(data).length > 0;
},
{
message: 'Value must be a non-empty object or a non-empty array',
},
);
// Cast the custom schema to a type that includes our JSON metadata.
const typedSchema = customSchema as z.ZodType<
Record<string, unknown> | unknown[],
CustomSchemaDef
>;
// Attach the updated `jsonSchema` metadata to the internal definition.
typedSchema._def.jsonSchema = {
anyOf: [
{
type: 'object',
minProperties: 1,
additionalProperties: true,
},
{
type: 'array',
minItems: 1,
},
],
};
schema = typedSchema;
break;
}
default:
schema = z.string();
}