fix(Set Node): Do not stringify null and undefined (#7313)

Github issue / Community forum post (link here to close automatically):
https://community.n8n.io/t/null-and-undefined-in-string-fields/31060/1
This commit is contained in:
Michael Kret 2023-10-03 11:18:33 +03:00 committed by GitHub
parent 3704760724
commit f0a66873b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 5 deletions

View file

@ -12,13 +12,14 @@ export class Set extends VersionedNodeType {
icon: 'fa:pen', icon: 'fa:pen',
group: ['input'], group: ['input'],
description: 'Add or edit fields on an input item and optionally remove other fields', description: 'Add or edit fields on an input item and optionally remove other fields',
defaultVersion: 3, defaultVersion: 3.1,
}; };
const nodeVersions: IVersionedNodeType['nodeVersions'] = { const nodeVersions: IVersionedNodeType['nodeVersions'] = {
1: new SetV1(baseDescription), 1: new SetV1(baseDescription),
2: new SetV1(baseDescription), 2: new SetV1(baseDescription),
3: new SetV2(baseDescription), 3: new SetV2(baseDescription),
3.1: new SetV2(baseDescription),
}; };
super(nodeVersions, baseDescription); super(nodeVersions, baseDescription);

View file

@ -21,7 +21,7 @@ const versionDescription: INodeTypeDescription = {
name: 'set', name: 'set',
icon: 'fa:pen', icon: 'fa:pen',
group: ['input'], group: ['input'],
version: 3, version: [3, 3.1],
description: 'Change the structure of your items', description: 'Change the structure of your items',
subtitle: '={{$parameter["mode"]}}', subtitle: '={{$parameter["mode"]}}',
defaults: { defaults: {

View file

@ -159,13 +159,26 @@ export const validateEntry = (
node: INode, node: INode,
itemIndex: number, itemIndex: number,
ignoreErrors = false, ignoreErrors = false,
nodeVersion?: number,
) => { ) => {
let entryValue = entry[entry.type]; let entryValue = entry[entry.type];
const name = entry.name; const name = entry.name;
const entryType = entry.type.replace('Value', '') as FieldType; 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 (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); entryValue = JSON.stringify(entryValue);
} else { } else {
entryValue = String(entryValue); entryValue = String(entryValue);
@ -179,7 +192,6 @@ export const validateEntry = (
validationResult.newValue = entry[entry.type]; validationResult.newValue = entry[entry.type];
} else { } else {
const message = `${validationResult.errorMessage} [item ${itemIndex}]`; 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, { throw new NodeOperationError(node, message, {
itemIndex, itemIndex,
description, description,

View file

@ -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; newData[name] = value;
} }