-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: include path for duplicate key exception (#19)
- Loading branch information
Showing
5 changed files
with
58 additions
and
47 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
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,46 @@ | ||
import { JsonPath } from '@stoplight/types'; | ||
import { Kind, YAMLMapping, YAMLNode, YAMLScalar, YAMLSequence } from 'yaml-ast-parser'; | ||
import { isValidNode } from './utils'; | ||
|
||
export function buildJsonPath(node: YAMLNode) { | ||
const path: JsonPath = []; | ||
|
||
let prevNode: YAMLNode = node; | ||
|
||
while (node) { | ||
switch (node.kind) { | ||
case Kind.SCALAR: | ||
path.unshift((node as YAMLScalar).value); | ||
break; | ||
case Kind.MAPPING: | ||
if (prevNode !== (node as YAMLMapping).key) { | ||
if ( | ||
path.length > 0 && | ||
isValidNode((node as YAMLMapping).value) && | ||
(node as YAMLMapping).value.value === path[0] | ||
) { | ||
path[0] = (node as YAMLMapping).key.value; | ||
} else { | ||
path.unshift((node as YAMLMapping).key.value); | ||
} | ||
} | ||
break; | ||
case Kind.SEQ: | ||
if (prevNode) { | ||
const index = (node as YAMLSequence).items.indexOf(prevNode); | ||
if (prevNode.kind === Kind.SCALAR) { | ||
path[0] = index; | ||
// always better to point to parent node rather than nothing | ||
} else if (index !== -1) { | ||
path.unshift(index); | ||
} | ||
} | ||
break; | ||
} | ||
|
||
prevNode = node; | ||
node = node.parent; | ||
} | ||
|
||
return path; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
|
||
export const isValidNode = (node: YAMLNode) => node !== null && node !== undefined; |