@@ -2684,7 +2684,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
26842684
26852685 // Optimization: if primitive path with no validators, or array of primitives
26862686 // with no validators, skip validating this path entirely.
2687- if ( ! _pathType . caster && _pathType . validators . length === 0 ) {
2687+ if ( ! _pathType . caster && _pathType . validators . length === 0 && ! _pathType . $parentSchemaDocArray ) {
26882688 paths . delete ( path ) ;
26892689 } else if ( _pathType . $isMongooseArray &&
26902690 ! _pathType . $isMongooseDocumentArray && // Skip document arrays...
@@ -2771,7 +2771,19 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
27712771
27722772 for ( const path of paths ) {
27732773 const _pathType = doc . $__schema . path ( path ) ;
2774- 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 ) {
27752787 continue ;
27762788 }
27772789
@@ -3318,14 +3330,7 @@ Document.prototype.$__reset = function reset() {
33183330 if ( this . isModified ( fullPathWithIndexes ) || isParentInit ( fullPathWithIndexes ) ) {
33193331 subdoc . $__reset ( ) ;
33203332 if ( subdoc . $isDocumentArrayElement ) {
3321- if ( ! resetArrays . has ( subdoc . parentArray ( ) ) ) {
3322- const array = subdoc . parentArray ( ) ;
3323- this . $__ . activePaths . clearPath ( fullPathWithIndexes . replace ( / \. \d + $ / , '' ) . slice ( - subdoc . $basePath - 1 ) ) ;
3324- array [ arrayAtomicsBackupSymbol ] = array [ arrayAtomicsSymbol ] ;
3325- array [ arrayAtomicsSymbol ] = { } ;
3326-
3327- resetArrays . add ( array ) ;
3328- }
3333+ resetArrays . add ( subdoc . parentArray ( ) ) ;
33293334 } else {
33303335 if ( subdoc . $parent ( ) === this ) {
33313336 this . $__ . activePaths . clearPath ( subdoc . $basePath ) ;
@@ -3338,6 +3343,12 @@ Document.prototype.$__reset = function reset() {
33383343 }
33393344 }
33403345
3346+ for ( const array of resetArrays ) {
3347+ this . $__ . activePaths . clearPath ( array . $path ( ) ) ;
3348+ array [ arrayAtomicsBackupSymbol ] = array [ arrayAtomicsSymbol ] ;
3349+ array [ arrayAtomicsSymbol ] = { } ;
3350+ }
3351+
33413352 function isParentInit ( path ) {
33423353 path = path . indexOf ( '.' ) === - 1 ? [ path ] : path . split ( '.' ) ;
33433354 let cur = '' ;
0 commit comments