Skip to content

ValidationError doesn't show what validation failed #3064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MichaelJCole opened this issue Jun 10, 2015 · 4 comments
Closed

ValidationError doesn't show what validation failed #3064

MichaelJCole opened this issue Jun 10, 2015 · 4 comments
Milestone

Comments

@MichaelJCole
Copy link

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 #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;
  }
});
@vkarpov15
Copy link
Collaborator

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

@MichaelJCole
Copy link
Author

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
#3064 (comment)
.

Michael Cole
http://Powma.com

@ksikka
Copy link

ksikka commented May 10, 2016

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

@ksikka
Copy link

ksikka commented May 27, 2017

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants