Skip to content

fix: fix validation for deeply nested maps of subdocuments #15469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025
Merged

Conversation

AbdelrahmanHafez
Copy link
Collaborator

This PR fixes #15447

The issue was that mongoose was double validating nested maps of subdocuments, once as the correct path, but then trying to validate .$* which represents the map schema rather than the actual value of a path.

The bug was in the _getPathsToValidate function in lib/document.js. We were incorrectly treating map schema paths (paths ending with .$*) as actual document paths for validation. These $* paths represent the schema for Maps but are not actual document paths that should be validated.

This PR ignores the .$* "path" when getting paths to validate.

@AbdelrahmanHafez AbdelrahmanHafez requested review from Copilot, hasezoey and vkarpov15 and removed request for Copilot, hasezoey and vkarpov15 June 9, 2025 02:37
@AbdelrahmanHafez AbdelrahmanHafez added this to the 8.15.2 milestone Jun 9, 2025
Copy link
Contributor

@Copilot 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 issue #15447 by preventing double validation of nested maps in subdocuments.

  • Updated _getPathsToValidate to skip paths ending with ".$*" (map schemas).
  • Added two tests in test/types.map.test.js to validate deeply nested maps with various nesting depths.

Reviewed Changes

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

File Description
test/types.map.test.js Added tests to verify proper validation behavior for nested map subdocuments.
lib/document.js Modified _getPathsToValidate to ignore '.$*' paths representing map schemas.

@AbdelrahmanHafez AbdelrahmanHafez requested a review from hasezoey June 9, 2025 02:41
@AbdelrahmanHafez
Copy link
Collaborator Author

Note to self:
Add a test case to verify that validations inside the map itself still error out when fields fail validation.

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.

This is a reasonable fix. I was looking into this and hoping to find a further upstream fix where this path wasn't modified, but this fix works.

@vkarpov15 vkarpov15 merged commit 9aaee24 into master Jun 9, 2025
71 checks passed
@hasezoey hasezoey deleted the gh-15447 branch June 9, 2025 17:02
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.

Validation failing on present required elements
3 participants