File tree Expand file tree Collapse file tree 3 files changed +160
-0
lines changed
packages/graph-schema-utils/src/formatters/json Expand file tree Collapse file tree 3 files changed +160
-0
lines changed Original file line number Diff line number Diff line change @@ -14,5 +14,24 @@ describe("JSON formatter", () => {
1414 message : "Duplicate node label IDs found in schema" ,
1515 } ) ;
1616 } ) ;
17+
18+ test ( "Identifies duplicated node object types and throws an error" , ( ) => {
19+ const schema = readFile (
20+ path . resolve (
21+ __dirname ,
22+ "./test-schemas/duplicated-nodeObjectType-ids.json"
23+ )
24+ ) ;
25+ assert . throws ( ( ) => fromJson ( schema ) , {
26+ message : "Duplicate node object type IDs found in schema" ,
27+ } ) ;
28+ } ) ;
29+
30+ test ( "Does not throw an error if there are no duplicated ids in node labels or node object types" , ( ) => {
31+ const schema = readFile (
32+ path . resolve ( __dirname , "./test-schemas/full.json" )
33+ ) ;
34+ assert . doesNotThrow ( ( ) => fromJson ( schema ) ) ;
35+ } ) ;
1736 } ) ;
1837} ) ;
Original file line number Diff line number Diff line change @@ -123,6 +123,21 @@ export function hasDuplicateNodeLabelIds(
123123 return false ;
124124}
125125
126+ export function hasDuplicateNodeObjectTypeIds (
127+ schema : GraphSchemaJsonStruct
128+ ) : boolean {
129+ const ids = new Set < string > ( ) ;
130+
131+ for ( const nodeObjectType of schema . nodeObjectTypes ) {
132+ if ( ids . has ( nodeObjectType . $id ) ) {
133+ return true ;
134+ }
135+ ids . add ( nodeObjectType . $id ) ;
136+ }
137+
138+ return false ;
139+ }
140+
126141export function fromJsonStruct ( schemaJson : RootSchemaJsonStruct ) : GraphSchema {
127142 const { graphSchema } = schemaJson . graphSchemaRepresentation ;
128143 console . log ( graphSchema , hasDuplicateNodeLabelIds ( graphSchema ) ) ;
@@ -133,6 +148,9 @@ export function fromJsonStruct(schemaJson: RootSchemaJsonStruct): GraphSchema {
133148 const relationshipTypes = graphSchema . relationshipTypes . map (
134149 relationshipType . create
135150 ) ;
151+ if ( hasDuplicateNodeObjectTypeIds ( graphSchema ) ) {
152+ throw new Error ( "Duplicate node object type IDs found in schema" ) ;
153+ }
136154 const nodeObjectTypes = graphSchema . nodeObjectTypes . map (
137155 ( nodeObjectTypeJson ) =>
138156 nodeObjectType . create ( nodeObjectTypeJson , ( ref ) => {
Original file line number Diff line number Diff line change 1+ {
2+ "graphSchemaRepresentation" : {
3+ "version" : " 1.0.0" ,
4+ "graphSchema" : {
5+ "nodeLabels" : [
6+ {
7+ "$id" : " nl:0" ,
8+ "token" : " DOCS_CHUNKS_TABLE" ,
9+ "properties" : [
10+ {
11+ "$id" : " p:0_0" ,
12+ "token" : " RELATIVE_PATH" ,
13+ "type" : {
14+ "type" : " string"
15+ },
16+ "nullable" : false
17+ },
18+ {
19+ "$id" : " p:0_1" ,
20+ "token" : " SIZE" ,
21+ "type" : {
22+ "type" : " integer"
23+ },
24+ "nullable" : false
25+ },
26+ {
27+ "$id" : " p:0_2" ,
28+ "token" : " FILE_URL" ,
29+ "type" : {
30+ "type" : " string"
31+ },
32+ "nullable" : false
33+ },
34+ {
35+ "$id" : " p:0_3" ,
36+ "token" : " SCOPED_FILE_URL" ,
37+ "type" : {
38+ "type" : " string"
39+ },
40+ "nullable" : false
41+ },
42+ {
43+ "$id" : " p:0_4" ,
44+ "token" : " CHUNK" ,
45+ "type" : {
46+ "type" : " string"
47+ },
48+ "nullable" : false
49+ }
50+ ]
51+ },
52+ {
53+ "$id" : " nl:1" ,
54+ "token" : " DOCS_METADATA" ,
55+ "properties" : [
56+ {
57+ "$id" : " p:0_0" ,
58+ "token" : " RELATIVE_PATH" ,
59+ "type" : {
60+ "type" : " string"
61+ },
62+ "nullable" : false
63+ },
64+ {
65+ "$id" : " p:0_1" ,
66+ "token" : " MODEL_NAME" ,
67+ "type" : {
68+ "type" : " string"
69+ },
70+ "nullable" : false
71+ },
72+ {
73+ "$id" : " p:0_2" ,
74+ "token" : " SKILLS" ,
75+ "type" : {
76+ "type" : " string"
77+ },
78+ "nullable" : false
79+ },
80+ {
81+ "$id" : " p:0_3" ,
82+ "token" : " DOMAINS" ,
83+ "type" : {
84+ "type" : " string"
85+ },
86+ "nullable" : false
87+ },
88+ {
89+ "$id" : " p:0_4" ,
90+ "token" : " SUMMARY" ,
91+ "type" : {
92+ "type" : " string"
93+ },
94+ "nullable" : false
95+ }
96+ ]
97+ }
98+ ],
99+ "relationshipTypes" : [],
100+ "nodeObjectTypes" : [
101+ {
102+ "$id" : " n:0" ,
103+ "labels" : [
104+ {
105+ "$ref" : " #nl:0"
106+ }
107+ ]
108+ },
109+ {
110+ "$id" : " n:0" ,
111+ "labels" : [
112+ {
113+ "$ref" : " #nl:0"
114+ }
115+ ]
116+ }
117+ ],
118+ "relationshipObjectTypes" : [],
119+ "constraints" : [],
120+ "indexes" : []
121+ }
122+ }
123+ }
You can’t perform that action at this time.
0 commit comments