ValidationError contains an infinite recursion #1834

Closed
chetverikov opened this Issue Dec 17, 2013 · 2 comments

Projects

None yet

2 participants

@chetverikov
Contributor

Problem is similar to the one described at Issue #1296 .

Infinite recursion may occur in ValidationError.

A = new Schema({
    name:       {type: String, required: true}
    contents:   [new Schema({
        key:    {type: String, required: true}
        value:  {type: String, required: true}
    }, {_id: false})]
})

Next...

var model = new A({
  name: 'Model',
  contents: [
    { key: 'foo' }
  ]
});
model.validate(function(err){
  console.log(err)
});

The code above causes the error below:

{ message: 'Validation failed',
  name: 'ValidationError',
  errors: 
   { 'contents.0.value': 
      { message: 'Path `value` is required.',
        name: 'ValidatorError',
        path: 'value',
        type: 'required',
        value: undefined },
     contents: [Circular] }, // recursion lives here

  key: 'undefined.0.undefined' }

The problem can be solved by adding the line of code somewhere at line 1016 in file /lib/document.js

Document.prototype.invalidate = function (path, err, val) {
  if (!this.$__.validationError) {
    this.$__.validationError = new ValidationError(this);
  }

  if (!err || 'string' === typeof err) {
    err = new ValidatorError(path, err, 'user defined', val)
  }

  if (this.$__.validationError == err) return; // that's the line that should be added

  this.$__.validationError.errors[path] = err;
}

After this little fix the validation works as expected:

{ message: 'Validation failed',
  name: 'ValidationError',
  errors: 
   { 'contents.0.value': 
      { message: 'Path `value` is required.',
        name: 'ValidatorError',
        path: 'value',
        type: 'required',
        value: undefined } },
  key: 'undefined.0.undefined' }
@aheckmann
Collaborator

Please send a pull request with the fix and a test.

On Monday, December 16, 2013, Maksim wrote:

Problem is similar to the one described at Issue #1296#1296.

Infinite recursion may occur in ValidationError.

A = new Schema({
name: {type: String, required: true}
contents: [new Schema({
key: {type: String, required: true}
value: {type: String, required: true}
}, {_id: false})]})

The code above causes the error below:

{ message: 'Validation failed',
name: 'ValidationError',
errors:
{ 'contents.0.value':
{ message: 'Path value is required.',
name: 'ValidatorError',
path: 'value',
type: 'required',
value: undefined },
contents: [Circular] }, // recursion lives here

key: 'undefined.0.undefined' }

The problem can be solved by adding the line of code somewhere at line
1016 in file /lib/document.js

Document.prototype.invalidate = function (path, err, val) {
if (!this.$.validationError) {
this.$
.validationError = new ValidationError(this);
}

if (!err || 'string' === typeof err) {
err = new ValidatorError(path, err, 'user defined', val)
}

if (this.$__.validationError == err) return; // that's the line that should be added

this.$__.validationError.errors[path] = err;}

After this little fix the validation works as expected:

{ message: 'Validation failed',
name: 'ValidationError',
errors:
{ 'contents.0.value':
{ message: 'Path value is required.',
name: 'ValidatorError',
path: 'value',
type: 'required',
value: undefined } },
key: 'undefined.0.undefined' }


Reply to this email directly or view it on GitHubhttps://github.com/LearnBoost/mongoose/issues/1834
.

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann
soundcloud.com/ajhecky
github.com/aheckmann

@aheckmann
Collaborator

fixed in 3.8.4

@aheckmann aheckmann closed this Jan 7, 2014
@aheckmann aheckmann pushed a commit that referenced this issue Jan 9, 2014
@chetverikov chetverikov + Aaron Heckmann fix issue #1834 d22abaf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment