Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ SchemaNumber.prototype.castForQuery = function($conditional, val, context) {
if ($conditional != null) {
handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new CastError('number', val, this.path, null, this);
throw new MongooseError('Can\'t use ' + $conditional + ' with Number.');
}
return handler.call(this, val, context);
}
Expand Down
12 changes: 12 additions & 0 deletions test/types.number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ describe('types.number', function() {
done();
});

it('throws an error when using an unsupported query operator (gh-16062)', function() {
const n = new SchemaNumber();
let err;
try {
n.castForQuery('$wrong', 100);
} catch (e) {
err = e;
}
assert.ok(err);
assert.ok(err.message.includes('Can\'t use $wrong with Number'));
});

it('throws a CastError with a bad conditional (gh-6927)', function() {
const n = new SchemaNumber();
let err;
Expand Down
10 changes: 9 additions & 1 deletion test/types/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ async function gh15779() {

type AgeType = typeof v8Filter.age;
ExpectAssignable<typeof v8Filter.age>()(42);
// @ts-expect-error Type '"taco"' is not assignable to type 'Condition<ApplyBasicQueryCasting<number>> | undefined'.
// @ts-expect-error Type '"taco"' is not assignable to type 'StrictCondition<ApplyBasicQueryCasting<number>> | undefined'.
const a1: AgeType = 'taco';

const TestModel = model('Test', new Schema({ age: Number, name: String }));
Expand Down Expand Up @@ -910,3 +910,11 @@ async function gh15779_2() {
{ $match: {} as QueryFilter<Job> }
]);
}

async function gh16062() {
const Test = model('Test', new Schema({ runtime: Number }));
// @ts-expect-error No overload matches this call.
await Test.findOne({ runtime: { $wrong: 100 } });
// @ts-expect-error No overload matches this call.
await Test.findOne({ $and: [{ runtime: { $wrong: 100 } }] });
}
31 changes: 24 additions & 7 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,34 @@ declare module 'mongoose' {
? DateQueryTypeCasting
: T;

export type ApplyBasicQueryCasting<T> = QueryTypeCasting<T> | QueryTypeCasting<T[]> | (T extends (infer U)[] ? QueryTypeCasting<U> : T) | null;
export type ApplyBasicQueryCasting<T> = QueryTypeCasting<T> | QueryTypeCasting<T>[] | (T extends (infer U)[] ? QueryTypeCasting<U> : T) | null;

type RemoveIndexSignature<T> = {
[K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
};

type StrictFilterOperators<TValue> = RemoveIndexSignature<mongodb.FilterOperators<TValue>>;

type StrictCondition<T> = mongodb.AlternativeType<T> | StrictFilterOperators<mongodb.AlternativeType<T>>;

type _StrictFilter<TSchema> = {
[P in keyof TSchema]?: StrictCondition<ApplyBasicQueryCasting<TSchema[P]>>;
} & StrictRootFilterOperators<TSchema>;

type StrictRootFilterOperators<TSchema> = Omit<mongodb.RootFilterOperators<TSchema>, '$and' | '$or' | '$nor'> & {
$and?: _StrictFilter<TSchema>[];
$or?: _StrictFilter<TSchema>[];
$nor?: _StrictFilter<TSchema>[];
};

type _QueryFilter<T> = (
{ [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } &
mongodb.RootFilterOperators<{ [P in keyof mongodb.WithId<T>]?: ApplyBasicQueryCasting<mongodb.WithId<T>[P]>; }>
{ [P in keyof T]?: StrictCondition<ApplyBasicQueryCasting<T[P]>>; } &
StrictRootFilterOperators<{ [P in keyof mongodb.WithId<T>]?: ApplyBasicQueryCasting<mongodb.WithId<T>[P]>; }>
);
type _QueryFilterLooseId<T> = (
{ [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } &
mongodb.RootFilterOperators<
{ [P in keyof T]?: ApplyBasicQueryCasting<T[P]>; } &
{ _id?: any; }
{ [P in keyof T]?: StrictCondition<ApplyBasicQueryCasting<T[P]>>; } &
StrictRootFilterOperators<
{ [P in keyof T]?: ApplyBasicQueryCasting<T[P]>; }
>
);
type QueryFilter<T> = IsItRecordAndNotAny<T> extends true ? _QueryFilter<WithLevel1NestedPaths<T>> : _QueryFilterLooseId<Record<string, any>>;
Expand Down