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 · 2 comments · Fixed by #14720
Closed
2 tasks done

Validate in schema options is not properly typed #14696

viganll opened this issue Jun 28, 2024 · 2 comments · Fixed by #14720
Labels
backwards-breaking 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
@vkarpov15
Copy link
Collaborator

Re opening based on comments in #14720

@vkarpov15 vkarpov15 modified the milestones: 8.6.4, 8.6.5 Sep 25, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.7.1, 8.7.2 Oct 8, 2024
@akshdev1998
Copy link

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(this: User, v: boolean) {
        // Now `this` is properly typed as `User`
        return !v || this.name === "super admin";
      },
    },
  },
});

TRY THESE ONE!

@vkarpov15 vkarpov15 modified the milestones: 8.7.2, 8.7.3 Oct 14, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.7.3, 8.7.4 Oct 25, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.8.1, 9.0 Nov 5, 2024
vkarpov15 added a commit that referenced this issue Nov 8, 2024
BREAKING CHANGE: change `this` to HydratedDocument for default() and required(), HydratedDocument | Query for validate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards-breaking typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants