Skip to content

fix(schema): treat undefined as not provided for strict, strictQuery and id options#16004

Open
AbdelrahmanHafez wants to merge 3 commits intomasterfrom
fix/default-strict-undefined
Open

fix(schema): treat undefined as not provided for strict, strictQuery and id options#16004
AbdelrahmanHafez wants to merge 3 commits intomasterfrom
fix/default-strict-undefined

Conversation

@AbdelrahmanHafez
Copy link
Collaborator

When creating a schema with { strict: undefined }, the strict option was staying as undefined instead of defaulting to true (or the global mongoose option). This happened because user options are spread onto defaults, and { strict: undefined } would overwrite the default.

This caused issues when using destructuring patterns like:

function createSchema({ strict } = {}) {
  return new Schema({ name: String }, { strict });
}
createSchema(); // strict becomes undefined instead of true

Fixed by adding nullish coalescing after the spread to treat undefined as "not provided" for strict, strictQuery, and id options.

Copy link
Contributor

Copilot AI left a 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 fixes an issue where passing undefined as the value for strict, strictQuery, or id schema options would override the defaults instead of being treated as "not provided". This commonly occurred when using destructuring patterns where an option might be undefined.

Changes:

  • Modified defaultOptions function to use nullish coalescing (??) for extracting defaults from baseOptions
  • Added nullish coalescing assignment after the spread operator to treat undefined as "not provided" for the three affected options
  • Added comprehensive test coverage for all three options with both default and global fallback scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/schema.js Updated defaultOptions function to handle undefined values correctly for strict, strictQuery, and id options using nullish coalescing operators
test/schema.test.js Added 6 new test cases covering default and global fallback behavior for all three affected options when passed as undefined

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.

2 participants