Skip to content

Commit

Permalink
fix(Set Node): Do not stringify null and undefined (#7313)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):
https://community.n8n.io/t/null-and-undefined-in-string-fields/31060/1
  • Loading branch information
michael-radency authored Oct 3, 2023
1 parent 3704760 commit f0a6687
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Set/Set.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ export class Set extends VersionedNodeType {
icon: 'fa:pen',
group: ['input'],
description: 'Add or edit fields on an input item and optionally remove other fields',
defaultVersion: 3,
defaultVersion: 3.1,
};

const nodeVersions: IVersionedNodeType['nodeVersions'] = {
1: new SetV1(baseDescription),
2: new SetV1(baseDescription),
3: new SetV2(baseDescription),
3.1: new SetV2(baseDescription),
};

super(nodeVersions, baseDescription);
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Set/v2/SetV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const versionDescription: INodeTypeDescription = {
name: 'set',
icon: 'fa:pen',
group: ['input'],
version: 3,
version: [3, 3.1],
description: 'Change the structure of your items',
subtitle: '={{$parameter["mode"]}}',
defaults: {
Expand Down
16 changes: 14 additions & 2 deletions packages/nodes-base/nodes/Set/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,26 @@ export const validateEntry = (
node: INode,
itemIndex: number,
ignoreErrors = false,
nodeVersion?: number,
) => {
let entryValue = entry[entry.type];
const name = entry.name;
const entryType = entry.type.replace('Value', '') as FieldType;

const description = `To fix the error try to change the type for the field "${name}" or activate the option “Ignore Type Conversion Errors” to apply a less strict type validation`;

if (entryType === 'string') {
if (typeof entryValue === 'object') {
if (nodeVersion && nodeVersion > 3 && (entryValue === undefined || entryValue === null)) {
if (ignoreErrors) {
return { name, value: null };
} else {
throw new NodeOperationError(
node,
`'${name}' expects a ${entryType} but we got '${String(entryValue)}' [item ${itemIndex}]`,
{ description },
);
}
} else if (typeof entryValue === 'object') {
entryValue = JSON.stringify(entryValue);
} else {
entryValue = String(entryValue);
Expand All @@ -179,7 +192,6 @@ export const validateEntry = (
validationResult.newValue = entry[entry.type];
} else {
const message = `${validationResult.errorMessage} [item ${itemIndex}]`;
const description = `To fix the error try to change the type for the field "${name}" or activate the option “Ignore Type Conversion Errors” to apply a less strict type validation`;
throw new NodeOperationError(node, message, {
itemIndex,
description,
Expand Down
8 changes: 7 additions & 1 deletion packages/nodes-base/nodes/Set/v2/manual.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ export async function execute(
);
}

const { name, value } = validateEntry(entry, node, i, options.ignoreConversionErrors);
const { name, value } = validateEntry(
entry,
node,
i,
options.ignoreConversionErrors,
node.typeVersion,
);
newData[name] = value;
}

Expand Down

0 comments on commit f0a6687

Please sign in to comment.