Skip to content

this context in nested schema required functions changed since mongoose 7.0.2+ #14788

Closed

Description

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

Node.js version

20.15.0

MongoDB server version

7.0.0

Typescript version (if applicable)

5.5.4

Description

From Mongoose 7.0.2 onwards, the this context in the required function of nested schemas references the entire parent object, causing incorrect validation behavior.

  • Mongoose Version 7.0.1:

    • The code runs successfully and logs "Post validate".
    • The this context in the required function of prop1 correctly logs the nested schema object.
    • Runkit Example
  • Mongoose Version 7.0.3:

    • The code throws an error during validation.
    • The this context in the required function of prop1 unexpectedly logs the entire parent object, including config.
    • Runkit Example
  • Mongoose Version 8.0.0:

    • The code throws an error during validation.
    • The this context in the required function of prop1 unexpectedly logs the entire parent object, including config.
    • Runkit Example

Observations:

  • In Mongoose 7.0.1, the this context within the required function of prop1 correctly points to the nested schema object.
  • From Mongoose 7.0.2 onwards, the this context unexpectedly includes the parent object, leading to validation errors.

Steps to Reproduce

  1. Run the provided code with Mongoose 7.0.1 and observe the successful validation and logging of "Post validate".
  2. Run the same code with Mongoose 7.0.2 or later versions and observe the validation error and the unexpected this context.
var mongoose = require('mongoose');

function createCustomSchema() {
    return new mongoose.Schema(
        {
            prop1: {
                type: mongoose.Schema.Types.String,
                required() {
                    console.log(this)
                    return this.prop1 === undefined;
                },
                validate: {
                    validator(prop1) {
                        if (this.prop2 !== null && prop1 != null) {
                            throw new Error(`cannot use prop1 if prop2 is defined!`);
                        }
                        return true;
                    },
                },
            },
            prop2: {
                type: mongoose.Schema.Types.String,
                required() {
                    return this.prop2 === undefined;
                },
                validate: {
                    validator(prop2) {
                        if (this.prop1 === null && prop2 === null) {
                            throw new Error(`cannot be null if prop1 is missing!`);
                        }
                        return true;
                    },
                },
            },
        },
        { _id: false },
    );
}

const schema = new mongoose.Schema({
    config: {
        prop: {
            type: createCustomSchema(),
            required: true,
        },
    },
});

const Model = mongoose.model('test-model', schema);

const doc = new Model({
    config: {
        prop: { prop1: null, prop2: 'test-value' },
    },
});

console.log('Pre validate');

await doc.validate()

console.log('Post validate');

Expected Behavior

The this context in the required function of nested schemas should consistently reference the nested schema object, as seen in version 7.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions