mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-28 22:19:41 -08:00
fix(core): Fix validation of items returned in the task runner (#11897)
Some checks failed
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Has been cancelled
Some checks failed
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Has been cancelled
This commit is contained in:
parent
30c6d969bf
commit
a535e88f1a
|
@ -0,0 +1,110 @@
|
|||
import { ValidationError } from '@/js-task-runner/errors/validation-error';
|
||||
import {
|
||||
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();
|
||||
});
|
||||
|
||||
test.each([
|
||||
['binary', {}],
|
||||
['pairedItem', {}],
|
||||
['error', {}],
|
||||
])(
|
||||
'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();
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
|
@ -9,7 +9,7 @@ export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem', '
|
|||
function validateTopLevelKeys(item: INodeExecutionData, itemIndex: number) {
|
||||
for (const key in item) {
|
||||
if (Object.prototype.hasOwnProperty.call(item, key)) {
|
||||
if (REQUIRED_N8N_ITEM_KEYS.has(key)) return;
|
||||
if (REQUIRED_N8N_ITEM_KEYS.has(key)) continue;
|
||||
|
||||
throw new ValidationError({
|
||||
message: `Unknown top-level item key: ${key}`,
|
||||
|
|
Loading…
Reference in a new issue