Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required option on a sub sub sub document failed validator #11564

Closed
dreimert opened this issue Mar 24, 2022 · 0 comments
Closed

Required option on a sub sub sub document failed validator #11564

dreimert opened this issue Mar 24, 2022 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@dreimert
Copy link

Do you want to request a feature or report a bug?

Report a bug in last version 6.2.8 but not in 6.2.7 or before.

What is the current behavior?
Required option on a sub sub sub document failed validator :

<$pwd>/node_modules/mongoose/lib/document.js:2953
    this.$__.validationError = new ValidationError(this);
                               ^

ValidationError: Test validation failed: list.0.list.0.name: Path `name` is required.
    at model.Document.invalidate (<$pwd>>/node_modules/mongoose/lib/document.js:2953:32)
    at EmbeddedDocument.Subdocument.invalidate (<$pwd>>/node_modules/mongoose/lib/types/subdocument.js:213:12)
    at EmbeddedDocument.Subdocument.invalidate (<$pwd>>/node_modules/mongoose/lib/types/subdocument.js:213:12)
    at <$pwd>>/node_modules/mongoose/lib/document.js:2742:17
    at <$pwd>>/node_modules/mongoose/lib/schematype.js:1324:9
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  errors: {
    'list.0.list.0.name': ValidatorError: Path `name` is required.
        at validate (<$pwd>>/node_modules/mongoose/lib/schematype.js:1321:13)
        at SchemaString.SchemaType.doValidate (<$pwd>>/node_modules/mongoose/lib/schematype.js:1305:7)
        at <$pwd>>/node_modules/mongoose/lib/document.js:2734:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: {
        validator: [Function (anonymous)],
        message: 'Path `name` is required.',
        type: 'required',
        path: 'name',
        value: undefined
      },
      kind: 'required',
      path: 'name',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'Test validation failed'
}

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose')
mongoose.set('debug', true)

const testSchemaSub3 = new mongoose.Schema({
  name: {
    type: String,
    required: true
  }
})

const testSchemaSub2 = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  list: [testSchemaSub3]
})

const testSchemaSub1 = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  list: [testSchemaSub2]
})

const testSchema = new mongoose.Schema({
  name: String,
  list: [testSchemaSub1]
})

const testModel = mongoose.model('Test', testSchema)

async function run () {
  await mongoose.connect('mongodb://localhost:27017')
  await mongoose.connection.dropDatabase()

  const entry1 = await testModel.create({
    name: 'lvl1',
    list: [{
      name: 'lvl2',
      list: [{
        name: 'lvl3',
        // list: [{
        //   name: 'lvl4'
        // }]
      }]
    }]
  })
}

run()

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: v16.13.2, v16.14.2 and v17.8.0
Mongoose: 6.2.8 (no bug before)
MongoDB: v4.4.5

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 28, 2022
@vkarpov15 vkarpov15 added this to the 6.2.11 milestone Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants