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