fix(schema): treat undefined as not provided for strict, strictQuery and id options#16004
Open
AbdelrahmanHafez wants to merge 3 commits intomasterfrom
Open
fix(schema): treat undefined as not provided for strict, strictQuery and id options#16004AbdelrahmanHafez wants to merge 3 commits intomasterfrom
undefined as not provided for strict, strictQuery and id options#16004AbdelrahmanHafez wants to merge 3 commits intomasterfrom
Conversation
Contributor
There was a problem hiding this comment.
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
defaultOptionsfunction to use nullish coalescing (??) for extracting defaults frombaseOptions - Added nullish coalescing assignment after the spread operator to treat
undefinedas "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 |
hasezoey
approved these changes
Feb 2, 2026
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.
When creating a schema with
{ strict: undefined }, thestrictoption was staying asundefinedinstead of defaulting totrue(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:
Fixed by adding nullish coalescing after the spread to treat
undefinedas "not provided" forstrict,strictQuery, andidoptions.