Skip to content

Commit

Permalink
Fix #2589: properly collect subdoc validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 3, 2015
1 parent f3fe51c commit 66f1ced
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
SchemaType.prototype.doValidate.call(this, array, function (err) {
if (err) return fn(err);

var count = array && array.length
, error;
var count = array && array.length;
var error;

if (!count) return fn();

Expand All @@ -92,16 +92,16 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
// sidestep sparse entries
var doc = array[i];
if (!doc) {
--count || fn();
--count || fn(errors);
continue;
}

;(function (i) {
doc.validate(function (err) {
if (err && !error) {
return fn(error = err);
if (err) {
error = err;
}
--count || fn();
--count || fn(error);
});
})(i);
}
Expand Down
14 changes: 14 additions & 0 deletions test/schema.validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,5 +754,19 @@ describe('schema', function(){
done();
});
});

it('handles multiple subdocument errors', function(done) {
var foodSchema = new Schema({ name: { type: String, required: true, enum: ['bacon', 'eggs'] } });
var breakfast = new Schema({ foods: [foodSchema] });

var Breakfast = mongoose.model('gh-2589', breakfast, 'gh-2589');
var bad = new Breakfast({ foods: [{ name: 'tofu' }, { name: 'waffles' }] });
bad.validate(function(error) {
assert.ok(error);
assert.ok(error.errors['foods.0.name']);
assert.ok(error.errors['foods.1.name']);
done();
});
});
});
});

0 comments on commit 66f1ced

Please sign in to comment.