Skip to content

Array validator receives an object on $pull #6889

@sebastian-nowak

Description

@sebastian-nowak

I'm not sure if it's a bug or just a very surprising behavior, but here we go:

new mongoose.Schema({
  arrayOfDocuments: {
    type: [{
      ...
    }],
    validate: {
      validator: v => _.uniqBy(_.map(v, '_id'), id => id.toString()).length === v.length,
      message: 'Ids have to be unique.'
     }
  }
})

Now let's run an update query on this model:

// validators are set to run on update
await SomeModel.findOneAndUpdate(
  {_id: 'some object id'},
  {$pull: {arrayOfDocuments: {_id: 'another object id'}}}
);

The above validator will be run with {_id: 'another object id'} as an argument - which isn't an array. I don't think this is valid, it's trying to validate a subset of document properties by passing it to an array validator.

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