From c6af5ec7529a330d5bfb317778e22a3f72925d2a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 9 Sep 2024 15:52:35 -0400 Subject: [PATCH] types: allow arbitrary keys in query filters again Revert #14764 Fix #14863 Fix #14862 --- test/types/models.test.ts | 28 +--------------------------- types/query.d.ts | 8 +++----- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/test/types/models.test.ts b/test/types/models.test.ts index fbe411225f0..a99f6ffa020 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -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); @@ -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] })); -} diff --git a/types/query.d.ts b/types/query.d.ts index 0df0388c3d4..cb447f23c70 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -12,7 +12,7 @@ declare module 'mongoose' { */ type RootFilterQuery = FilterQuery | Query | Types.ObjectId; - type FilterQuery ={ + type FilterQuery = { [P in keyof T]?: Condition; } & RootQuerySelector & { _id?: Condition; }; @@ -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; - // 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 {