Throw nicer error if field does not exist on collection (#15803)

* Throw nicer error if field does not exist on collection

* Simplify type signature and shift to be consistent with validateFilterOperator

Co-authored-by: ian <licitdev@gmail.com>
This commit is contained in:
Gerard Lamusse
2022-10-15 11:25:30 +02:00
committed by GitHub
parent 18bf1344e1
commit e4df9b09ba

View File

@@ -2,6 +2,7 @@ import {
Aggregate,
ClientFilterOperator,
FieldFunction,
FieldOverview,
Filter,
Query,
Relation,
@@ -367,20 +368,24 @@ export function applyFilter(
if (!columnPath) continue;
validateFilterOperator(
schema.collections[targetCollection].fields[stripFunction(filterPath[filterPath.length - 1])].type,
filterOperator,
schema.collections[targetCollection].fields[stripFunction(filterPath[filterPath.length - 1])].special
const { type, special } = validateFilterField(
schema.collections[targetCollection].fields,
stripFunction(filterPath[filterPath.length - 1]),
targetCollection
);
validateFilterOperator(type, filterOperator, special);
applyFilterToQuery(columnPath, filterOperator, filterValue, logical, targetCollection);
} else {
validateFilterOperator(
schema.collections[collection].fields[stripFunction(filterPath[0])].type,
filterOperator,
schema.collections[collection].fields[stripFunction(filterPath[0])].special
const { type, special } = validateFilterField(
schema.collections[collection].fields,
stripFunction(filterPath[0]),
collection
);
validateFilterOperator(type, filterOperator, special);
applyFilterToQuery(`${collection}.${filterPath[0]}`, filterOperator, filterValue, logical);
}
} else if (subQuery === false || filterPath.length > 1) {
@@ -417,6 +422,14 @@ export function applyFilter(
}
}
function validateFilterField(fields: Record<string, FieldOverview>, key: string, collection = 'unknown') {
if (fields[key] === undefined) {
throw new InvalidQueryException(`Invalid filter key "${key}" on "${collection}"`);
}
return fields[key];
}
function validateFilterOperator(type: Type, filterOperator: string, special: string[]) {
if (filterOperator.startsWith('_')) {
filterOperator = filterOperator.slice(1);