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

fix(types): set this to doc type in SchemaType.prototype.validate() #12663

Merged
merged 2 commits into from Nov 7, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #12590

Summary

SchemaType should have a reference to the DocType so it knows that type to set this to in when the user does schema.path('mypath').validate(function(v) { ... }). With this PR, the following script compiles fine

import { Schema, SchemaType, InferSchemaType } from 'mongoose';
  
const UserSchema = new Schema({
  _password: String
});

type User = InferSchemaType<typeof UserSchema>;

UserSchema.path('hashed_password').validate(function (v) {
  if (this._password && this._password.length < 8) {
    this.invalidate('password', 'Password must be at least 8 characters.');
  }
});

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though with a question

test/types/schema.test.ts Outdated Show resolved Hide resolved
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Nov 7, 2022
@vkarpov15 vkarpov15 merged commit 4559282 into master Nov 7, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12590 branch November 7, 2022 17:07
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

Successfully merging this pull request may close these issues.

the key of this in Schema.path occur error with typecsrcipt
3 participants