Skip to content

Validation failing on present required elements #15447

@antoniolibrada

Description

@antoniolibrada

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

Node.js version

20.11.0

MongoDB server version

7.x

Typescript version (if applicable)

No response

Description

Hello!

I have been having a problem with the validation when saving a document. It seems there is a limit when nesting objects in a document. This worked perfectly in mongoose 7.x but it's failing in mongoose in 8.15.1.

Maybe I misread the changelog but I didn't see any changes regarding this.

I appreciate any kind of help.

Thanks!

Steps to Reproduce

I created this little snippet to see the behavior I hope it shows the issue.

import mongoose from "mongoose";

//This one does not work
const BadSchema = new mongoose.Schema({
  a: {
    b: {
      c: {
        type: mongoose.Schema.Types.Map,
        of: new mongoose.Schema({
          e: { type: Number, required: true },
        }),
      },
    },
  },
});

const BadModel = mongoose.model("Bad", BadSchema);

const BadData = {
  a: {
    b: {
      c: {
        d: {
          e: 2,
        },
      },
    },
  },
};

//This one does work
const GoodSchema = new mongoose.Schema({
  a: {
    b: {
      type: mongoose.Schema.Types.Map,
      of: new mongoose.Schema({
        e: { type: Number, required: true },
      }),
    },
  },
});

const GoodData = {
  a: {
    b: {
      d: {
        e: 2,
      },
    },
  },
};

const GoodModel = mongoose.model("Good", GoodSchema);

async function main() {
  await mongoose.connect("mongodb://localhost:27017/mydatabase");
  await GoodModel.deleteMany({});
  await GoodModel.create(GoodData);
  const result = await GoodModel.countDocuments();
  console.log(`Number of nested documents in the collection: ${result}`);
  await BadModel.deleteMany({});
  await BadModel.create(BadData);
  const badResult = await BadModel.countDocuments();
  console.log(`Number of bad nested documents in the collection: ${badResult}`);
  await mongoose.disconnect();
}

main();

Im getting the following exception in the output

Number of nested documents in the collection: 1
/Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/document.js:3343
    this.$__.validationError = new ValidationError(this);
                               ^

ValidationError: Bad validation failed: a.b.c.$*.e: Path `e` is required.
    at Document.invalidate (/Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/document.js:3343:32)
    at Subdocument.invalidate (/Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/types/subdocument.js:229:12)
    at /Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/document.js:3104:17
    at /Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/schemaType.js:1407:9
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  errors: {
    'a.b.c.$*.e': ValidatorError: Path `e` is required.
        at validate (/Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/schemaType.js:1404:13)
        at SchemaType.doValidate (/Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/schemaType.js:1388:7)
        at /Users/antoniolibrada/Developer/mongoose-test/node_modules/mongoose/lib/document.js:3096:18
        at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
      properties: {
        validator: [Function (anonymous)],
        message: 'Path `e` is required.',
        type: 'required',
        path: 'e',
        fullPath: undefined,
        value: undefined
      },
      kind: 'required',
      path: 'e',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose#validatorError)]: true
    }
  },
  _message: 'Bad validation failed'
}

Expected Behavior

I expect it to validate as it did with mongoose version 7

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