Skip to content

Commit

Permalink
fix(schema): treat creating dotted path with no parent as creating a …
Browse files Browse the repository at this point in the history
…nested path

Fix #9020
  • Loading branch information
vkarpov15 committed May 16, 2020
1 parent c7dec63 commit 59f0024
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,21 +656,24 @@ Schema.prototype.path = function(path, obj) {
const subpaths = path.split(/\./);
const last = subpaths.pop();
let branch = this.tree;
let fullPath = '';

subpaths.forEach(function(sub, i) {
for (const sub of subpaths) {
fullPath = fullPath += (fullPath.length > 0 ? '.' : '') + sub;
if (!branch[sub]) {
this.nested[fullPath] = true;
branch[sub] = {};
}
if (typeof branch[sub] !== 'object') {
const msg = 'Cannot set nested path `' + path + '`. '
+ 'Parent path `'
+ subpaths.slice(0, i).concat([sub]).join('.')
+ fullPath
+ '` already set to type ' + branch[sub].name
+ '.';
throw new Error(msg);
}
branch = branch[sub];
});
}

branch[last] = utils.clone(obj);

Expand Down

0 comments on commit 59f0024

Please sign in to comment.