mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

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

Open skrtheboss opened this issue 1 year ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] 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.

skrtheboss avatar Aug 02 '24 06:08 skrtheboss