You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.9.7
Node.js version
18.20.4
MongoDB server version
6.12.0
Typescript version (if applicable)
5.6.3
Description
The type of this in schema validator methods is reported as the RawDocType passed to new mongoose.Schema<RawDocType, ...>.
Steps to Reproduce
Here is a minimal 'blogpost' example that will produce the error. I have attempted the workaround mentioned in #14720 in this example and it highlights the different type expectations.
import mongoose from 'mongoose';
type PostPersisted = {
title: string,
postTime: Date
};
type ValidatorThis = DocumentValidatorThis | QueryValidatorThis;
type DocumentValidatorThis = mongoose.Document & PostPersisted;
type QueryValidatorThis = mongoose.Query<PostRecord, PostRecord>;
export const PostSchema = new mongoose.Schema<PostPersisted>( {
title: { type: String, required: true },
postTime: {
type: Date,
required: true,
validate: {
validator: async function( this: ValidatorThis, postTime: Date ): Promise<boolean> {
return true;
}
}
}
} );
export type PostRecord = mongoose.HydratedDocument<PostPersisted>;
export const PostModel = mongoose.model<PostPersisted>( 'Post', PostSchema );
The reported error is:
Type '{ type: DateConstructor; required: true; validate: { validator: (this: ValidatorThis, postTime: Date) => Promise<boolean>; }; }' is not assignable to type 'Date | SchemaDefinitionProperty<Date, PostPersisted>'.
Types of property 'validate' are incompatible.
Type '{ validator: (this: ValidatorThis, postTime: Date) => Promise<boolean>; }' is not assignable to type 'Function | RegExp | [RegExp, string] | [Function, string] | ValidateOpts<Mixed, PostPersisted> | ... 6 more ... | readonly SchemaValidator<...>[]'.
Types of property 'validator' are incompatible.
Type '(this: ValidatorThis, postTime: Date) => Promise<boolean>' is not assignable to type 'ValidateFn<Mixed, PostPersisted> | AsyncValidateFn<Mixed, PostPersisted> | ValidateFn<Date, PostPersisted> | AsyncValidateFn<...>'.
Type '(this: ValidatorThis, postTime: Date) => Promise<boolean>' is not assignable to type 'ValidateFn<Mixed, PostPersisted>'.
The 'this' types of each signature are incompatible.
Type 'PostPersisted' is not assignable to type 'ValidatorThis'.
Type 'PostPersisted' is not assignable to type 'DocumentValidatorThis'.
Type 'PostPersisted' is missing the following properties from type 'Document<unknown, any, any>': _id, $assertPopulated, $clearModifiedPaths, $clone, and 51 more.ts(2322)
blogPost.ts(5, 3): The expected type comes from property 'postTime' which is declared here on type 'PostPersisted | { title?: SchemaDefinitionProperty<string, PostPersisted>; postTime?: SchemaDefinitionProperty<Date, PostPersisted>; }'
Expected Behavior
Based on my understanding of the docs, the type of this should be either:
The hydrated document type: mongoose.HydratedDocument<RawDocType> in the case of a regular validation.
A Query type: mongoose.Query<ResultType, DocType> in the case of validation caused by update methods with runValidators: true set.
The text was updated successfully, but these errors were encountered:
Prerequisites
Mongoose version
8.9.7
Node.js version
18.20.4
MongoDB server version
6.12.0
Typescript version (if applicable)
5.6.3
Description
The type of
this
in schema validator methods is reported as theRawDocType
passed tonew mongoose.Schema<RawDocType, ...>
.Steps to Reproduce
Here is a minimal 'blogpost' example that will produce the error. I have attempted the workaround mentioned in #14720 in this example and it highlights the different type expectations.
The reported error is:
Expected Behavior
Based on my understanding of the docs, the type of
this
should be either:mongoose.HydratedDocument<RawDocType>
in the case of a regular validation.mongoose.Query<ResultType, DocType>
in the case of validation caused by update methods withrunValidators: true
set.The text was updated successfully, but these errors were encountered: