@@ -2682,15 +2682,9 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
26822682 }
26832683 }
26842684
2685- // If underneath a document array, may need to re-validate the parent
2686- // array re: gh-6818
2687- if ( _pathType . $parentSchemaDocArray && typeof _pathType . $parentSchemaDocArray . path === 'string' ) {
2688- paths . add ( _pathType . $parentSchemaDocArray . path ) ;
2689- }
2690-
26912685 // Optimization: if primitive path with no validators, or array of primitives
26922686 // with no validators, skip validating this path entirely.
2693- if ( ! _pathType . caster && _pathType . validators . length === 0 ) {
2687+ if ( ! _pathType . caster && _pathType . validators . length === 0 && ! _pathType . $parentSchemaDocArray ) {
26942688 paths . delete ( path ) ;
26952689 } else if ( _pathType . $isMongooseArray &&
26962690 ! _pathType . $isMongooseDocumentArray && // Skip document arrays...
@@ -2777,7 +2771,19 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
27772771
27782772 for ( const path of paths ) {
27792773 const _pathType = doc . $__schema . path ( path ) ;
2780- if ( ! _pathType || ! _pathType . $isSchemaMap ) {
2774+
2775+ if ( ! _pathType ) {
2776+ continue ;
2777+ }
2778+
2779+ // If underneath a document array, may need to re-validate the parent
2780+ // array re: gh-6818. Do this _after_ adding subpaths, because
2781+ // we don't want to add every array subpath.
2782+ if ( _pathType . $parentSchemaDocArray && typeof _pathType . $parentSchemaDocArray . path === 'string' ) {
2783+ paths . add ( _pathType . $parentSchemaDocArray . path ) ;
2784+ }
2785+
2786+ if ( ! _pathType . $isSchemaMap ) {
27812787 continue ;
27822788 }
27832789
0 commit comments