Skip to content

Commit 872c4be

Browse files
committed
fix(schema): avoid creating unnecessary subpaths for array elements to avoid subpaths growing without bound
Re: #13874
1 parent b721f54 commit 872c4be

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/schema.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ Schema.prototype.path = function(path, obj) {
10171017

10181018
// subpaths?
10191019
return /\.\d+\.?.*$/.test(path)
1020-
? getPositionalPath(this, path)
1020+
? getPositionalPath(this, path, cleanPath)
10211021
: undefined;
10221022
}
10231023

@@ -1634,7 +1634,7 @@ Schema.prototype.pathType = function(path) {
16341634
}
16351635

16361636
if (/\.\d+\.|\.\d+$/.test(path)) {
1637-
return getPositionalPathType(this, path);
1637+
return getPositionalPathType(this, path, cleanPath);
16381638
}
16391639
return 'adhocOrUndefined';
16401640
};
@@ -1678,7 +1678,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
16781678
* @api private
16791679
*/
16801680

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

1732-
self.subpaths[path] = val;
1732+
self.subpaths[cleanPath] = val;
17331733
if (val) {
17341734
return 'real';
17351735
}
@@ -1744,9 +1744,9 @@ function getPositionalPathType(self, path) {
17441744
* ignore
17451745
*/
17461746

1747-
function getPositionalPath(self, path) {
1748-
getPositionalPathType(self, path);
1749-
return self.subpaths[path];
1747+
function getPositionalPath(self, path, cleanPath) {
1748+
getPositionalPathType(self, path, cleanPath);
1749+
return self.subpaths[cleanPath];
17501750
}
17511751

17521752
/**
@@ -2638,6 +2638,9 @@ Schema.prototype._getSchema = function(path) {
26382638
// Re: gh-5628, because `schema.path()` doesn't take $ into account.
26392639
parts[i] = '0';
26402640
}
2641+
if (/^\d+$/.test(parts[i])) {
2642+
parts[i] = '$';
2643+
}
26412644
}
26422645
return search(parts, _this);
26432646
};

0 commit comments

Comments
 (0)