Skip to content

fix(query): throw cast errors and respect strict option in replaceOne() - #16629

Merged
vkarpov15 merged 3 commits into
Automattic:masterfrom
OmBhandwaldar:fix/replaceone-cast-errors
Nov 11, 2026
Merged

vkarpov15 merged 3 commits into
Automattic:masterfrom
OmBhandwaldar:fix/replaceone-cast-errors

Conversation

@OmBhandwaldar

@OmBhandwaldar OmBhandwaldar commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #16628.

replaceOne() creates the replacement with new this.model(...), but cast errors from that document were only reported with runValidators: 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's strict option to the replacement document, and throw $__.validationError when runValidators isn't set. With runValidators: true, cast errors were already thrown by validate(), so only the strict change applies there.

Behavior changes:

  • A value that can't be cast now gives a ValidationError (with the CastError in errors) instead of being dropped.
  • The query-level strict option now applies. strict: false keeps unknown paths, and strict: 'throw' gives a ValidationError with the StrictModeError in errors, like findOneAndReplace(). With strict: 'throw' in the schema, replaceOne() used to throw a bare StrictModeError; that's now wrapped the same way.

Examples

// schema: { name: String, price: Number }
await Product.replaceOne({ _id: product._id }, { name: 'Lamp', price: 'abc' });
// before: resolves, stored doc is { name: 'Lamp' }
// after:  throws ValidationError, stored doc is unchanged

Tests in test/model.updateOne.test.js: the cast error, strict: 'throw' / strict: false on the query, and strict: 'throw' in the schema. They fail on master and pass with this change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 runValidators is disabled.
  • Pass query-level strict options 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 vkarpov15 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment but overall looks good

Comment thread lib/query.js Outdated
if ('strict' in this._mongooseOptions) {
modelOpts.strict = this._mongooseOptions.strict;
}
this[queryUpdateSymbol] = new this.model(this[queryUpdateSymbol], null, modelOpts);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

OmBhandwaldar and others added 2 commits November 2, 2026 22:36
…ndReplace

Refactor validation and BSON conversion logic in query update.
@vkarpov15 vkarpov15 added this to the 9.12.2 milestone Nov 11, 2026

@vkarpov15 vkarpov15 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a quick refactor to more closely match _findOneAndReplace but LGTM overall thanks 👍

@vkarpov15
vkarpov15 merged commit 3d49853 into Automattic:master Nov 11, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

replaceOne() silently removes a path when its value fails to cast

3 participants