-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat: add index and doc to insertMany validation errors #15735
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
feat: add index and doc to insertMany validation errors #15735
Conversation
|
Hey @vkarpov15 @hasezoey pls review this PR. |
hasezoey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
hasezoey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still looks good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the insertMany() method to add an index property to validation errors when using { ordered: false }. The addition helps developers identify which document in the input array failed validation, aligning with MongoDB's native error reporting pattern.
Key changes:
- Added
indexproperty toValidationErrortype definition - Modified validation error handling to set the
indexproperty in unordered inserts - Added test coverage for the new
indexproperty in two scenarios
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| types/error.d.ts | Added optional index property to ValidationError type definition with JSDoc |
| lib/model.js | Set error.index during validation error handling for unordered inserts |
| test/model.insertMany.test.js | Added two test cases verifying index property with rawResult and throwOnValidationError options |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
vkarpov15
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
This PR addresses feature request #14351 and introduces an enhancement for the insertMany() method when used with
{ ordered: false }It adds two new properties to validation errors:
error.index: the index of the failed document in the input arrayThese additions align Mongoose’s error reporting more closely with MongoDB’s native write errors and make debugging bulk inserts easier.
Problem
Currently, when using
insertMany()with{ ordered: false, rawResult: true }, validation errors lack:Without this, it’s difficult to identify and retry failed documents in production.
Solution
Modified
Model.$__insertMany()to add two properties to validation errors whenordered: false:error.index: The position of the failing document in the original arrayBefore:
After:
Testing
indexproperty matches the array positionrawResultandthrowOnValidationErroroptionsfixes #14351