From 11b8f68da8ece5b00f6be4c595fb3dd790f0b526 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 29 Dec 2023 12:38:09 +0530 Subject: [PATCH 1/3] fix: SUBTR argument validation correction --- packages/nocodb-sdk/src/lib/formulaHelpers.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.ts index 89689677d0..d6e91ee7ff 100644 --- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts +++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts @@ -913,7 +913,38 @@ export const formulas: Record = { args: { min: 2, max: 3, - type: FormulaDataTypes.STRING, + }, + 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' + ); + } }, }, description: From 156f420c3bd4e16df212f35641ee911efb93b92a Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 29 Dec 2023 12:47:52 +0530 Subject: [PATCH 2/3] fix: type validation corrections RIGHT, LEFT and MID --- packages/nocodb-sdk/src/lib/formulaHelpers.ts | 79 +++++++++++++++++-- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.ts index d6e91ee7ff..6b9450e0d8 100644 --- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts +++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts @@ -321,7 +321,6 @@ export const formulas: Record = { validation: { args: { rqd: 3, - type: FormulaDataTypes.DATE, }, custom: (_argTypes: FormulaDataTypes[], parsedTree: any) => { if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { @@ -377,6 +376,7 @@ export const formulas: Record = { validation: { args: { rqd: 1, + type: FormulaDataTypes.DATE, }, }, syntax: 'DATESTR(date | datetime)', @@ -388,6 +388,7 @@ export const formulas: Record = { validation: { args: { rqd: 1, + type: FormulaDataTypes.DATE, }, }, syntax: 'DAY(date | datetime)', @@ -399,6 +400,7 @@ export const formulas: Record = { validation: { args: { rqd: 1, + type: FormulaDataTypes.DATE, }, }, syntax: 'MONTH(date | datetime)', @@ -410,6 +412,7 @@ export const formulas: Record = { validation: { args: { rqd: 1, + type: FormulaDataTypes.DATE, }, }, syntax: 'DAY(time | datetime)', @@ -425,7 +428,6 @@ export const formulas: Record = { args: { min: 2, max: 3, - type: FormulaDataTypes.DATE, }, custom: (_argTypes: FormulaDataTypes[], parsedTree: any) => { if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { @@ -814,7 +816,6 @@ export const formulas: Record = { validation: { args: { rqd: 0, - type: FormulaDataTypes.DATE, }, }, description: 'Retrieve the current time and day.', @@ -882,8 +883,29 @@ export const formulas: Record = { validation: { args: { rqd: 2, - type: FormulaDataTypes.STRING, }, + 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' + ); + } + } }, description: 'Retrieve the last n characters from the input string.', syntax: 'RIGHT(str, n)', @@ -897,8 +919,29 @@ export const formulas: Record = { validation: { args: { rqd: 2, - type: FormulaDataTypes.STRING, }, + 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' + ); + } + } }, description: 'Retrieve the first n characters from the input string.', syntax: 'LEFT(str, n)', @@ -964,8 +1007,31 @@ export const formulas: Record = { validation: { args: { rqd: 3, - type: FormulaDataTypes.STRING, }, + 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' + ); + } + for(const i in [1,2]) { + if (argTypes[i] !== FormulaDataTypes.NUMERIC) { + throw new FormulaError( + FormulaErrorType.INVALID_ARG, + { + key: 'msg.formula.numericTypeIsExpected', + calleeName: parsedTree.callee?.name?.toUpperCase(), + }, + 'Numeric type is expected' + ); + } + } + } }, description: 'Extracts a substring; an alias for SUBSTR.', syntax: 'MID(str, position, [count])', @@ -1083,7 +1149,6 @@ export const formulas: Record = { args: { min: 1, max: 2, - type: FormulaDataTypes.NUMERIC, }, custom(_argTypes: FormulaDataTypes[], parsedTree: any) { if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { From 1588962182e68f6284d6ddb535d92911e7af1bf3 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 29 Dec 2023 12:52:36 +0530 Subject: [PATCH 3/3] fix: typo correction --- packages/nocodb-sdk/src/lib/formulaHelpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.ts index 6b9450e0d8..3b472b47ef 100644 --- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts +++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts @@ -1019,7 +1019,7 @@ export const formulas: Record = { 'String type is expected' ); } - for(const i in [1,2]) { + for(const i of [1,2]) { if (argTypes[i] !== FormulaDataTypes.NUMERIC) { throw new FormulaError( FormulaErrorType.INVALID_ARG,