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

Validate in schema options is not properly typed #14696

Closed
2 tasks done
viganll opened this issue Jun 28, 2024 · 0 comments · Fixed by #14720
Closed
2 tasks done

Validate in schema options is not properly typed #14696

viganll opened this issue Jun 28, 2024 · 0 comments · Fixed by #14720
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@viganll
Copy link

viganll commented Jun 28, 2024

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.3.1

Node.js version

18.17.1

MongoDB server version

6

Typescript version (if applicable)

4.8

Description

The validate config is not properly typed in schema options e.g

 isActive: {
            type: Boolean,
            default: false,
            validate: {
                validator(v) {
                   // this & v are any
                    return !v || this.name === "super admin"
                },
            },
        },

Steps to Reproduce

import { Schema } from "mongoose";

interface User {
  name: string;
  isActive: boolean;
}

const userSchema = new Schema<User>({
  name: {
    type: String,
    required: [true, "Name on card is required"],
  },
  isActive: {
    type: Boolean,
    default: false,
    validate: {
      validator(v) {
        return !v || this.name === "super admin";
      },
    },
  },
});

Expected Behavior

I would expect that this is properly typed.

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jul 1, 2024
@vkarpov15 vkarpov15 added this to the 8.4.5 milestone Jul 1, 2024
vkarpov15 added a commit that referenced this issue Jul 4, 2024
types: correct `this` for `validate.validator` schematype option
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 a pull request may close this issue.

2 participants