Skip to content

fix: avoid double-calling validators on document arrays underneath nested paths re: #5411 - #16484

Open
vkarpov15 wants to merge 12 commits into
masterfrom
vkarpov15/paths-to-validate-fix
Open

vkarpov15 wants to merge 12 commits into
masterfrom
vkarpov15/paths-to-validate-fix

Conversation

@vkarpov15

@vkarpov15 vkarpov15 commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

While working on #16291 I spotted an inconsistency in how validate() and validateSync() work on document arrays underneath nested paths. In master, switching the test from #5411 to use validate() instead of validateSync() fails: validate() still double-runs validators on nested document arrays, validateSync() does not.

I fixed that by avoiding adding the array path in _getPathsToValidate() in the nested path case and adding a consistent doValidateSync() override for SchemaDocumentArrayElement analogous to the override for doValidate().

That also turned up some related issues when testing validate() in parallel with validateSync() in some related tests that errored while I was working on the original problem related to skipSchemaValidators: with the new behavior, document arrays no longer need skipSchemaValidators to avoid double-calling schema validators. Still necessary for subdocuments though.

Further work in b91495f and 4443584 ended up as a full rewrite of _getPathsToValidate() focusing on simplification and performance improvements. Existing _getPathsToValidate() added unnecessary paths and then cleaned them up with an o(n^2) loop that always bugged me. It turns out this loop was only necessary for two cases that we can handle separately: arr.0 (handled by the new covered check) and arr.0.subprop (handled by _isValidatedBySubdoc). The new _getPathsToValidate() looks to be significantly faster as well as simpler.

Examples

@vkarpov15
vkarpov15 requested review from AbdelrahmanHafez and hasezoey and a lite review from Copilot August 30, 2026 20:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vkarpov15 vkarpov15 added this to the 9.9.4 milestone Sep 4, 2026
@vkarpov15 vkarpov15 removed this from the 9.9.4 milestone Sep 4, 2026
@vkarpov15
vkarpov15 marked this pull request as draft September 4, 2026 17:58
@vkarpov15

Copy link
Copy Markdown
Collaborator Author

Converting this to a draft because there's a lot of details and nuance to investigate here. _getPathsToValidate() has got a lot of small inconsistencies and hacks that should be cleaned up.

@vkarpov15
vkarpov15 marked this pull request as ready for review September 19, 2026 23:11
@vkarpov15
vkarpov15 requested a lite review from Copilot September 19, 2026 23:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The validation-path issues, including one critical finding, must be addressed before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Low severity

Open (2)

Comment thread lib/document.js
Comment on lines +3285 to +3289
const parentDocArray = type.$parentSchemaDocArray;
if (parentDocArray != null) {
const arrayPath = parentDocArray.path;
if (typeof arrayPath === 'string' && schemaPaths[arrayPath] === parentDocArray &&
covered[arrayPath] !== true) {
Comment thread test/types.documentarray.test.js Outdated
vkarpov15 and others added 3 commits September 19, 2026 19:19
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ic/mongoose into vkarpov15/paths-to-validate-fix
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