@@ -2,7 +2,7 @@ import { getSchema, compile, interpret, getKeywordName, hasDialect, BASIC } from
2
2
import * as JsonPointer from "@hyperjump/json-pointer" ;
3
3
import { resolveIri , toAbsoluteIri } from "@hyperjump/uri" ;
4
4
import { getNodeValue , parseTree } from "jsonc-parser" ;
5
- import * as JsonNode from "./json -node.js" ;
5
+ import * as SchemaNode from "./schema -node.js" ;
6
6
import { uriFragment } from "./util.js" ;
7
7
8
8
@@ -27,16 +27,16 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
27
27
28
28
buildSchemaResources ( document , root , textDocument . uri , contextDialectUri ) ;
29
29
30
- for ( const { dialectUri , schemaResource } of document . schemaResources ) {
31
- if ( ! hasDialect ( dialectUri ) ) {
32
- const $schema = JsonNode . get ( "#/$schema" , schemaResource ) ;
33
- if ( $schema && JsonNode . typeOf ( $schema ) === "string" ) {
30
+ for ( const schemaResource of document . schemaResources ) {
31
+ if ( ! hasDialect ( schemaResource . dialectUri ) ) {
32
+ const $schema = SchemaNode . get ( "#/$schema" , schemaResource ) ;
33
+ if ( $schema && SchemaNode . typeOf ( $schema ) === "string" ) {
34
34
document . errors . push ( {
35
35
keyword : "https://json-schema.org/keyword/schema" ,
36
36
instanceNode : $schema ,
37
37
message : "Unknown dialect"
38
38
} ) ;
39
- } else if ( dialectUri ) {
39
+ } else if ( schemaResource . dialectUri ) {
40
40
document . errors . push ( {
41
41
keyword : "https://json-schema.org/keyword/schema" ,
42
42
instanceNode : schemaResource ,
@@ -53,15 +53,15 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
53
53
continue ;
54
54
}
55
55
56
- const schema = await getSchema ( dialectUri ) ;
56
+ const schema = await getSchema ( schemaResource . dialectUri ) ;
57
57
const compiled = await compile ( schema ) ;
58
58
const output = interpret ( compiled , schemaResource , BASIC ) ;
59
59
if ( ! output . valid ) {
60
60
for ( const error of output . errors ) {
61
61
document . errors . push ( {
62
62
keyword : error . keyword ,
63
63
keywordNode : await getSchema ( error . absoluteKeywordLocation ) ,
64
- instanceNode : JsonNode . get ( error . instanceLocation , schemaResource )
64
+ instanceNode : SchemaNode . get ( error . instanceLocation , schemaResource )
65
65
} ) ;
66
66
}
67
67
}
@@ -72,13 +72,13 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
72
72
} ;
73
73
74
74
const buildSchemaResources = ( document , node , uri = "" , dialectUri = "" , pointer = "" , parent = undefined , anchors = { } ) => {
75
- const jsonNode = JsonNode . cons ( uri , pointer , getNodeValue ( node ) , node . type , [ ] , parent , node . offset , node . length ) ;
75
+ const schemaNode = SchemaNode . cons ( uri , pointer , getNodeValue ( node ) , node . type , [ ] , parent , node . offset , node . length , dialectUri , anchors ) ;
76
76
77
77
switch ( node . type ) {
78
78
case "array" :
79
- jsonNode . children = node . children . map ( ( child , index ) => {
79
+ schemaNode . children = node . children . map ( ( child , index ) => {
80
80
const itemPointer = JsonPointer . append ( index , pointer ) ;
81
- return buildSchemaResources ( document , child , uri , dialectUri , itemPointer , jsonNode , anchors ) ;
81
+ return buildSchemaResources ( document , child , uri , dialectUri , itemPointer , schemaNode , anchors ) ;
82
82
} ) ;
83
83
break ;
84
84
@@ -89,6 +89,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
89
89
if ( $schema ?. type === "string" ) {
90
90
try {
91
91
dialectUri = toAbsoluteIri ( getNodeValue ( $schema ) ) ;
92
+ schemaNode . dialectUri = dialectUri ;
92
93
} catch ( error ) {
93
94
// Ignore
94
95
}
@@ -98,7 +99,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
98
99
const $idNode = idToken && nodeStep ( node , idToken ) ;
99
100
if ( $idNode ) {
100
101
uri = toAbsoluteIri ( resolveIri ( getNodeValue ( $idNode ) , uri ) ) ;
101
- jsonNode . baseUri = uri ;
102
+ schemaNode . baseUri = uri ;
102
103
}
103
104
104
105
const legacyIdToken = keywordNameFor ( "https://json-schema.org/keyword/draft-04/id" , dialectUri ) ;
@@ -107,7 +108,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
107
108
const legacy$id = getNodeValue ( legacy$idNode ) ;
108
109
if ( legacy$id [ 0 ] !== "#" ) {
109
110
uri = toAbsoluteIri ( resolveIri ( legacy$id , uri ) ) ;
110
- jsonNode . baseUri = uri ;
111
+ schemaNode . baseUri = uri ;
111
112
}
112
113
}
113
114
} else {
@@ -116,7 +117,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
116
117
if ( embeddedDialectUri ) {
117
118
buildSchemaResources ( document , node , uri , embeddedDialectUri ) ;
118
119
119
- return JsonNode . cons ( uri , pointer , true , "boolean" , [ ] , parent , node . offset , node . length ) ;
120
+ return SchemaNode . cons ( uri , pointer , true , "boolean" , [ ] , parent , node . offset , node . length , dialectUri , anchors ) ;
120
121
}
121
122
}
122
123
@@ -138,10 +139,10 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
138
139
139
140
for ( const child of node . children ) {
140
141
const propertyPointer = JsonPointer . append ( getNodeValue ( child . children [ 0 ] ) , pointer ) ;
141
- const propertyNode = buildSchemaResources ( document , child , uri , dialectUri , propertyPointer , jsonNode , anchors ) ;
142
+ const propertyNode = buildSchemaResources ( document , child , uri , dialectUri , propertyPointer , schemaNode , anchors ) ;
142
143
143
144
if ( propertyNode ) {
144
- jsonNode . children . push ( propertyNode ) ;
145
+ schemaNode . children . push ( propertyNode ) ;
145
146
}
146
147
}
147
148
break ;
@@ -151,22 +152,17 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
151
152
return ;
152
153
}
153
154
154
- jsonNode . children = node . children . map ( ( child ) => {
155
- return buildSchemaResources ( document , child , uri , dialectUri , pointer , jsonNode , anchors ) ;
155
+ schemaNode . children = node . children . map ( ( child ) => {
156
+ return buildSchemaResources ( document , child , uri , dialectUri , pointer , schemaNode , anchors ) ;
156
157
} ) ;
157
158
break ;
158
159
}
159
160
160
- if ( jsonNode . pointer === "" ) {
161
- document . schemaResources . push ( {
162
- schemaResource : jsonNode ,
163
- dialectUri : dialectUri ,
164
- baseUri : uri ,
165
- anchors : anchors
166
- } ) ;
161
+ if ( schemaNode . pointer === "" ) {
162
+ document . schemaResources . push ( schemaNode ) ;
167
163
}
168
164
169
- return jsonNode ;
165
+ return schemaNode ;
170
166
} ;
171
167
172
168
const getEmbeddedDialectUri = ( node , dialectUri ) => {
@@ -194,7 +190,7 @@ const getEmbeddedDialectUri = (node, dialectUri) => {
194
190
} ;
195
191
196
192
export const findNodeAtOffset = ( document , offset ) => {
197
- for ( const { schemaResource } of document . schemaResources ) {
193
+ for ( const schemaResource of document . schemaResources ) {
198
194
const node = _findNodeAtOffset ( schemaResource , offset ) ;
199
195
if ( node ) {
200
196
return node ;
0 commit comments