fix(query): replace {MODEL} in cast error messages from update and bulkWrite() casting - #16529
Open
SulimanAbdulrazzaq wants to merge 1 commit into
Open
SulimanAbdulrazzaq wants to merge 1 commit into
SulimanAbdulrazzaq wants to merge 1 commit into
Conversation
…lkWrite() casting
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Some update paths bypass the new handling, and discriminator updates can report the wrong model.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds model-aware formatting to update and bulkWrite() cast errors.
Changes:
- Assigns models to update cast errors.
- Adds model-aware bulk-write filter casting.
- Tests query and bulk-write error messages.
| File | Description |
|---|---|
lib/helpers/query/castUpdate.js |
Sets models on collected update cast errors. |
lib/helpers/model/castBulkWrite.js |
Wraps bulk-write filter casting. |
test/schema.test.js |
Tests {MODEL} substitution. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
501
to
+505
| function _appendError(error, query, key, aggregatedError) { | ||
| // Set the model on cast errors so `{MODEL}` gets replaced, like it is for | ||
| // query filter casting and document validation (gh-8300). `query` is a | ||
| // Query, or the model itself when casting a `bulkWrite()` operation. | ||
| const model = query?.modelName != null ? query : query?.model; |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
{MODEL}in a customcasterror message is replaced when casting query filters, on document validation (#16480) and incastObject()(#16502), but not when casting updates.updateOne(),updateMany()andfindOneAndUpdate()report the literal{MODEL}, and the default cast error message for updates leaves out thefor model "..."suffix that filter cast errors have.bulkWrite()has the same gap for both its update and filter casting, since it callscastUpdate()andcast()directly instead of going throughQuery.This PR:
castUpdate()'s_appendError(), which every update cast error goes through. It's done before the error is thrown or added to themultipleCastErrorValidationError, so the aggregated error message is also correct. The context there is theQuery, or the model itself forbulkWrite().bulkWrite()filters through a small helper that sets the model on cast errors, likeQuery.prototype._castConditions()does.Examples
Added a test in
test/schema.test.jsnext to the other{MODEL}tests. It coversupdateOne(),updateMany(),findOneAndUpdate(),multipleCastError, andbulkWrite()updates and filters.