refactor(core): Consolidate task result validation in Code node (#18928)

This commit is contained in:
Iván Ovejero
2025-09-02 16:05:10 +02:00
committed by GitHub
parent 57baa10fa4
commit fbaee9ac02
12 changed files with 269 additions and 553 deletions

View File

@@ -13,7 +13,6 @@ import type { JsRunnerConfig } from '@/config/js-runner-config';
import { MainConfig } from '@/config/main-config';
import { ExecutionError } from '@/js-task-runner/errors/execution-error';
import { UnsupportedFunctionError } from '@/js-task-runner/errors/unsupported-function.error';
import { ValidationError } from '@/js-task-runner/errors/validation-error';
import type { JSExecSettings } from '@/js-task-runner/js-task-runner';
import { JsTaskRunner } from '@/js-task-runner/js-task-runner';
import {
@@ -30,7 +29,6 @@ import {
withPairedItem,
wrapIntoJson,
} from './test-data';
import { ReservedKeyFoundError } from '../errors/reserved-key-not-found.error';
jest.mock('ws');
@@ -237,7 +235,7 @@ describe('JsTaskRunner', () => {
inputItems,
});
expect(outcome.result).toEqual([wrapIntoJson(needsWrapping ? { val: expected } : expected)]);
expect(outcome.result).toEqual(needsWrapping ? { val: expected } : expected);
};
const testExpressionForEachItem = async (
@@ -250,22 +248,28 @@ describe('JsTaskRunner', () => {
inputItems,
});
expect(outcome.result).toEqual([
withPairedItem(0, wrapIntoJson(needsWrapping ? { val: expected } : expected)),
]);
// if expected has json property, use it, else use entire result
const expectedJson =
expected !== null && typeof expected === 'object' && 'json' in expected
? expected.json
: needsWrapping
? { val: expected }
: expected;
expect(outcome.result).toEqual([{ json: expectedJson, pairedItem: { item: 0 } }]);
};
const testGroups = {
// https://docs.n8n.io/code/builtin/current-node-input/
'current node input': [
['$input.first()', inputItems[0]],
['$input.last()', inputItems[inputItems.length - 1]],
['$input.first()', { json: inputItems[0] }],
['$input.last()', { json: inputItems[inputItems.length - 1] }],
['$input.params', { manualTriggerParam: 'empty' }],
],
// https://docs.n8n.io/code/builtin/output-other-nodes/
'output of other nodes': [
['$("Trigger").first()', inputItems[0]],
['$("Trigger").last()', inputItems[inputItems.length - 1]],
['$("Trigger").first()', { json: inputItems[0] }],
['$("Trigger").last()', { json: inputItems[inputItems.length - 1] }],
['$("Trigger").params', { manualTriggerParam: 'empty' }],
],
// https://docs.n8n.io/code/builtin/date-time/
@@ -360,7 +364,7 @@ describe('JsTaskRunner', () => {
}),
});
expect(outcome.result).toEqual([wrapIntoJson({ val: 'value' })]);
expect(outcome.result).toEqual({ val: 'value' });
});
it('should be possible to access env if it has been blocked', async () => {
@@ -411,7 +415,7 @@ describe('JsTaskRunner', () => {
}),
});
expect(outcome.result).toEqual([wrapIntoJson({ val: undefined })]);
expect(outcome.result).toEqual({ val: undefined });
});
});
@@ -431,7 +435,7 @@ describe('JsTaskRunner', () => {
});
const helsinkiTimeNow = DateTime.now().setZone('Europe/Helsinki').toSeconds();
expect(outcome.result[0].json.val).toBeCloseTo(helsinkiTimeNow, 1);
expect(outcome.result).toEqual({ val: expect.closeTo(helsinkiTimeNow, 1) });
});
it('should use the default timezone', async () => {
@@ -448,7 +452,7 @@ describe('JsTaskRunner', () => {
});
const helsinkiTimeNow = DateTime.now().setZone('Europe/Helsinki').toSeconds();
expect(outcome.result[0].json.val).toBeCloseTo(helsinkiTimeNow, 1);
expect(outcome.result).toEqual({ val: expect.closeTo(helsinkiTimeNow, 1) });
});
});
@@ -466,7 +470,7 @@ describe('JsTaskRunner', () => {
}),
});
expect(outcome.result).toEqual([wrapIntoJson({ val: { key: 'value' } })]);
expect(outcome.result).toEqual({ val: { key: 'value' } });
});
it('should have the global workflow static data available in runOnceForEachItem', async () => {
@@ -559,7 +563,7 @@ describe('JsTaskRunner', () => {
taskData: createTaskDataWithNodeStaticData({ key: 'value' }),
});
expect(outcome.result).toEqual([wrapIntoJson({ val: { key: 'value' } })]);
expect(outcome.result).toEqual({ val: { key: 'value' } });
});
it('should have the node workflow static data available in runOnceForEachItem', async () => {
@@ -745,7 +749,7 @@ describe('JsTaskRunner', () => {
}),
});
expect(outcomeAll.result).toEqual([wrapIntoJson({ val: 'test-buffer' })]);
expect(outcomeAll.result).toEqual({ val: 'test-buffer' });
const outcomePer = await execTaskWithParams({
task: newTaskParamsWithSettings({
@@ -789,38 +793,6 @@ describe('JsTaskRunner', () => {
});
});
describe('invalid output', () => {
test.each([['undefined'], ['42'], ['"a string"']])(
'should throw a ValidationError if the code output is %s',
async (output) => {
await expect(
executeForAllItems({
code: `return ${output}`,
inputItems: [{ a: 1 }],
}),
).rejects.toThrow(ValidationError);
},
);
it('should throw a ValidationError if some items are wrapped in json and some are not', async () => {
await expect(
executeForAllItems({
code: 'return [{b: 1}, {json: {b: 2}}]',
inputItems: [{ a: 1 }],
}),
).rejects.toThrow(ValidationError);
});
it('should throw a ReservedKeyFoundError if there are unknown keys alongside reserved keys', async () => {
await expect(
executeForAllItems({
code: 'return [{json: {b: 1}, objectId: "123"}]',
inputItems: [{ a: 1 }],
}),
).rejects.toThrow(ReservedKeyFoundError);
});
});
it('should return static items', async () => {
const outcome = await executeForAllItems({
code: 'return [{json: {b: 1}}]',
@@ -852,19 +824,19 @@ describe('JsTaskRunner', () => {
});
expect(outcome).toEqual({
result: [wrapIntoJson({ b: 1 })],
result: [{ b: 1 }],
customData: undefined,
});
});
it('should wrap single item into an array and json', async () => {
it('should not wrap single item into an array and json', async () => {
const outcome = await executeForAllItems({
code: 'return {b: 1}',
inputItems: [{ a: 1 }],
});
expect(outcome).toEqual({
result: [wrapIntoJson({ b: 1 })],
result: { b: 1 },
customData: undefined,
});
});
@@ -914,20 +886,6 @@ describe('JsTaskRunner', () => {
});
});
describe('invalid output', () => {
test.each([['undefined'], ['42'], ['"a string"'], ['[]'], ['[1,2,3]']])(
'should throw a ValidationError if the code output is %s',
async (output) => {
await expect(
executeForEachItem({
code: `return ${output}`,
inputItems: [{ a: 1 }],
}),
).rejects.toThrow(ValidationError);
},
);
});
describe('chunked execution', () => {
it('should use correct index for each item', async () => {
const outcome = await executeForEachItem({
@@ -1112,7 +1070,7 @@ describe('JsTaskRunner', () => {
runner,
});
expect(outcome.result).toEqual([wrapIntoJson({ val: expected })]);
expect(outcome.result).toEqual({ val: expected });
},
);
@@ -1174,7 +1132,7 @@ describe('JsTaskRunner', () => {
runner,
});
expect(outcome.result).toEqual([wrapIntoJson({ val: expected })]);
expect(outcome.result).toEqual({ val: expected });
},
);
@@ -1526,7 +1484,7 @@ describe('JsTaskRunner', () => {
inputItems: [],
});
expect(outcome.result).toEqual([wrapIntoJson({ val: 2 })]);
expect(outcome.result).toEqual({ val: 2 });
});
});
});

View File

@@ -1,119 +0,0 @@
import { ValidationError } from '@/js-task-runner/errors/validation-error';
import {
NonArrayOfObjectsError,
validateRunForAllItemsOutput,
validateRunForEachItemOutput,
} from '@/js-task-runner/result-validation';
describe('result validation', () => {
describe('validateRunForAllItemsOutput', () => {
it('should throw an error if the output is not an object', () => {
expect(() => {
validateRunForAllItemsOutput(undefined);
}).toThrowError(ValidationError);
});
it('should throw an error if the output is an array and at least one item has a non-n8n key', () => {
expect(() => {
validateRunForAllItemsOutput([{ json: {} }, { json: {}, unknownKey: {} }]);
}).toThrowError(ValidationError);
});
it('should not throw an error if the output is an array and all items are json wrapped', () => {
expect(() => {
validateRunForAllItemsOutput([{ json: {} }, { json: {} }, { json: {} }]);
}).not.toThrow();
});
it('should throw a NonArrayOfObjectsError if the output is an array of arrays (empty)', () => {
expect(() => {
// @ts-expect-error Intentionally invalid
validateRunForAllItemsOutput([[]]);
}).toThrowError(NonArrayOfObjectsError);
});
test.each([
['binary', {}],
['pairedItem', {}],
['error', {}],
['index', {}], // temporarily allowed until refactored out
])(
'should not throw an error if the output item has %s key in addition to json',
(key, value) => {
expect(() => {
validateRunForAllItemsOutput([{ json: {} }, { json: {}, [key]: value }]);
}).not.toThrow();
},
);
it('should not throw an error if the output is an array and all items are not json wrapped', () => {
expect(() => {
validateRunForAllItemsOutput([
{
id: 1,
name: 'test3',
},
{
id: 2,
name: 'test4',
},
{
id: 3,
name: 'test5',
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any);
}).not.toThrow();
});
it('should throw if json is not an object', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validateRunForAllItemsOutput([{ json: 1 } as any]);
}).toThrowError(ValidationError);
});
});
describe('validateRunForEachItemOutput', () => {
const index = 0;
it('should throw an error if the output is not an object', () => {
expect(() => {
validateRunForEachItemOutput(undefined, index);
}).toThrowError(ValidationError);
});
it('should throw an error if the output is an array', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validateRunForEachItemOutput([] as any, index);
}).toThrowError(ValidationError);
});
it('should throw if json is not an object', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validateRunForEachItemOutput({ json: 1 } as any, index);
}).toThrowError(ValidationError);
});
it('should throw an error if the output is an array and at least one item has a non-n8n key', () => {
expect(() => {
validateRunForEachItemOutput({ json: {}, unknownKey: {} }, index);
}).toThrowError(ValidationError);
});
test.each([
['binary', {}],
['pairedItem', {}],
['error', {}],
])(
'should not throw an error if the output item has %s key in addition to json',
(key, value) => {
expect(() => {
validateRunForEachItemOutput({ json: {}, [key]: value }, index);
}).not.toThrow();
},
);
});
});

View File

@@ -1,11 +0,0 @@
import { ValidationError } from './validation-error';
export class ReservedKeyFoundError extends ValidationError {
constructor(reservedKey: string, itemIndex: number) {
super({
message: 'Invalid output format',
description: `An output item contains the reserved key <code>${reservedKey}</code>. To get around this, please wrap each item in an object, under a key called <code>json</code>. <a href="https://docs.n8n.io/data/data-structure/#data-structure" target="_blank">Example</a>`,
itemIndex,
});
}
}

View File

@@ -1,44 +0,0 @@
import { SerializableError } from './serializable-error';
export class ValidationError extends SerializableError {
description = '';
itemIndex: number | undefined = undefined;
context: { itemIndex: number } | undefined = undefined;
lineNumber: number | undefined = undefined;
constructor({
message,
description,
itemIndex,
lineNumber,
}: {
message: string;
description: string;
itemIndex?: number;
lineNumber?: number;
}) {
super(message);
this.lineNumber = lineNumber;
this.itemIndex = itemIndex;
if (this.lineNumber !== undefined && this.itemIndex !== undefined) {
this.message = `${message} [line ${lineNumber}, for item ${itemIndex}]`;
} else if (this.lineNumber !== undefined) {
this.message = `${message} [line ${lineNumber}]`;
} else if (this.itemIndex !== undefined) {
this.message = `${message} [item ${itemIndex}]`;
} else {
this.message = message;
}
this.description = description;
if (this.itemIndex !== undefined) {
this.context = { itemIndex: this.itemIndex };
}
}
}

View File

@@ -48,7 +48,6 @@ import { makeSerializable } from './errors/serializable-error';
import { TimeoutError } from './errors/timeout-error';
import type { RequireResolver } from './require-resolver';
import { createRequireResolver } from './require-resolver';
import { validateRunForAllItemsOutput, validateRunForEachItemOutput } from './result-validation';
import { DataRequestResponseReconstruct } from '../data-request/data-request-response-reconstruct';
export interface RpcCallObject {
@@ -240,7 +239,7 @@ export class JsTaskRunner extends TaskRunner {
data: JsTaskData,
workflow: Workflow,
signal: AbortSignal,
): Promise<INodeExecutionData[]> {
): Promise<TaskResultData['result']> {
const dataProxy = this.createDataProxy(data, workflow, data.itemIndex);
const inputItems = data.connectionInputData;
@@ -278,7 +277,7 @@ export class JsTaskRunner extends TaskRunner {
return [];
}
return validateRunForAllItemsOutput(result);
return result;
} catch (e) {
// Errors thrown by the VM are not instances of Error, so map them to an ExecutionError
const error = this.toExecutionErrorIfNeeded(e);
@@ -318,7 +317,7 @@ export class JsTaskRunner extends TaskRunner {
Object.assign(context, dataProxy, { item: inputItems[index] });
try {
let result = await new Promise<INodeExecutionData | undefined>((resolve, reject) => {
const result = await new Promise<INodeExecutionData | undefined>((resolve, reject) => {
const abortHandler = () => {
reject(new TimeoutError(this.taskTimeout));
};
@@ -348,17 +347,23 @@ export class JsTaskRunner extends TaskRunner {
continue;
}
result = validateRunForEachItemOutput(result, index);
if (result) {
let jsonData;
if (isObject(result) && 'json' in result) {
jsonData = result.json;
} else {
jsonData = result;
}
returnData.push(
result.binary
? {
json: result.json,
json: jsonData,
pairedItem: { item: index },
binary: result.binary,
}
: {
json: result.json,
json: jsonData,
pairedItem: { item: index },
},
);

View File

@@ -1,148 +0,0 @@
import { normalizeItems } from 'n8n-core';
import type { INodeExecutionData } from 'n8n-workflow';
import { ReservedKeyFoundError } from './errors/reserved-key-not-found.error';
import { ValidationError } from './errors/validation-error';
import { isObject } from './obj-utils';
export const REQUIRED_N8N_ITEM_KEYS = new Set([
'json',
'binary',
'pairedItem',
'error',
/**
* The `index` key was added accidentally to Function, FunctionItem, Gong,
* Execute Workflow, and ToolWorkflowV2, so we need to allow it temporarily.
* Once we stop using it in all nodes, we can stop allowing the `index` key.
*/
'index',
]);
function validateTopLevelKeys(item: INodeExecutionData, itemIndex: number) {
let foundReservedKey: string | null = null;
const unknownKeys: string[] = [];
for (const key in item) {
if (!Object.prototype.hasOwnProperty.call(item, key)) continue;
if (REQUIRED_N8N_ITEM_KEYS.has(key)) {
foundReservedKey ??= key;
} else {
unknownKeys.push(key);
}
}
if (unknownKeys.length > 0) {
if (foundReservedKey) throw new ReservedKeyFoundError(foundReservedKey, itemIndex);
throw new ValidationError({
message: `Unknown top-level item key: ${unknownKeys[0]}`,
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
itemIndex,
});
}
}
function validateItem({ json, binary }: INodeExecutionData, itemIndex: number) {
if (json === undefined || !isObject(json)) {
throw new ValidationError({
message: "A 'json' property isn't an object",
description: "In the returned data, every key named 'json' must point to an object.",
itemIndex,
});
}
if (binary !== undefined && !isObject(binary)) {
throw new ValidationError({
message: "A 'binary' property isn't an object",
description: "In the returned data, every key named 'binary' must point to an object.",
itemIndex,
});
}
}
export class NonArrayOfObjectsError extends ValidationError {
constructor() {
super({
message: "Code doesn't return items properly",
description: 'Please return an array of objects, one for each item you would like to output.',
});
}
}
/**
* Validates the output of a code node in 'Run for All Items' mode.
*/
export function validateRunForAllItemsOutput(
executionResult: INodeExecutionData | INodeExecutionData[] | undefined,
) {
if (Array.isArray(executionResult)) {
for (const item of executionResult) {
if (!isObject(item)) throw new NonArrayOfObjectsError();
}
/**
* If at least one top-level key is an n8n item key (`json`, `binary`, etc.),
* then require all item keys to be an n8n item key.
*
* If no top-level key is an n8n key, then skip this check, allowing non-n8n
* item keys to be wrapped in `json` when normalizing items below.
*/
const mustHaveTopLevelN8nKey = executionResult.some((item) =>
Object.keys(item).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
);
if (mustHaveTopLevelN8nKey) {
for (let index = 0; index < executionResult.length; index++) {
const item = executionResult[index];
validateTopLevelKeys(item, index);
}
}
} else if (!isObject(executionResult)) {
throw new NonArrayOfObjectsError();
}
const returnData = normalizeItems(executionResult);
returnData.forEach(validateItem);
return returnData;
}
/**
* Validates the output of a code node in 'Run for Each Item' mode for single item
*/
export function validateRunForEachItemOutput(
executionResult: INodeExecutionData | undefined,
itemIndex: number,
) {
if (typeof executionResult !== 'object') {
throw new ValidationError({
message: "Code doesn't return an object",
description: `Please return an object representing the output item. ('${executionResult}' was returned instead.)`,
itemIndex,
});
}
if (Array.isArray(executionResult)) {
const firstSentence =
executionResult.length > 0
? `An array of ${typeof executionResult[0]}s was returned.`
: 'An empty array was returned.';
throw new ValidationError({
message: "Code doesn't return a single object",
description: `${firstSentence} If you need to output multiple items, please use the 'Run Once for All Items' mode instead.`,
itemIndex,
});
}
const [returnData] = normalizeItems([executionResult]);
validateItem(returnData, itemIndex);
// If at least one top-level key is a supported item key (`json`, `binary`, etc.),
// and another top-level key is unrecognized, then the user mis-added a property
// directly on the item, when they intended to add it on the `json` property
validateTopLevelKeys(returnData, itemIndex);
return returnData;
}

View File

@@ -59,7 +59,8 @@ export interface DataRequestResponse {
}
export interface TaskResultData {
result: INodeExecutionData[];
/** Raw user output, i.e. not yet validated or normalized. */
result: unknown;
customData?: Record<string, string>;
staticData?: IDataObject;
}