Skip to content

Commit

Permalink
types: allow arbitrary keys in query filters again
Browse files Browse the repository at this point in the history
Revert #14764
Fix #14863
Fix #14862
  • Loading branch information
vkarpov15 committed Sep 9, 2024
1 parent d533e9f commit c6af5ec
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 32 deletions.
28 changes: 1 addition & 27 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function find() {
Project.find({ name: 'Hello' });

// just callback; this is no longer supported on .find()
expectError(Project.find((error: CallbackError, result: IProject[]) => console.log(error, result)));
Project.find((error: CallbackError, result: IProject[]) => console.log(error, result));

// filter + projection
Project.find({}, undefined);
Expand Down Expand Up @@ -977,29 +977,3 @@ function testWithLevel1NestedPaths() {
'foo.one': string | null | undefined
}>({} as Test2);
}

function gh14764TestFilterQueryRestrictions() {
const TestModel = model<{ validKey: number }>('Test', new Schema({}));
// A key not in the schema should be invalid
expectError(TestModel.find({ invalidKey: 0 }));
// A key not in the schema should be invalid for simple root operators
expectError(TestModel.find({ $and: [{ invalidKey: 0 }] }));

// Any "nested" keys should be valid
TestModel.find({ 'validKey.subkey': 0 });

// And deeply "nested" keys should be valid
TestModel.find({ 'validKey.deep.nested.key': 0 });
TestModel.find({ validKey: { deep: { nested: { key: 0 } } } });

// Any Query should be accepted as the root argument (due to merge support)
TestModel.find(TestModel.find());
// A Query should not be a valid type for a FilterQuery within an op like $and
expectError(TestModel.find({ $and: [TestModel.find()] }));

const id = new Types.ObjectId();
// Any ObjectId should be accepted as the root argument
TestModel.find(id);
// A ObjectId should not be a valid type for a FilterQuery within an op like $and
expectError(TestModel.find({ $and: [id] }));
}
8 changes: 3 additions & 5 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module 'mongoose' {
*/
type RootFilterQuery<T> = FilterQuery<T> | Query<any, any> | Types.ObjectId;

type FilterQuery<T> ={
type FilterQuery<T> = {
[P in keyof T]?: Condition<T[P]>;
} & RootQuerySelector<T> & { _id?: Condition<string>; };

Expand Down Expand Up @@ -117,10 +117,8 @@ declare module 'mongoose' {
/** @see https://www.mongodb.com/docs/manual/reference/operator/query/comment/#op._S_comment */
$comment?: string;
$expr?: Record<string, any>;
// we could not find a proper TypeScript generic to support nested queries e.g. 'user.friends.name'
// this will mark all unrecognized properties as any (including nested queries) only if
// they include a "." (to avoid generically allowing any unexpected keys)
[nestedSelector: `${string}.${string}`]: any;
// this will mark all unrecognized properties as any (including nested queries)
[key: string]: any;
};

interface QueryTimestampsConfig {
Expand Down

0 comments on commit c6af5ec

Please sign in to comment.