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

Function _addArrayPathsToValidate

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

Source from the content-addressed store, hash-verified

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

Callers 1

_getPathsToValidateFunction · 0.85

Calls 2

_pushNestedArrayPathsFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected