Browse Source

refactor: validate sort options as well

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/6770/head
Pranav C 1 year ago
parent
commit
7c2727e7e3
  1. 8
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 3
      packages/nocodb/src/db/conditionV2.ts
  3. 11
      packages/nocodb/src/db/sortV2.ts

8
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -3198,7 +3198,7 @@ class BaseModelSqlv2 {
], ],
qb, qb,
undefined, undefined,
true true,
); );
const execQueries: ((trx: Knex.Transaction, qb: any) => Promise<any>)[] = const execQueries: ((trx: Knex.Transaction, qb: any) => Promise<any>)[] =
[]; [];
@ -4851,6 +4851,7 @@ class BaseModelSqlv2 {
export function extractSortsObject( export function extractSortsObject(
_sorts: string | string[], _sorts: string | string[],
aliasColObjMap: { [columnAlias: string]: Column }, aliasColObjMap: { [columnAlias: string]: Column },
throwErrorIfInvalid = false,
): Sort[] { ): Sort[] {
if (!_sorts?.length) return; if (!_sorts?.length) return;
@ -4867,6 +4868,11 @@ export function extractSortsObject(
// replace + at the beginning if present // replace + at the beginning if present
else sort.fk_column_id = aliasColObjMap[s.replace(/^\+/, '')]?.id; else sort.fk_column_id = aliasColObjMap[s.replace(/^\+/, '')]?.id;
if (throwErrorIfInvalid && !sort.fk_column_id)
NcError.unprocessableEntity(
`Invalid column '${s.replace(/^[+-]/, '')}' in sort`,
);
return new Sort(sort); return new Sort(sort);
}); });
} }

3
packages/nocodb/src/db/conditionV2.ts

@ -2,6 +2,7 @@ import { isNumericCol, RelationTypes, UITypes } from 'nocodb-sdk';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
// import customParseFormat from 'dayjs/plugin/customParseFormat.js'; // import customParseFormat from 'dayjs/plugin/customParseFormat.js';
import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2'; import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2';
import { NcError } from '~/helpers/catchError'
import type LinkToAnotherRecordColumn from '~/models/LinkToAnotherRecordColumn'; import type LinkToAnotherRecordColumn from '~/models/LinkToAnotherRecordColumn';
import type { Knex } from 'knex'; import type { Knex } from 'knex';
import type Column from '~/models/Column'; import type Column from '~/models/Column';
@ -114,7 +115,7 @@ const parseConditionV2 = async (
const column = await filter.getColumn(); const column = await filter.getColumn();
if (!column) { if (!column) {
if (throwErrorIfInvalid) { if (throwErrorIfInvalid) {
throw new Error(`Invalid column id '${filter.fk_column_id}' in filter`); NcError.unprocessableEntity(`Invalid column id '${filter.fk_column_id}' in filter`);
} }
return; return;
} }

11
packages/nocodb/src/db/sortV2.ts

@ -7,6 +7,7 @@ import type {
LookupColumn, LookupColumn,
RollupColumn, RollupColumn,
} from '~/models'; } from '~/models';
import { NcError } from '~/helpers/catchError';
import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2'; import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2';
import genRollupSelectv2 from '~/db/genRollupSelectv2'; import genRollupSelectv2 from '~/db/genRollupSelectv2';
import { sanitize } from '~/helpers/sqlSanitize'; import { sanitize } from '~/helpers/sqlSanitize';
@ -17,6 +18,7 @@ export default async function sortV2(
sortList: Sort[], sortList: Sort[],
qb: Knex.QueryBuilder, qb: Knex.QueryBuilder,
alias?: string, alias?: string,
throwErrorIfInvalid = false,
) { ) {
const knex = baseModelSqlv2.dbDriver; const knex = baseModelSqlv2.dbDriver;
@ -32,7 +34,14 @@ export default async function sortV2(
sort = new Sort(_sort); sort = new Sort(_sort);
} }
const column = await sort.getColumn(); const column = await sort.getColumn();
if (!column) continue; if (!column) {
if (throwErrorIfInvalid) {
NcError.unprocessableEntity(
`Invalid column id '${sort.fk_column_id}' in sort`,
);
}
continue;
}
const model = await column.getModel(); const model = await column.getModel();
const nulls = sort.direction === 'desc' ? 'LAST' : 'FIRST'; const nulls = sort.direction === 'desc' ? 'LAST' : 'FIRST';

Loading…
Cancel
Save