From dd7e84d7cf47ea48ba7ddb70c19c855f7e66fde7 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sat, 21 Oct 2023 17:55:42 +0530 Subject: [PATCH] fix: add option to validate filter Signed-off-by: Pranav C --- packages/nocodb/src/db/BaseModelSqlv2.ts | 29 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index d4b542fd53..3eb6af0be0 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -2994,7 +2994,7 @@ class BaseModelSqlv2 { const { where } = this._getListArgs(args); const qb = this.dbDriver(this.tnPath); const aliasColObjMap = await this.model.getAliasColObjMap(); - const filterObj = extractFilterFromXwhere(where, aliasColObjMap); + const filterObj = extractFilterFromXwhere(where, aliasColObjMap, true); const conditionObj = [ new Filter({ @@ -4872,6 +4872,7 @@ export function extractSortsObject( export function extractFilterFromXwhere( str, aliasColObjMap: { [columnAlias: string]: Column }, + throwErrorIfInvalid = false, ) { if (!str) { return []; @@ -4893,7 +4894,11 @@ export function extractFilterFromXwhere( nestedArrayConditions = str.split( /(?=~(?:or(?:not)?|and(?:not)?|not)\()/, ); - return extractCondition(nestedArrayConditions || [], aliasColObjMap); + return extractCondition( + nestedArrayConditions || [], + aliasColObjMap, + throwErrorIfInvalid, + ); } // iterate until finding right closing @@ -4921,7 +4926,11 @@ export function extractFilterFromXwhere( const lhsOfNestedQuery = str.substring(0, openIndex); nestedArrayConditions.push( - ...extractFilterFromXwhere(lhsOfNestedQuery, aliasColObjMap), + ...extractFilterFromXwhere( + lhsOfNestedQuery, + aliasColObjMap, + throwErrorIfInvalid, + ), // calling recursively for nested query new Filter({ is_group: true, @@ -4932,7 +4941,11 @@ export function extractFilterFromXwhere( ), }), // RHS of nested query(recursion) - ...extractFilterFromXwhere(str.substring(closingIndex + 2), aliasColObjMap), + ...extractFilterFromXwhere( + str.substring(closingIndex + 2), + aliasColObjMap, + throwErrorIfInvalid, + ), ); return nestedArrayConditions; } @@ -4959,7 +4972,11 @@ function validateFilterComparison(uidt: UITypes, op: any, sub_op?: any) { } } -export function extractCondition(nestedArrayConditions, aliasColObjMap) { +export function extractCondition( + nestedArrayConditions, + aliasColObjMap, + throwErrorIfInvalid, +) { return nestedArrayConditions?.map((str) => { let [logicOp, alias, op, value] = str.match(/(?:~(and|or|not))?\((.*?),(\w+),(.*)\)/)?.slice(1) || []; @@ -4986,6 +5003,8 @@ export function extractCondition(nestedArrayConditions, aliasColObjMap) { } validateFilterComparison(aliasColObjMap[alias].uidt, op, sub_op); + } else if (throwErrorIfInvalid) { + NcError.badRequest(`Column ${alias} not found.`); } return new Filter({