From 2ef7b2ad8d9b802d9044164bed8eda1b6ce1f661 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 18 Jul 2017 20:32:09 -0700 Subject: [PATCH 1/2] Remove path translator --- src/core/path-translator.js | 67 ---------------------------------- src/core/plugins/util/index.js | 3 +- 2 files changed, 1 insertion(+), 69 deletions(-) delete mode 100644 src/core/path-translator.js diff --git a/src/core/path-translator.js b/src/core/path-translator.js deleted file mode 100644 index 4d1ed27b..00000000 --- a/src/core/path-translator.js +++ /dev/null @@ -1,67 +0,0 @@ -import get from "lodash/get" - -export function transformPathToArray(property, jsSpec) { - if(property.slice(0,9) === "instance.") { - var str = property.slice(9) - } else { // eslint-disable-next-line no-redeclare - var str = property - } - - var pathArr = [] - - str - .split(".") - .map(item => { - // "key[0]" becomes ["key", "0"] - if(item.includes("[")) { - let index = parseInt(item.match(/\[(.*)\]/)[1]) - let keyName = item.slice(0, item.indexOf("[")) - return [keyName, index.toString()] - } else { - return item - } - }) - .reduce(function(a, b) { - // flatten! - return a.concat(b) - }, []) - .concat([""]) // add an empty item into the array, so we don't get stuck with something in our buffer below - .reduce((buffer, curr) => { - let obj = pathArr.length ? get(jsSpec, pathArr) : jsSpec - - if(get(obj, makeAccessArray(buffer, curr))) { - if(buffer.length) { - pathArr.push(buffer) - } - if(curr.length) { - pathArr.push(curr) - } - return "" - } else { - // attach key to buffer - return `${buffer}${buffer.length ? "." : ""}${curr}` - } - }, "") - - if(typeof get(jsSpec, pathArr) !== "undefined") { - return pathArr - } else { - // if our path is not correct (there is no value at the path), - // return null - return null - } -} - -function makeAccessArray(buffer, curr) { - let arr = [] - - if(buffer.length) { - arr.push(buffer) - } - - if(curr.length) { - arr.push(curr) - } - - return arr -} diff --git a/src/core/plugins/util/index.js b/src/core/plugins/util/index.js index 57e163f4..033288a6 100644 --- a/src/core/plugins/util/index.js +++ b/src/core/plugins/util/index.js @@ -1,8 +1,7 @@ import { shallowEqualKeys } from "core/utils" -import { transformPathToArray } from "core/path-translator" export default function() { return { - fn: { shallowEqualKeys, transformPathToArray } + fn: { shallowEqualKeys } } } From 6a48e43ca526bac923da561f412ae05a96e5621c Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 18 Jul 2017 20:42:46 -0700 Subject: [PATCH 2/2] Delete path translator tests --- test/core/path-translator.js | 183 ----------------------------------- 1 file changed, 183 deletions(-) delete mode 100644 test/core/path-translator.js diff --git a/test/core/path-translator.js b/test/core/path-translator.js deleted file mode 100644 index 7abc1d8e..00000000 --- a/test/core/path-translator.js +++ /dev/null @@ -1,183 +0,0 @@ -/* eslint-env mocha */ -import expect from "expect" -import { transformPathToArray } from "core/path-translator" - -describe("validation plugin - path translator", function(){ - - describe("string paths", function(){ - - it("should translate a simple string path to an array", function(){ - // Given - let jsSpec = { - one: { - a: "a thing", - b: "another thing", - c: "one more thing" - }, - two: 2 - } - - let path = "instance.one.a" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(["one", "a"]) - - }) - - it("should translate an ambiguous string path to an array", function(){ - // Since JSONSchema uses periods to mark different properties, - // a key with a period in it is ambiguous, because it can mean at least two things. - // In our case, the path can mean: - // ["google", "com", "a"] or ["google.com", "a"] - - // Given - let jsSpec = { - "google.com": { - a: "a thing", - b: "another thing", - c: "one more thing" - }, - "gmail.com": { - d: "more stuff", - e: "even more stuff" - } - } - - let path = "instance.google.com.a" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a"]) - - }) - - it("should translate an doubly ambiguous string path to an array", function(){ - // Since JSONSchema uses periods to mark different properties, - // a key with two periods in it (like "www.google.com") is doubly ambiguous, - // because it can mean at least three things. - - - // Given - let jsSpec = { - "www.google.com": { - a: "a thing", - b: "another thing", - c: "one more thing" - }, - "gmail.com": { - d: "more stuff", - e: "even more stuff" - } - } - - let path = "instance.www.google.com.a" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(["www.google.com", "a"]) - - }) - - it("should return null for an invalid path", function(){ - - // Given - let jsSpec = { - "google.com": { - a: "a thing", - b: "another thing", - c: "one more thing" - }, - "gmail.com": { - d: "more stuff", - e: "even more stuff" - } - } - - let path = "instance.google.net.a" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(null) - - }) - - it("should return inline array indices in their own value", function(){ - // "a[1]" => ["a", "1"] - - // Given - let jsSpec = { - "google.com": { - a: [ - "hello", - "here is the target" - ], - b: "another thing", - c: "one more thing" - }, - "gmail.com": { - d: "more stuff", - e: "even more stuff" - } - } - - let path = "instance.google.com.a[1]" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a", "1"]) - - }) - - it("should return the correct path when the last part is ambiguous", function(){ - - // Given - let jsSpec = { - "google.com": { - a: [ - "hello", - { - "gmail.com": 1234 - } - ], - b: "another thing", - c: "one more thing" - }, - "gmail.com": { - d: "more stuff", - e: "even more stuff" - } - } - - let path = "instance.google.com.a[1].gmail.com" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a", "1", "gmail.com"]) - - }) - - it("should return the correct path when the last part is doubly ambiguous", function(){ - - // Given - let jsSpec = { - "google.com": { - a: [ - "hello", - { - "www.gmail.com": 1234 - } - ], - b: "another thing", - c: "one more thing" - }, - "gmail.com": { - d: "more stuff", - e: "even more stuff" - } - } - - let path = "instance.google.com.a[1].www.gmail.com" - - // Then - expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a", "1", "www.gmail.com"]) - - }) - - }) - -})