Skip to content

Validate in schema options is not properly typed #14696

Closed
@viganll

Description

@viganll

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions