Skip to content

fix(map): validate map subdocument when loaded with init#15960

Merged
vkarpov15 merged 5 commits intomasterfrom
fix/gh-15957-nested-map-validation
Jan 16, 2026
Merged

fix(map): validate map subdocument when loaded with init#15960
vkarpov15 merged 5 commits intomasterfrom
fix/gh-15957-nested-map-validation

Conversation

@AbdelrahmanHafez
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez commented Jan 12, 2026

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.someKey in activePaths.modify, which gets validated through SchemaSubdocument. But init only tracks myMap in activePaths.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 additional doValidate method was needed.

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 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 doValidate method 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.

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

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.

@AbdelrahmanHafez AbdelrahmanHafez marked this pull request as ready for review January 13, 2026 22:05
@AbdelrahmanHafez AbdelrahmanHafez force-pushed the fix/gh-15957-nested-map-validation branch from 487e145 to 459d64f Compare January 13, 2026 22:06
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@vkarpov15 vkarpov15 added this to the 9.1.5 milestone Jan 16, 2026
@vkarpov15 vkarpov15 merged commit d41fb49 into master Jan 16, 2026
50 checks passed
@vkarpov15 vkarpov15 deleted the fix/gh-15957-nested-map-validation branch January 16, 2026 11:11
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.

[Bug] Validation silently passes for nested Map subdocuments loaded via init()

3 participants