mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
fix: Do not show [object Object] in error message when validating type (no-changelog) (#9591)
This commit is contained in:
parent
19e5c0397a
commit
cda1e3f6e5
|
@ -9,6 +9,7 @@ import {
|
||||||
ApplicationError,
|
ApplicationError,
|
||||||
NodeOperationError,
|
NodeOperationError,
|
||||||
deepCopy,
|
deepCopy,
|
||||||
|
getValueDescription,
|
||||||
jsonParse,
|
jsonParse,
|
||||||
validateFieldType,
|
validateFieldType,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
@ -184,7 +185,7 @@ export const validateEntry = (
|
||||||
} else {
|
} else {
|
||||||
throw new NodeOperationError(
|
throw new NodeOperationError(
|
||||||
node,
|
node,
|
||||||
`'${name}' expects a ${type} but we got '${String(value)}' [item ${itemIndex}]`,
|
`'${name}' expects a ${type} but we got ${getValueDescription(value)} [item ${itemIndex}]`,
|
||||||
{ description },
|
{ description },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -145,6 +145,16 @@ export const tryToParseObject = (value: unknown): object => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getValueDescription = <T>(value: T): string => {
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
if (value === null) return "'null'";
|
||||||
|
if (Array.isArray(value)) return 'array';
|
||||||
|
return 'object';
|
||||||
|
}
|
||||||
|
|
||||||
|
return `'${String(value)}'`;
|
||||||
|
};
|
||||||
|
|
||||||
export const tryToParseUrl = (value: unknown): string => {
|
export const tryToParseUrl = (value: unknown): string => {
|
||||||
if (typeof value === 'string' && !value.includes('://')) {
|
if (typeof value === 'string' && !value.includes('://')) {
|
||||||
value = `http://${value}`;
|
value = `http://${value}`;
|
||||||
|
@ -196,7 +206,8 @@ export function validateFieldType(
|
||||||
const strict = options.strict ?? false;
|
const strict = options.strict ?? false;
|
||||||
const valueOptions = options.valueOptions ?? [];
|
const valueOptions = options.valueOptions ?? [];
|
||||||
const parseStrings = options.parseStrings ?? false;
|
const parseStrings = options.parseStrings ?? false;
|
||||||
const defaultErrorMessage = `'${fieldName}' expects a ${type} but we got '${String(value)}'`;
|
|
||||||
|
const defaultErrorMessage = `'${fieldName}' expects a ${type} but we got ${getValueDescription(value)}`;
|
||||||
switch (type.toLowerCase()) {
|
switch (type.toLowerCase()) {
|
||||||
case 'string': {
|
case 'string': {
|
||||||
if (!parseStrings) return { valid: true, newValue: value };
|
if (!parseStrings) return { valid: true, newValue: value };
|
||||||
|
@ -256,7 +267,7 @@ export function validateFieldType(
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
errorMessage: `'${fieldName}' expects time (hh:mm:(:ss)) but we got '${String(value)}'.`,
|
errorMessage: `'${fieldName}' expects time (hh:mm:(:ss)) but we got ${getValueDescription(value)}.`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -287,9 +298,9 @@ export function validateFieldType(
|
||||||
if (!isValidOption) {
|
if (!isValidOption) {
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
errorMessage: `'${fieldName}' expects one of the following values: [${validOptions}] but we got '${String(
|
errorMessage: `'${fieldName}' expects one of the following values: [${validOptions}] but we got ${getValueDescription(
|
||||||
value,
|
value,
|
||||||
)}'`,
|
)}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return { valid: true, newValue: value };
|
return { valid: true, newValue: value };
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { validateFieldType } from '@/TypeValidation';
|
import { getValueDescription, validateFieldType } from '@/TypeValidation';
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
const VALID_ISO_DATES = [
|
const VALID_ISO_DATES = [
|
||||||
|
@ -230,4 +230,15 @@ describe('Type Validation', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('getValueDescription util function', () => {
|
||||||
|
it('should return correct description', () => {
|
||||||
|
expect(getValueDescription('foo')).toBe("'foo'");
|
||||||
|
expect(getValueDescription(42)).toBe("'42'");
|
||||||
|
expect(getValueDescription(true)).toBe("'true'");
|
||||||
|
expect(getValueDescription(null)).toBe("'null'");
|
||||||
|
expect(getValueDescription(undefined)).toBe("'undefined'");
|
||||||
|
expect(getValueDescription([{}])).toBe('array');
|
||||||
|
expect(getValueDescription({})).toBe('object');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue