Skip to content

Commit

Permalink
fix(schema): avoid creating unnecessary subpaths for array elements t…
Browse files Browse the repository at this point in the history
…o avoid `subpaths` growing without bound

Re: #13874
  • Loading branch information
vkarpov15 committed Oct 6, 2023
1 parent b721f54 commit 872c4be
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ Schema.prototype.path = function(path, obj) {

// subpaths?
return /\.\d+\.?.*$/.test(path)
? getPositionalPath(this, path)
? getPositionalPath(this, path, cleanPath)
: undefined;
}

Expand Down Expand Up @@ -1634,7 +1634,7 @@ Schema.prototype.pathType = function(path) {
}

if (/\.\d+\.|\.\d+$/.test(path)) {
return getPositionalPathType(this, path);
return getPositionalPathType(this, path, cleanPath);
}
return 'adhocOrUndefined';
};
Expand Down Expand Up @@ -1678,7 +1678,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
* @api private
*/

function getPositionalPathType(self, path) {
function getPositionalPathType(self, path, cleanPath) {
const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
if (subpaths.length < 2) {
return self.paths.hasOwnProperty(subpaths[0]) ?
Expand Down Expand Up @@ -1729,7 +1729,7 @@ function getPositionalPathType(self, path) {
val = val.schema.path(subpath);
}

self.subpaths[path] = val;
self.subpaths[cleanPath] = val;
if (val) {
return 'real';
}
Expand All @@ -1744,9 +1744,9 @@ function getPositionalPathType(self, path) {
* ignore
*/

function getPositionalPath(self, path) {
getPositionalPathType(self, path);
return self.subpaths[path];
function getPositionalPath(self, path, cleanPath) {
getPositionalPathType(self, path, cleanPath);
return self.subpaths[cleanPath];
}

/**
Expand Down Expand Up @@ -2638,6 +2638,9 @@ Schema.prototype._getSchema = function(path) {
// Re: gh-5628, because `schema.path()` doesn't take $ into account.
parts[i] = '0';
}
if (/^\d+$/.test(parts[i])) {
parts[i] = '$';
}
}
return search(parts, _this);
};
Expand Down

0 comments on commit 872c4be

Please sign in to comment.