mongoose: ValidationError string representation shows no relevant details

I’m getting a ValidationError which looks like this:

ValidationError: Validation failed
  at model.Document.invalidate (.../project/node_modules/mongoose/lib/document.js:990:32)
  at .../project/node_modules/mongoose/lib/document.js:959:16
  at validate (.../project/node_modules/mongoose/lib/schematype.js:561:7)
  at .../project/node_modules/mongoose/lib/schematype.js:577:9
  at Array.forEach (native)
  at ObjectId.SchemaType.doValidate (.../project/node_modules/mongoose/lib/schematype.js:565:19)
  at .../project/node_modules/mongoose/lib/document.js:957:9
  at process._tickCallback (node.js:415:13)

The problem is there’s no useful information contained in the error.

  1. No lines of code in my application are mentioned.
  2. It doesn’t say what model failed.
  3. It doesn’t say what field or value failed.

I hunted down the error, and I see it has an ‘errors’ subobject:

ValidatorError: Path `account` is required.
  at validate (.../project/node_modules/mongoose/lib/schematype.js:610:16)
  at .../project/node_modules/mongoose/lib/schematype.js:627:9
  at Array.forEach (native)
  at ObjectId.SchemaType.doValidate (.../project/node_modules/mongoose/lib/schematype.js:614:19)
  at .../project/node_modules/mongoose/lib/document.js:956:9
  at process._tickCallback (node.js:415:13)
  1. Still no mention of what model failed (account is required on a number of my models).
  2. Still no mention of my application code in the stack trace.

There are a couple of things I think would be helpful:

  1. Add the model name and a listing of fields that errored to the main error message. This way it will print out useful information by default, without requiring the consumer to create custom logic to pull out the details.
  2. Is it possible to create a dummy error object when the initial create or save request is made, and then use the stack trace if any errors occur? Just throwing out ideas here, but anything that could show me the entry point into your library in the trace would be very helpful, as that’s the line of code I actually need to change.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 4
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Concrete example of where more information would be helpful: I wrote a job that gets data from an API and creates mongoose models out of the data. After running it, I got:

ValidationError: Client validation failed

I didn’t know why it failed. I looked at the stack trace to see exactly at which line it failed - line 139 - and navigated to that line of code.

client.lastName = remoteClient.LastName;

Why would this fail? I looked at the model, lastName expects a string. It must be the case that the value assigned to the field was not a string. But then what was it? I had to run my script again with extra logging to determine it was a number.

This could have all been avoided if the error message read:

ValidationError: Client validation failed: Expected string for lastName, but got number instead.

As for multiple errors, just append (N more errors...) to the message.

Thoughts?