Skip to content

Mongoose runs array-level validators on the array and its elements during updates #4440

@zbjornson

Description

@zbjornson

When updating a model, the validators are run on on both the whole array and its elements. #2618 reported this same problem during document saves.

it.only('doesnt do double validation on document arrays during updates (gh-4440)', function(done) {
  var db = start();

  var A = new Schema({str: String});
  var B = new Schema({a: [A]});
  var validateCalls = 0;
  B.path('a').validate(function(val, next) {
    ++validateCalls;
    assert(Array.isArray(val));
    next();
  });

  B = db.model('b', B);

  B.findOneAndUpdate(
    {foo: 'bar'},
    {$set: {a: [{str: 'asdf'}]}},
    {runValidators: true},
    function(err) {
      assert.ifError(err);
      assert.equal(validateCalls, 1); // Assertion error: 1 == 2
      db.close(done);
    }
  );
});

[email protected]

(My attempts to fix this have all conflicted with a test for #2733.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions