Skip to content

Commit c6af5ec

Browse files
committed
types: allow arbitrary keys in query filters again
Revert #14764 Fix #14863 Fix #14862
1 parent d533e9f commit c6af5ec

File tree

2 files changed

+4
-32
lines changed

2 files changed

+4
-32
lines changed

test/types/models.test.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function find() {
216216
Project.find({ name: 'Hello' });
217217

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

221221
// filter + projection
222222
Project.find({}, undefined);
@@ -977,29 +977,3 @@ function testWithLevel1NestedPaths() {
977977
'foo.one': string | null | undefined
978978
}>({} as Test2);
979979
}
980-
981-
function gh14764TestFilterQueryRestrictions() {
982-
const TestModel = model<{ validKey: number }>('Test', new Schema({}));
983-
// A key not in the schema should be invalid
984-
expectError(TestModel.find({ invalidKey: 0 }));
985-
// A key not in the schema should be invalid for simple root operators
986-
expectError(TestModel.find({ $and: [{ invalidKey: 0 }] }));
987-
988-
// Any "nested" keys should be valid
989-
TestModel.find({ 'validKey.subkey': 0 });
990-
991-
// And deeply "nested" keys should be valid
992-
TestModel.find({ 'validKey.deep.nested.key': 0 });
993-
TestModel.find({ validKey: { deep: { nested: { key: 0 } } } });
994-
995-
// Any Query should be accepted as the root argument (due to merge support)
996-
TestModel.find(TestModel.find());
997-
// A Query should not be a valid type for a FilterQuery within an op like $and
998-
expectError(TestModel.find({ $and: [TestModel.find()] }));
999-
1000-
const id = new Types.ObjectId();
1001-
// Any ObjectId should be accepted as the root argument
1002-
TestModel.find(id);
1003-
// A ObjectId should not be a valid type for a FilterQuery within an op like $and
1004-
expectError(TestModel.find({ $and: [id] }));
1005-
}

types/query.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare module 'mongoose' {
1212
*/
1313
type RootFilterQuery<T> = FilterQuery<T> | Query<any, any> | Types.ObjectId;
1414

15-
type FilterQuery<T> ={
15+
type FilterQuery<T> = {
1616
[P in keyof T]?: Condition<T[P]>;
1717
} & RootQuerySelector<T> & { _id?: Condition<string>; };
1818

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

126124
interface QueryTimestampsConfig {

0 commit comments

Comments
 (0)