forked from atlassian/better-ajv-errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds decorated data path (atlassian#28)
* adds decorated data path * keep standard json pointer path * migrate tests * remove integration test
- Loading branch information
Showing
10 changed files
with
71 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/node_modules/ | ||
/coverage/ | ||
*.log | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export default function getDecoratedDataPath(jsonAst, dataPath) { | ||
// TODO: Handle json pointer escape notation and better error handling | ||
const pointers = dataPath.split('/').slice(1); | ||
let decoratedPath = ''; | ||
pointers.reduce((obj, pointer, idx) => { | ||
switch (obj.type) { | ||
case 'Object': { | ||
decoratedPath += `/${pointer}`; | ||
const filtered = obj.children.filter( | ||
child => child.key.value === pointer | ||
); | ||
if (filtered.length !== 1) { | ||
throw new Error(`Couldn't find property ${pointer} of ${dataPath}`); | ||
} | ||
return filtered[0].value; | ||
} | ||
case 'Array': | ||
decoratedPath += `/${pointer}${getTypeName(obj.children[pointer])}`; | ||
return obj.children[pointer]; | ||
default: | ||
// eslint-disable-next-line no-console | ||
console.log(obj); | ||
} | ||
}, jsonAst); | ||
return decoratedPath; | ||
} | ||
|
||
function getTypeName(obj) { | ||
const type = obj.children.filter(child => child.key.value === 'type'); | ||
|
||
if (!type.length) { | ||
return ''; | ||
} | ||
|
||
return (type[0].value && `:${type[0].value.value}`) || ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as getMetaFromPath } from './get-meta-from-path'; | ||
export { default as getDecoratedDataPath } from './get-decorated-data-pah'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters