(doc, paths, doValidateOptions)
| 3290 | } |
| 3291 | |
| 3292 | function _addArrayPathsToValidate(doc, paths, doValidateOptions) { |
| 3293 | for (const path of paths) { |
| 3294 | const _pathType = doc.$__schema.path(path); |
| 3295 | if (!_pathType) { |
| 3296 | continue; |
| 3297 | } |
| 3298 | |
| 3299 | if (!_pathType.$isMongooseArray || |
| 3300 | // To avoid potential performance issues, skip doc arrays whose elements |
| 3301 | // have no validators of their own. `getPositionalPathType()` may be |
| 3302 | // slow, so avoid it unless we have a case of #6364 |
| 3303 | (!Array.isArray(_pathType) && |
| 3304 | _pathType.$isMongooseDocumentArray && |
| 3305 | !_pathType.embeddedSchemaType?.validators.length)) { |
| 3306 | continue; |
| 3307 | } |
| 3308 | |
| 3309 | // gh-11380: optimization. If the array isn't a document array and there's no validators |
| 3310 | // on the array type, there's no need to run validation on the individual array elements. |
| 3311 | if (_pathType.$isMongooseArray && |
| 3312 | !_pathType.$isMongooseDocumentArray && // Skip document arrays... |
| 3313 | !_pathType.embeddedSchemaType.$isMongooseArray && // and arrays of arrays |
| 3314 | _pathType.embeddedSchemaType.validators.length === 0) { |
| 3315 | continue; |
| 3316 | } |
| 3317 | |
| 3318 | const val = doc.$__getValue(path); |
| 3319 | if (_pathType.$isMongooseDocumentArray) { |
| 3320 | // We only get here because the document array itself is being validated, |
| 3321 | // which validates every subdocument. Leave the subdocuments to it and run |
| 3322 | // just the element-level validators here, so nothing validates twice. |
| 3323 | const numElements = val?.length ?? 0; |
| 3324 | for (let j = 0; j < numElements; ++j) { |
| 3325 | const elementPath = path + '.' + j; |
| 3326 | paths.add(elementPath); |
| 3327 | doValidateOptions[elementPath] = { |
| 3328 | ...doValidateOptions[path], |
| 3329 | ...doValidateOptions[elementPath], |
| 3330 | skipSubdocumentValidation: true, |
| 3331 | index: j |
| 3332 | }; |
| 3333 | } |
| 3334 | continue; |
| 3335 | } |
| 3336 | _pushNestedArrayPaths(val, paths, path); |
| 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | function _pushNestedArrayPaths(val, paths, path) { |
| 3341 | if (val != null) { |
no test coverage detected