Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type of 'this' in schema field validator method is unexpected #15242

Closed
2 tasks done
RadotJar opened this issue Feb 7, 2025 · 0 comments
Closed
2 tasks done

Type of 'this' in schema field validator method is unexpected #15242

RadotJar opened this issue Feb 7, 2025 · 0 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@RadotJar
Copy link

RadotJar commented Feb 7, 2025

Prerequisites

  • I have written a descriptive issue title
  • 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.
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Feb 8, 2025
@vkarpov15 vkarpov15 added this to the 8.10.1 milestone Feb 8, 2025
vkarpov15 added a commit that referenced this issue Feb 12, 2025
types(middleware): make this in document middleware the hydrated doc type, not raw doc type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

2 participants