Browse Source

fix: add option to validate filter

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/6770/head
Pranav C 1 year ago
parent
commit
dd7e84d7cf
  1. 29
      packages/nocodb/src/db/BaseModelSqlv2.ts

29
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({

Loading…
Cancel
Save