From 5e6aacb0caa8e34a313d62e518857ba0a8e4178b Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 17 Jan 2024 06:57:49 +0000 Subject: [PATCH] fix: allow args with different types --- packages/nocodb-sdk/src/lib/formulaHelpers.ts | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.ts index 8f9436dbf5..7f9f6b0bf7 100644 --- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts +++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts @@ -267,7 +267,9 @@ interface FormulaMeta { max?: number; rqd?: number; - type?: FormulaDataTypes; + // array of allowed types when args types are not same + // types should be in order of args + type?: FormulaDataTypes | FormulaDataTypes[]; }; custom?: (args: FormulaDataTypes[], parseTree: any) => void; }; @@ -956,38 +958,11 @@ export const formulas: Record = { args: { min: 2, max: 3, - }, - custom(argTypes: FormulaDataTypes[], parsedTree) { - if (argTypes[0] !== FormulaDataTypes.STRING) { - throw new FormulaError( - FormulaErrorType.INVALID_ARG, - { - key: 'msg.formula.stringTypeIsExpected', - calleeName: parsedTree.callee?.name?.toUpperCase(), - }, - 'String type is expected' - ); - } - if (argTypes[1] !== FormulaDataTypes.NUMERIC) { - throw new FormulaError( - FormulaErrorType.INVALID_ARG, - { - key: 'msg.formula.numericTypeIsExpected', - calleeName: parsedTree.callee?.name?.toUpperCase(), - }, - 'Numeric type is expected' - ); - } - if (argTypes[2] && argTypes[2] !== FormulaDataTypes.NUMERIC) { - throw new FormulaError( - FormulaErrorType.INVALID_ARG, - { - key: 'msg.formula.numericTypeIsExpected', - calleeName: parsedTree.callee?.name?.toUpperCase(), - }, - 'Numeric type is expected' - ); - } + type: [ + FormulaDataTypes.STRING, + FormulaDataTypes.NUMERIC, + FormulaDataTypes.NUMERIC, + ], }, }, description: @@ -1007,6 +982,11 @@ export const formulas: Record = { validation: { args: { rqd: 3, + type: [ + FormulaDataTypes.STRING, + FormulaDataTypes.NUMERIC, + FormulaDataTypes.NUMERIC, + ], }, custom(argTypes: FormulaDataTypes[], parsedTree) { if (argTypes[0] !== FormulaDataTypes.STRING) { @@ -1758,9 +1738,16 @@ export async function validateFormulaAndExtractTreeWithType({ } // validate against expected arg types if present else if (formulas[calleeName].validation?.args?.type) { - const expectedArgType = formulas[calleeName].validation.args.type; + for (let i = 0; i < validateResult.length; i++) { + const argPt = validateResult[i]; + + // if type + const expectedArgType = Array.isArray( + formulas[calleeName].validation.args.type + ) + ? formulas[calleeName].validation.args.type[i] + : formulas[calleeName].validation.args.type; - for (const argPt of validateResult) { if ( argPt.dataType !== expectedArgType && argPt.dataType !== FormulaDataTypes.NULL && @@ -1786,7 +1773,6 @@ export async function validateFormulaAndExtractTreeWithType({ } else { let key = '', message = 'Invalid argument type'; - if (expectedArgType === FormulaDataTypes.NUMERIC) { key = 'msg.formula.numericTypeIsExpected'; message = 'Numeric type is expected';