@@ -3,6 +3,7 @@ import * as JsonPointer from "@hyperjump/json-pointer";
3
3
import { resolveIri , toAbsoluteIri } from "@hyperjump/uri" ;
4
4
import { getNodeValue , parseTree } from "jsonc-parser" ;
5
5
import * as Instance from "./json-instance.js" ;
6
+ import { uriFragment } from "./util.js" ;
6
7
7
8
8
9
export class JsonSchemaDocument {
@@ -69,14 +70,14 @@ export class JsonSchemaDocument {
69
70
return document ;
70
71
}
71
72
72
- #buildSchemaResources( node , uri = "" , dialectUri = "" , pointer = "" , parent = undefined ) {
73
+ #buildSchemaResources( node , uri = "" , dialectUri = "" , pointer = "" , parent = undefined , anchors = { } ) {
73
74
const jsonNode = Instance . cons ( uri , pointer , getNodeValue ( node ) , node . type , [ ] , parent , node . offset , node . length ) ;
74
75
75
76
switch ( node . type ) {
76
77
case "array" :
77
78
jsonNode . children = node . children . map ( ( child , index ) => {
78
79
const itemPointer = JsonPointer . append ( index , pointer ) ;
79
- return this . #buildSchemaResources( child , uri , dialectUri , itemPointer , jsonNode ) ;
80
+ return this . #buildSchemaResources( child , uri , dialectUri , itemPointer , jsonNode , anchors ) ;
80
81
} ) ;
81
82
break ;
82
83
@@ -118,9 +119,25 @@ export class JsonSchemaDocument {
118
119
}
119
120
}
120
121
122
+ const anchorToken = keywordNameFor ( "https://json-schema.org/keyword/anchor" , dialectUri ) ;
123
+ const $anchorNode = anchorToken && nodeStep ( node , anchorToken ) ;
124
+ if ( $anchorNode ) {
125
+ const anchor = getNodeValue ( $anchorNode ) ;
126
+ anchors [ anchor ] = pointer ;
127
+ }
128
+
129
+ const legacyAnchorToken = keywordNameFor ( "https://json-schema.org/keyword/draft-04/id" , dialectUri ) ;
130
+ const legacyAnchorNode = legacyAnchorToken && nodeStep ( node , legacyAnchorToken ) ;
131
+ if ( legacyAnchorNode ) {
132
+ const anchor = getNodeValue ( legacyAnchorNode ) ;
133
+ if ( anchor [ 0 ] === "#" ) {
134
+ anchors [ uriFragment ( anchor ) ] = pointer ;
135
+ }
136
+ }
137
+
121
138
for ( const child of node . children ) {
122
139
const propertyPointer = JsonPointer . append ( getNodeValue ( child . children [ 0 ] ) , pointer ) ;
123
- const propertyNode = this . #buildSchemaResources( child , uri , dialectUri , propertyPointer , jsonNode ) ;
140
+ const propertyNode = this . #buildSchemaResources( child , uri , dialectUri , propertyPointer , jsonNode , anchors ) ;
124
141
125
142
if ( propertyNode ) {
126
143
jsonNode . children . push ( propertyNode ) ;
@@ -134,13 +151,18 @@ export class JsonSchemaDocument {
134
151
}
135
152
136
153
jsonNode . children = node . children . map ( ( child ) => {
137
- return this . #buildSchemaResources( child , uri , dialectUri , pointer , jsonNode ) ;
154
+ return this . #buildSchemaResources( child , uri , dialectUri , pointer , jsonNode , anchors ) ;
138
155
} ) ;
139
156
break ;
140
157
}
141
158
142
159
if ( jsonNode . pointer === "" ) {
143
- this . schemaResources . push ( { dialectUri, schemaResource : jsonNode } ) ;
160
+ this . schemaResources . push ( {
161
+ schemaResource : jsonNode ,
162
+ dialectUri : dialectUri ,
163
+ baseUri : uri ,
164
+ anchors : anchors
165
+ } ) ;
144
166
}
145
167
146
168
return jsonNode ;
0 commit comments