Fixed using hyphens in the path of custom validation errors (#665)

* Fixed a bug where using hyphens in the path for custom validatons error didn't work

* Added closing bracket check and unit tests

* Automatically add brackets when path component contains hyphens

* use regexp to check if dot notation is safe
This commit is contained in:
tobiasfriden 2019-03-21 21:27:17 +01:00 committed by Jos de Jong
parent 0fbe7eee9a
commit 4763bdf293
3 changed files with 29 additions and 31 deletions

41
package-lock.json generated
View File

@ -1699,8 +1699,7 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"aproba": {
"version": "1.2.0",
@ -1721,14 +1720,12 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -1743,20 +1740,17 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@ -1873,8 +1867,7 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"ini": {
"version": "1.3.5",
@ -1886,7 +1879,6 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -1901,7 +1893,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -1909,14 +1900,12 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
@ -1935,7 +1924,6 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@ -2016,8 +2004,7 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"object-assign": {
"version": "4.1.1",
@ -2029,7 +2016,6 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@ -2115,8 +2101,7 @@
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
@ -2152,7 +2137,6 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@ -2172,7 +2156,6 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@ -2216,14 +2199,12 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
}
}
},

View File

@ -821,7 +821,13 @@ exports.parsePath = function parsePath(jsonPath) {
exports.stringifyPath = function stringifyPath(path) {
return path
.map(function (p) {
return typeof p === 'number' ? ('[' + p + ']') : ('.' + p);
if (typeof p === 'number'){
return ('[' + p + ']');
} else if(typeof p === 'string' && p.match(/^[A-Za-z0-9_$]+$/)) {
return '.' + p;
} else {
return '["' + p + '"]';
}
})
.join('');
};

View File

@ -97,6 +97,17 @@ describe('util', function () {
describe('jsonPath', function () {
it('should stringify an array of paths', function() {
assert.deepEqual(util.stringifyPath([]), '');
assert.deepEqual(util.stringifyPath(['foo']), '.foo');
assert.deepEqual(util.stringifyPath(['foo', 'bar']), '.foo.bar');
assert.deepEqual(util.stringifyPath(['foo', 2]), '.foo[2]');
assert.deepEqual(util.stringifyPath(['foo', 2, 'bar']), '.foo[2].bar');
assert.deepEqual(util.stringifyPath(['foo', 2, 'bar_baz']), '.foo[2].bar_baz');
assert.deepEqual(util.stringifyPath(['foo', 'prop-with-hyphens']), '.foo["prop-with-hyphens"]');
assert.deepEqual(util.stringifyPath(['foo', 'prop with spaces']), '.foo["prop with spaces"]');
})
it ('should parse a json path', function () {
assert.deepEqual(util.parsePath(''), []);
assert.deepEqual(util.parsePath('.foo'), ['foo']);