fix(map): validate map subdocument when loaded with init#15960
fix(map): validate map subdocument when loaded with init#15960
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where validation silently passes for invalid nested map subdocuments when documents are loaded from the database via findOne() or similar methods. The issue occurred because _getPathsToValidate() has an optimization that removes paths with no validators, and SchemaMap itself has no direct validators. Additionally, SchemaMap lacked a doValidate method to validate subdocuments within the map.
Changes:
- Added
doValidatemethod to SchemaMap to validate subdocuments in map values - Modified
_getPathsToValidate()to skip the validator optimization for SchemaMap types - Added test case to verify nested map subdocument validation after initialization
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| lib/schema/map.js | Added doValidate method to validate subdocuments in map values when the map contains schemas with $isSingleNested flag |
| lib/document.js | Modified validation path optimization to exclude SchemaMap types, ensuring maps are always validated even without direct validators |
| test/types.map.test.js | Added test case for validating nested map subdocuments loaded via init() with invalid data |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
487e145 to
459d64f
Compare
Fixes #15957
When a document with nested Maps is loaded from the db via
findOne()etc.,validate()silently passes even for invalid data.The issue is in
_getPathsToValidate(), which has an optimization that removes paths with no validators. SchemaMap has no direct validators, so map paths were getting removed entirely before validation could recurse into subdocuments.The reason constructor-created docs worked fine is they track paths like
myMap.someKeyinactivePaths.modify, which gets validated through SchemaSubdocument. But init only tracksmyMapinactivePaths.init, which was getting skipped by the optimization.Fix: skip the optimization for SchemaMap paths. The existing
$*schema path (SchemaSubdocument) already handles recursive validation of map entries and their nested subdocuments, so no additionaldoValidatemethod was needed.