feat: Filter DSL for collections.list (#13442)

* feat: Filter DSL for collections.list

Adds the structured `filters` expression language to collections.list,
matching documents.list and documents.search. Supported fields are name,
createdAt, updatedAt, archivedAt, createdById and permission, each validated
against its column type at the input layer.

Deprecates the ad-hoc `query` and `statusFilter` params, which now translate
into filter leaves so that one code path serves both shapes, and returns a 400
when a caller combines them with `filters`.

Note that `query` matching is no longer accent-insensitive, as the unaccent
comparison is replaced by the shared iLike conversion.

* fix: Make filter expressions assignable to a JSON request body

Filters are sent as a JSON body, but `FilterCondition` and `FilterGroup` were
interfaces, which TypeScript does not give an implicit index signature, and
`FilterValue` admitted a `Date` that JSON cannot carry. Both stores worked
around this by asserting the request body to JSONObject, which suppressed
checking of every other field as well.

Declare the two shapes as type aliases and drop `Date` from the value union so
a filter type-checks against the request body directly, and remove the
assertions from the collection and document stores.

* feat: Restrict filter fields to a set of accepted values

Add an optional `values` set to a filter field definition, checked against
scalar and array values at the input layer so that an unsupported value returns
a 400 rather than an empty result or, for a field backed by a Postgres enum, a
database error. A field with a fixed value set is matched exactly, so pattern
operators are rejected for it in the same way as for non-text columns.

Apply it to `permission` on collections.list.

* refactor: Build the list_collections name filter with the filter DSL

The MCP tool carried its own copy of the unaccent name comparison that
collections.list used before it moved to the DSL. Build the clause with
buildWhere instead, so both paths match names the same way, and drop the
replacements plumbing it needed.
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-23 16:03:10 -04:00
1 parent 9a0a4eff2d
commit d551ead073
10 files changed
+454 -66

No files matched your search

+21
View File
@@ -315,6 +315,27 @@ describe("createFilterSchema value allowlists", () => {
}).success
).toBe(true);
});
it("still allows null operators with no value", () => {
expect(
EnumSchema.safeParse({
field: "role",
operator: "isNull",
}).success
).toBe(true);
});
it("rejects a disallowed value nested inside a group", () => {
expect(
EnumSchema.safeParse({
operator: "AND",
filters: [
{ field: "title", operator: "eq", value: "x" },
{ field: "role", operator: "eq", value: "superuser" },
],
}).success
).toBe(false);
});
});
describe("createFilterSchema node limit", () => {
+14 -5
View File
@@ -39,7 +39,10 @@ export type ComparisonOperator = z.infer<typeof ComparisonOperator>;
export const LogicalOperator = z.enum(["AND", "OR"]);
export type LogicalOperator = z.infer<typeof LogicalOperator>;
/** The value a filter condition compares against. */
/**
* The value a filter condition compares against. Filters travel over the wire
* as JSON, so the value is limited to JSON types.
*/
export const FilterValue = z.union([
z.string(),
z.number(),
@@ -52,9 +55,12 @@ export type FilterValue = z.infer<typeof FilterValue>;
/**
* A single comparison against one field, the leaf of a filter expression.
*
* Declared as a type alias rather than an interface so that TypeScript infers
* an index signature and a filter stays assignable to a JSON request body.
*
* @typeParam F the field names the condition may reference.
*/
export interface FilterCondition<F extends string = string> {
export type FilterCondition<F extends string = string> = {
/** The field being compared. */
field: F;
@@ -63,21 +69,24 @@ export interface FilterCondition<F extends string = string> {
/** The value to compare against, omitted for `isNull` and `isNotNull`. */
value?: FilterValue;
}
};
/**
* A set of filter expressions combined under one logical operator. Groups may
* contain other groups, forming a tree.
*
* Declared as a type alias rather than an interface so that TypeScript infers
* an index signature and a filter stays assignable to a JSON request body.
*
* @typeParam F the field names the nested conditions may reference.
*/
export interface FilterGroup<F extends string = string> {
export type FilterGroup<F extends string = string> = {
/** How the nested expressions are combined. */
operator: LogicalOperator;
/** The nested expressions, each a condition or a further group. */
filters: Array<FilterCondition<F> | FilterGroup<F>>;
}
};
/**
* A filter expression: either a single condition or a group of them.