Skip to content

fix(model): throw ObjectParameterError in insertOne() if doc is not an object#16221

Open
IshitaSingh0822 wants to merge 2 commits intoAutomattic:masterfrom
IshitaSingh0822:master
Open

fix(model): throw ObjectParameterError in insertOne() if doc is not an object#16221
IshitaSingh0822 wants to merge 2 commits intoAutomattic:masterfrom
IshitaSingh0822:master

Conversation

@IshitaSingh0822
Copy link
Copy Markdown

What does this PR do?

Model.insertOne() did not validate that the doc argument is an object,
unlike Model.insertMany() which throws an ObjectParameterError for
non-object entries.

This PR adds the same input validation to insertOne() for consistency
and better developer experience.

Problem

When a non-object value (like a string, number, or boolean) was passed
to Model.insertOne(), Mongoose would either silently fail or throw a
cryptic internal error that was hard to debug.

Solution

Added the same ObjectParameterError check that already exists in
insertMany() to insertOne() as well.

Changes

  • lib/model.js — Added ObjectParameterError check at the top of insertOne()
  • test/insertOne.validation.test.js — Added 7 test cases

Why this matters

  • Makes insertOne() and insertMany() behave consistently
  • Gives developers a clear, actionable error message
  • Zero breaking changes

Testing

All 7 new test cases pass. Existing test suite passes with no regressions.
4301 passing, 0 failing.

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.

1 participant