@@ -2684,7 +2684,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
2684
2684
2685
2685
// Optimization: if primitive path with no validators, or array of primitives
2686
2686
// 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 ) {
2688
2688
paths . delete ( path ) ;
2689
2689
} else if ( _pathType . $isMongooseArray &&
2690
2690
! _pathType . $isMongooseDocumentArray && // Skip document arrays...
@@ -2771,7 +2771,19 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
2771
2771
2772
2772
for ( const path of paths ) {
2773
2773
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 ) {
2775
2787
continue ;
2776
2788
}
2777
2789
@@ -3318,14 +3330,7 @@ Document.prototype.$__reset = function reset() {
3318
3330
if ( this . isModified ( fullPathWithIndexes ) || isParentInit ( fullPathWithIndexes ) ) {
3319
3331
subdoc . $__reset ( ) ;
3320
3332
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 ( ) ) ;
3329
3334
} else {
3330
3335
if ( subdoc . $parent ( ) === this ) {
3331
3336
this . $__ . activePaths . clearPath ( subdoc . $basePath ) ;
@@ -3338,6 +3343,12 @@ Document.prototype.$__reset = function reset() {
3338
3343
}
3339
3344
}
3340
3345
3346
+ for ( const array of resetArrays ) {
3347
+ this . $__ . activePaths . clearPath ( array . $path ( ) ) ;
3348
+ array [ arrayAtomicsBackupSymbol ] = array [ arrayAtomicsSymbol ] ;
3349
+ array [ arrayAtomicsSymbol ] = { } ;
3350
+ }
3351
+
3341
3352
function isParentInit ( path ) {
3342
3353
path = path . indexOf ( '.' ) === - 1 ? [ path ] : path . split ( '.' ) ;
3343
3354
let cur = '' ;
0 commit comments