Browse Source

refactor: improved/corrected validation messages

pull/7268/head
Pranav C 9 months ago
parent
commit
2aead9ef22
  1. 38
      packages/nocodb-sdk/src/lib/formulaHelpers.ts

38
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -1312,24 +1312,40 @@ export function validateFormulaAndExtractTreeWithType({
// validate against expected arg types if present // validate against expected arg types if present
else if (formulas[calleeName].validation?.args?.type) { else if (formulas[calleeName].validation?.args?.type) {
const expectedArgType = formulas[calleeName].validation.args.type; const expectedArgType = formulas[calleeName].validation.args.type;
for (const argPt of validateResult) {
if ( if (
argTypes.some( argPt.dataType !== expectedArgType &&
(argType) => argPt.dataType !== FormulaDataTypes.NULL &&
argType !== expectedArgType && argPt.dataType !== FormulaDataTypes.UNKNOWN
argType !== FormulaDataTypes.NULL &&
argType !== FormulaDataTypes.UNKNOWN
)
) { ) {
let key = ''; if (argPt.type === JSEPNode.IDENTIFIER) {
throw new FormulaError(
FormulaErrorType.INVALID_ARG,
{
key: 'msg.formula.columnWithTypeFoundButExpected',
columnName: parsedTree.name,
columnType: parsedTree.dataType,
expectedType: expectedArgType,
},
`Field ${parsedTree.name} with ${parsedTree.dataType} type is found but ${expectedArgType} type is expected`
);
} else {
let key = '',
message = 'Invalid argument type';
if (expectedArgType === FormulaDataTypes.NUMERIC) { if (expectedArgType === FormulaDataTypes.NUMERIC) {
key = 'msg.formula.numericTypeIsExpected'; key = 'msg.formula.numericTypeIsExpected';
message = 'Numeric type is expected';
} else if (expectedArgType === FormulaDataTypes.STRING) { } else if (expectedArgType === FormulaDataTypes.STRING) {
key = 'msg.formula.stringTypeIsExpected'; key = 'msg.formula.stringTypeIsExpected';
message = 'String type is expected';
} else if (expectedArgType === FormulaDataTypes.BOOLEAN) { } else if (expectedArgType === FormulaDataTypes.BOOLEAN) {
key = 'msg.formula.booleanTypeIsExpected'; key = 'msg.formula.booleanTypeIsExpected';
message = 'Boolean type is expected';
} else if (expectedArgType === FormulaDataTypes.DATE) { } else if (expectedArgType === FormulaDataTypes.DATE) {
key = 'msg.formula.dateTypeIsExpected'; key = 'msg.formula.dateTypeIsExpected';
message = 'Date type is expected';
} }
throw new FormulaError( throw new FormulaError(
@ -1338,10 +1354,12 @@ export function validateFormulaAndExtractTreeWithType({
key, key,
calleeName, calleeName,
}, },
'Invalid argument type' message
); );
} }
} }
}
}
if (typeof formulas[calleeName].returnType === 'function') { if (typeof formulas[calleeName].returnType === 'function') {
res.dataType = (formulas[calleeName].returnType as any)?.( res.dataType = (formulas[calleeName].returnType as any)?.(
@ -1358,8 +1376,8 @@ export function validateFormulaAndExtractTreeWithType({
throw new FormulaError( throw new FormulaError(
FormulaErrorType.INVALID_COLUMN, FormulaErrorType.INVALID_COLUMN,
{ {
key: 'msg.formula.invalidColumn', key: 'msg.formula.columnNotAvailable',
column: parsedTree.name, columnName: parsedTree.name,
}, },
`Invalid column name/id ${JSON.stringify(parsedTree.name)} in formula` `Invalid column name/id ${JSON.stringify(parsedTree.name)} in formula`
); );

Loading…
Cancel
Save