mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user