From d22abaf1a51197de95a2c4ba1391ee6b997590c7 Mon Sep 17 00:00:00 2001 From: Maksim Chetverikov Date: Wed, 18 Dec 2013 16:44:31 +0900 Subject: [PATCH] fix issue #1834 --- lib/document.js | 2 ++ test/errors.validation.test.js | 40 ++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/document.js b/lib/document.js index a3152e0e553..bded0121712 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1013,6 +1013,8 @@ Document.prototype.invalidate = function (path, err, val) { err = new ValidatorError(path, err, 'user defined', val) } + if (this.$__.validationError == err) return; + this.$__.validationError.errors[path] = err; } diff --git a/test/errors.validation.test.js b/test/errors.validation.test.js index e94ffa607e2..4cd12a9b315 100644 --- a/test/errors.validation.test.js +++ b/test/errors.validation.test.js @@ -14,16 +14,48 @@ var assert = require('assert') , ValidationError = mongoose.Document.ValidationError describe('ValidationError', function(){ + describe('#infiniteRecursion', function(){ + it('does not cause RangeError (gh-1834)', function(done){ + var SubSchema + , M + , model; + + SubSchema = new Schema({ + name: {type: String, required: true}, + contents: [new Schema({ + key: {type: String, required: true}, + value: {type: String, required: true} + }, {_id: false})] + }); + + M = mongoose.model('SubSchema', SubSchema); + + model = new M({ + name: 'Model', + contents: [ + { key: 'foo' } + ] + }); + + model.validate(function(err){ + assert.doesNotThrow(function(){ + JSON.stringify(err); + }); + done(); + }); + }) + }); + 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; @@ -31,9 +63,9 @@ describe('ValidationError', function(){ m.validate(function (err) { assert.doesNotThrow(function(){ String(err) - }) + }); done(); }); }) }) -}) +});