Skip to content

Commit

Permalink
fixed; RangeError in ValidationError.toString()
Browse files Browse the repository at this point in the history
closes #1296
  • Loading branch information
aheckmann committed Apr 3, 2013
1 parent 580cf88 commit ed12f5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/errors/validation.js
Expand Up @@ -25,9 +25,15 @@ function ValidationError (instance) {
*/

ValidationError.prototype.toString = function () {
return this.name + ': ' + Object.keys(this.errors).map(function (key) {
return String(this.errors[key]);
}, this).join(', ');
var ret = this.name + ': ';
var msgs = [];

Object.keys(this.errors).forEach(function (key) {
if (this == this.errors[key]) return;
msgs.push(String(this.errors[key]));
}, this)

return ret + msgs.join(', ');
};

/*!
Expand Down
39 changes: 39 additions & 0 deletions test/errors.validation.test.js
@@ -0,0 +1,39 @@

/**
* Module dependencies.
*/

var assert = require('assert')
, start = require('./common')
, mongoose = start.mongoose
, EmbeddedDocument = require('../lib/types/embedded')
, DocumentArray = require('../lib/types/documentarray')
, Schema = mongoose.Schema
, SchemaType = mongoose.SchemaType
, ValidatorError = SchemaType.ValidatorError
, ValidationError = mongoose.Document.ValidationError

describe('ValidationError', function(){
describe('#toString', function(){
it('does not cause RangeError (gh-1296)', function(done){
var ASchema = new Schema({
key: {type: String, required: true}
, value: {type:String, required: true}
})

var BSchema = new Schema({
contents: [ASchema]
})

var M = mongoose.model('A', BSchema);
var m = new M;
m.contents.push({ key: 'asdf' });
m.validate(function (err) {
assert.doesNotThrow(function(){
String(err)
})
done();
});
})
})
})

0 comments on commit ed12f5d

Please sign in to comment.