Skip to content

Nested discriminator validation kills node process in 6.11.4 #13626

Closed
@skrtheboss

Description

@skrtheboss

Prerequisites

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

Mongoose version

6.11.4

Node.js version

14.16.0

MongoDB server version

4.2

Typescript version (if applicable)

4.9

Description

After upgrading mongoose to version 6.11.4, the validation stopped working in some conditions.
If a discriminator schema is created with leaf nodes that contain custom validation logic then the node process exists.

Steps to Reproduce

const mongoose = require("mongoose")

function createParameterizedAttributeAssignmentSchema() {
    return mongoose.Schema({
        value: {
            type: mongoose.Schema.Types.Mixed,
            validate: {
                validator: function () {
                    throw new Error(`Invalid Value!`);
                }
            }
        }
    })
}

const TestModel = mongoose.model('TestModel', new mongoose.Schema({})).discriminator(
    'type1',
    new mongoose.Schema(
        {
            config: {
                type: new mongoose.Schema({
                    attributes: {
                        type: [createParameterizedAttributeAssignmentSchema()],
                        required: false,
                    },
                })
            },
        }
    )
);

const modelInstance = new TestModel({ __t: 'type1', config: { attributes: [{ value: 'test-value' }] } });

const timeout = setTimeout(() => process.exit(0), 100);

await modelInstance.validate().catch(err => {
    console.log(err instanceof mongoose.Error.ValidationError);
    console.log(err.message);
}).finally(() => clearTimeout(timeout));

Mongoose 6.11.3: https://runkit.com/embed/3g09wn1jwl4q ( Works as expected )
Mongoose 6.11.4: https://runkit.com/embed/cfyj2e1ibkry ( Process exists with exit code 1 )

Expected Behavior

No response

Metadata

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions