ValidationError string representation shows no relevant details
See original GitHub issueI’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.
- No lines of code in my application are mentioned.
- It doesn’t say what model failed.
- 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)
- Still no mention of what model failed (account is required on a number of my models).
- Still no mention of my application code in the stack trace.
There are a couple of things I think would be helpful:
- Add the
model name
and alisting 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. - 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.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:4
- Comments:18 (4 by maintainers)
Top Results From Across the Web
I don't want drf ValidationError response to be textual. How to ...
This is where your boolean becomes string ( text = force_text(data) part). One way to overcome this is to overwrite the ValidationError ......
Read more >ValidationError Class (System.Workflow.ComponentModel ...
Creates a shallow copy of the current Object. (Inherited from Object). ToString(). Provides a string representation of this instance incorporating the error ...
Read more >Handling Validation Errors - python-jsonschema
When an invalid instance is encountered, a ValidationError will be raised or returned, depending on which method or function is used.
Read more >Form fields - Django documentation
If a Field has required=False and you pass clean() an empty value, then clean() will return a normalized empty value rather than raising...
Read more >Improve Validation Errors with Adaptive Messages
This is pretty much as bad as it gets. The user is just told their input is invalid with no hints as to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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?
@whitecolor not expected behavior. Can you open up a new issue with some code samples please?