Repository navigation
fix(query): throw cast errors and respect strict option in replaceOne() - #16629
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the replaceOne() casting and query-level strict-option changes.
No open issues 🎉
What changed in this PR
Fixes #16628 by aligning replaceOne() with findOneAndReplace(): invalid replacement values now raise errors rather than silently removing stored fields, and query-level strict settings are respected.
Changes:
- Report replacement document cast errors even when
runValidatorsis disabled. - Pass query-level
strictoptions to the replacement document. - Add regression tests for cast errors, unchanged stored data after failure, and
strict: 'throw'/strict: false.
| File | Description |
|---|---|
| test/model.updateOne.test.js | Adds regression coverage for cast errors and query-level strict options. |
| lib/query.js | Applies strict options and throws replacement validation errors when validators are disabled. |
Review effort: Balanced
vkarpov15
left a comment
There was a problem hiding this comment.
One minor comment but overall looks good
| if ('strict' in this._mongooseOptions) { | ||
| modelOpts.strict = this._mongooseOptions.strict; | ||
| } | ||
| this[queryUpdateSymbol] = new this.model(this[queryUpdateSymbol], null, modelOpts); |
There was a problem hiding this comment.
Slight inconsistency with _findOneAndReplace() here: _findOneAndReplace() wraps new this.model() in a try/catch and converts error results to ValidationError instances. new this.model() can throw a StrictModeError, so this is an inconsistency between replaceOne() and findOneAndReplace().
There was a problem hiding this comment.
Done, replaceOne() now wraps new this.model() in a try/catch and converts other errors to a ValidationError, like findOneAndReplace(). That also covers strict: 'throw' set in the schema, which used to throw a bare StrictModeError. Updated the strict test and added one for the schema-level case.
…Error in replaceOne()
…ndReplace Refactor validation and BSON conversion logic in query update.
vkarpov15
left a comment
There was a problem hiding this comment.
I made a quick refactor to more closely match _findOneAndReplace but LGTM overall thanks 👍
Summary
Fixes #16628.
replaceOne()creates the replacement withnew this.model(...), but cast errors from that document were only reported withrunValidators: true. Otherwise the path that failed to cast was left out and the stored value was removed without an error.This makes
_replaceOne()do what_findOneAndReplace()already does: pass the query'sstrictoption to the replacement document, and throw$__.validationErrorwhenrunValidatorsisn't set. WithrunValidators: true, cast errors were already thrown byvalidate(), so only thestrictchange applies there.Behavior changes:
ValidationError(with theCastErrorinerrors) instead of being dropped.strictoption now applies.strict: falsekeeps unknown paths, andstrict: 'throw'gives aValidationErrorwith theStrictModeErrorinerrors, likefindOneAndReplace(). Withstrict: 'throw'in the schema,replaceOne()used to throw a bareStrictModeError; that's now wrapped the same way.Examples
Tests in
test/model.updateOne.test.js: the cast error,strict: 'throw'/strict: falseon the query, andstrict: 'throw'in the schema. They fail onmasterand pass with this change.