Mongoose: ValidationError doesn't show what validation failed

Created on 10 Jun 2015  Â·  4Comments  Â·  Source: Automattic/mongoose

with .validate() and .save(), I'm getting the same error.

This doesn't explain why or where the validation failed. Rather than a guessing game, it would be nice to have some information on what was invalid. Path? Hook?

Thanks!

  m:import ValidationError: Topic validation failed
    at model.Document.invalidate (/home/michael/app/node_modules/mongoose/lib/document.js:1156:32)
    at /home/michael/app/node_modules/mongoose/lib/document.js:1031:16
    at validate (/home/michael/app/node_modules/mongoose/lib/schematype.js:651:7)
    at /home/michael/app/node_modules/mongoose/lib/schematype.js:679:9
    at Array.forEach (native)
    at SchemaString.SchemaType.doValidate (/home/michael/app/node_modules/mongoose/lib/schematype.js:656:19)
    at /home/michael/app/node_modules/mongoose/lib/document.js:1029:9
    at process._tickCallback (node.js:355:11)

See https://github.com/Automattic/mongoose/issues/2135

This was caused by this code:

schema.virtual('modifiedBy').set(function (userId) {
  userId = Schema.Types.ObjectId(userId);          //  <-- this probably doesn't do what I think it does
  if (this.isNew) {
    this.createdAt = this.updatedAt = new Date;
    this.createdBy = this.updatedBy = userId;
  } else {
    this.updatedAt = new Date;
    this.updatedBy = userId;
  }
});

Most helpful comment

I think this issue should be re-opened.

While it's definitely useful for the errors property to contain more information, it doesn't make application error logs any less confusing and difficult to triage if that information isn't outputted in the stack trace. For now, every user of mongoose will find a cryptic error message, find this issue or the ValidationError docs, write their own ValidationError handling code to extract the data out of .errors, and try to reproduce their error again. Instead, mongoose should provide a default .toString() that surfaces some of the information in .errors so that developers don't have to go through this pain.

A sensible default might be to print the first error and if there are more than 1 errors, print out how many more there are after that.

Examples of useful errors:

  • Topic.name: Expected String but got Null
  • Topic.createdAt: Required Field is missing
  • Topic.description: Value exceeded max length of 30

All 4 comments

The error object has an errors property that contains a map from path names to validation errors. Try doing a require('util').inspect(error); and you should see it. Also see validation docs

Hey Valeri, thanks for the tip!

On Thu, Jun 11, 2015 at 12:52 PM, Valeri Karpov [email protected]
wrote:

The error object has an errors property that contains a map from path
names to validation errors. Try doing a require('util').inspect(error);
and you should see it. See validation docs
http://mongoosejs.com/docs/validation.html

—
Reply to this email directly or view it on GitHub
https://github.com/Automattic/mongoose/issues/3064#issuecomment-111222145
.

Michael Cole
http://Powma.com

I think this issue should be re-opened.

While it's definitely useful for the errors property to contain more information, it doesn't make application error logs any less confusing and difficult to triage if that information isn't outputted in the stack trace. For now, every user of mongoose will find a cryptic error message, find this issue or the ValidationError docs, write their own ValidationError handling code to extract the data out of .errors, and try to reproduce their error again. Instead, mongoose should provide a default .toString() that surfaces some of the information in .errors so that developers don't have to go through this pain.

A sensible default might be to print the first error and if there are more than 1 errors, print out how many more there are after that.

Examples of useful errors:

  • Topic.name: Expected String but got Null
  • Topic.createdAt: Required Field is missing
  • Topic.description: Value exceeded max length of 30

Awesome

Was this page helpful?
0 / 5 - 0 ratings