Skip to content

Commit

Permalink
fix(n8n Form Trigger Node): Update error in validateResponseModeConfi…
Browse files Browse the repository at this point in the history
…guration (no-changelog) (#13162)
  • Loading branch information
michael-radency authored Feb 11, 2025
1 parent 78644b0 commit 6abb1f9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ describe('FormTrigger', () => {
);

await expect(
testVersionedWebhookTriggerNode(FormTrigger, 2, {
testVersionedWebhookTriggerNode(FormTrigger, 2.1, {
node: {
typeVersion: 2.1,
parameters: {
responseMode: 'onReceived',
},
Expand Down
56 changes: 56 additions & 0 deletions packages/nodes-base/nodes/Form/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
resolveRawData,
isFormConnected,
sanitizeHtml,
validateResponseModeConfiguration,
} from '../utils';

describe('FormTrigger, parseFormDescription', () => {
Expand Down Expand Up @@ -939,3 +940,58 @@ describe('FormTrigger, isFormConnected', () => {
expect(result).toBe(true);
});
});

describe('validateResponseModeConfiguration', () => {
let webhookFunctions: ReturnType<typeof mock<IWebhookFunctions>>;

beforeEach(() => {
webhookFunctions = mock<IWebhookFunctions>();

webhookFunctions.getNode.mockReturnValue({
name: 'TestNode',
typeVersion: 2.2,
} as INode);

webhookFunctions.getChildNodes.mockReturnValue([]);
});

test('throws error if responseMode is "responseNode" but no Respond to Webhook node is connected', () => {
webhookFunctions.getNodeParameter.mockReturnValue('responseNode');

expect(() => validateResponseModeConfiguration(webhookFunctions)).toThrow(
'No Respond to Webhook node found in the workflow',
);
});

test('throws error if "Respond to Webhook" node is connected but "responseMode" is not "responseNode" in typeVersion <= 2.1', () => {
webhookFunctions.getNodeParameter.mockReturnValue('onReceived');
webhookFunctions.getNode.mockReturnValue({
name: 'TestNode',
typeVersion: 2.1,
} as INode);
webhookFunctions.getChildNodes.mockReturnValue([
{ type: 'n8n-nodes-base.respondToWebhook' } as NodeTypeAndVersion,
]);

expect(() => validateResponseModeConfiguration(webhookFunctions)).toThrow(
'TestNode node not correctly configured',
);
});

test('throws error if "Respond to Webhook" node is connected, version >= 2.2', () => {
webhookFunctions.getNodeParameter.mockReturnValue('responseNode');
webhookFunctions.getChildNodes.mockReturnValue([
{ type: 'n8n-nodes-base.respondToWebhook' } as NodeTypeAndVersion,
]);

expect(() => validateResponseModeConfiguration(webhookFunctions)).toThrow(
'The "Respond to Webhook" node is not supported in workflows initiated by the "n8n Form Trigger"',
);
});

test('does not throw an error mode in not "responseNode" and no "Respond to Webhook" node is connected', () => {
webhookFunctions.getNodeParameter.mockReturnValue('onReceived');

expect(() => validateResponseModeConfiguration(webhookFunctions)).not.toThrow();
});
});
18 changes: 16 additions & 2 deletions packages/nodes-base/nodes/Form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ export function prepareFormData({
return formData;
}

const validateResponseModeConfiguration = (context: IWebhookFunctions) => {
export const validateResponseModeConfiguration = (context: IWebhookFunctions) => {
const responseMode = context.getNodeParameter('responseMode', 'onReceived') as string;
const connectedNodes = context.getChildNodes(context.getNode().name);
const nodeVersion = context.getNode().typeVersion;

const isRespondToWebhookConnected = connectedNodes.some(
(node) => node.type === 'n8n-nodes-base.respondToWebhook',
Expand All @@ -206,7 +207,7 @@ const validateResponseModeConfiguration = (context: IWebhookFunctions) => {
);
}

if (isRespondToWebhookConnected && responseMode !== 'responseNode') {
if (isRespondToWebhookConnected && responseMode !== 'responseNode' && nodeVersion <= 2.1) {
throw new NodeOperationError(
context.getNode(),
new Error(`${context.getNode().name} node not correctly configured`),
Expand All @@ -216,6 +217,19 @@ const validateResponseModeConfiguration = (context: IWebhookFunctions) => {
},
);
}

if (isRespondToWebhookConnected && nodeVersion > 2.1) {
throw new NodeOperationError(
context.getNode(),
new Error(
'The "Respond to Webhook" node is not supported in workflows initiated by the "n8n Form Trigger"',
),
{
description:
'To configure your response, add an "n8n Form" node and set the "Page Type" to "Form Ending"',
},
);
}
};

export async function prepareFormReturnItem(
Expand Down

0 comments on commit 6abb1f9

Please sign in to comment.