Skip to content

Commit 5bc3f21

Browse files
authored
Merge pull request #1405 from owenconti/bug/1395-dot-in-model-names
Fixes #1395 - Allow model names with "."s
2 parents 219cd4a + 516f0f2 commit 5bc3f21

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"test": "npm run lint-errors && npm run just-test-in-node",
3434
"test-in-node": "npm run lint-errors && npm run just-test-in-node",
3535
"just-test": "karma start --config karma.conf.js",
36-
"just-test-in-node": "mocha --recursive --compilers js:babel-core/register test/plugins",
36+
"just-test-in-node": "mocha --recursive --compilers js:babel-core/register test",
3737
"serve-static": "http-server -i -a 0.0.0.0 -p 3001",
3838
"prestart": "npm install",
3939
"start": "npm-run-all --parallel serve-static open-static",

src/plugins/validation/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { transformPathToArray } from "./path-translator"
2+
3+
14
// Core validation plugin
25
// Does nothing for now
36

47
export default function() {
5-
return {}
8+
return {
9+
fn: {
10+
transformPathToArray
11+
}
12+
}
613
}

src/path-translator.js renamed to src/plugins/validation/path-translator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ export function transformPathToArray(property, jsSpec) {
99

1010
var pathArr = []
1111

12+
// replace '.', '["', '"]' separators with pipes
13+
str = str.replace(/\.(?![^["]*"\])|(\[\")|(\"\]\.?)/g, "|")
14+
15+
// split on our new delimiter, pipe
16+
str = str.split("|")
17+
1218
str
13-
.split(".")
1419
.map(item => {
1520
// "key[0]" becomes ["key", "0"]
1621
if(item.includes("[")) {

src/plugins/validation/semantic-validators/hook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import concat from "lodash/concat"
22
import reduce from "lodash/reduce"
33
import isArray from "lodash/isArray"
4-
import { transformPathToArray } from "../../../path-translator"
4+
import { transformPathToArray } from "../path-translator"
55

66
import assign from "lodash/assign"
77
import getTimestamp from "../get-timestamp"

src/plugins/validation/structural-validation/validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import JSONSchema from "jsonschema"
2-
import { transformPathToArray } from "../../../path-translator.js"
2+
import { transformPathToArray } from "../path-translator.js"
33

44

55
import jsonSchema from "./jsonSchema"

test/path-translator.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
import expect from "expect"
3-
import { transformPathToArray } from "core/path-translator"
3+
import { transformPathToArray } from "src/plugins/validation/path-translator"
44

55
describe("validation plugin - path translator", function(){
66

@@ -50,6 +50,37 @@ describe("validation plugin - path translator", function(){
5050

5151
})
5252

53+
it("should translate paths separated by brackets", function() {
54+
// Given
55+
let jsSpec = {
56+
definitions: {
57+
"One.Two": {
58+
a: "1"
59+
}
60+
}
61+
}
62+
let path = "instance.definitions[\"One.Two\"]"
63+
64+
// Then
65+
expect(transformPathToArray(path, jsSpec)).toEqual(["definitions", "One.Two"])
66+
})
67+
68+
it("should translate paths separated by brackets with string keys, and then periods", function() {
69+
// Given
70+
let jsSpec = {
71+
definitions: {
72+
"One.Two": {
73+
a: "1",
74+
abc123: "1"
75+
}
76+
}
77+
}
78+
let path = "instance.definitions[\"One.Two\"].abc123"
79+
80+
// Then
81+
expect(transformPathToArray(path, jsSpec)).toEqual(["definitions", "One.Two", "abc123"])
82+
})
83+
5384
it("should translate an doubly ambiguous string path to an array", function(){
5485
// Since JSONSchema uses periods to mark different properties,
5586
// a key with two periods in it (like "www.google.com") is doubly ambiguous,

0 commit comments

Comments
 (0)