Skip to content

Model.validate() types are wrong #16338

Description

@tomkel

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

9.7.1

Node.js version

26.3.1

MongoDB server version

8.3.4

Typescript version (if applicable)

6.0.3

Description

Here are the current types for Model.validate

mongoose/types/models.d.ts

Lines 687 to 691 in 03bedea

/** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
validate(): Promise<void>;
validate(obj: any): Promise<void>;
validate(obj: any, pathsOrOptions: PathsToValidate): Promise<void>;
validate(obj: any, pathsOrOptions: { pathsToSkip?: pathsToSkip }): Promise<void>;

It says return type is void. But that's not true because validate calls castObj and returns it.

obj = this.castObject(obj);

Steps to Reproduce

conts validated = await MyModel.validate({ a: 'string' })
validated.a // ts(2339): Property 'a' does not exist on type 'void'.

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions