MCPcopy Create free account
hub / github.com/Automattic/mongoose / _addArrayPathsToValidate

Function _addArrayPathsToValidate

lib/document.js:3292–3338  ·  view source on GitHub ↗
(doc, paths, doValidateOptions)

Source from the content-addressed store, hash-verified

3290}
3291
3292function _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
3340function _pushNestedArrayPaths(val, paths, path) {
3341 if (val != null) {

Callers 1

_getPathsToValidateFunction · 0.85

Calls 2

_pushNestedArrayPathsFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected