@@ -136,22 +136,55 @@ export default (fetch: any, fs: any) => {
136
136
}
137
137
}
138
138
139
- if ( await fileExistsAndReadable ( ref ) === true ) {
140
- const fileContents = await readFile ( ref ) ;
139
+ // copy hash fragment from the filepath / url
140
+ const hashFragmentSplit = ref . split ( "#" ) ;
141
+ let hashFragment ;
142
+ if ( hashFragmentSplit . length > 1 ) {
143
+ hashFragment = hashFragmentSplit [ hashFragmentSplit . length - 1 ] ;
144
+ }
145
+
146
+ let hashlessRef = ref ;
147
+ if ( hashFragment ) {
148
+ hashlessRef = ref . replace ( `#${ hashFragment } ` , "" ) ;
149
+ }
150
+
151
+ if ( await fileExistsAndReadable ( hashlessRef ) === true ) {
152
+ // pull off the hash fragment first
153
+ const fileContents = await readFile ( hashlessRef ) ;
141
154
let reffedSchema ;
142
155
try {
143
156
reffedSchema = JSON . parse ( fileContents ) ;
144
157
} catch ( e ) {
145
158
throw new NonJsonRefError ( { $ref : ref } , fileContents ) ;
146
159
}
147
160
161
+ if ( hashFragment ) {
162
+ try {
163
+ const pointer = Ptr . parse ( hashFragment ) ;
164
+ return Promise . resolve ( pointer . eval ( reffedSchema ) ) ;
165
+ } catch ( e ) {
166
+ throw new InvalidJsonPointerRefError ( { $ref : ref } ) ;
167
+ }
168
+ }
169
+
148
170
return reffedSchema ;
149
171
} else if ( isUrlLike ( ref ) === false ) {
150
172
throw new InvalidFileSystemPathError ( ref ) ;
151
173
}
152
174
153
175
try {
154
- return await fetch ( ref ) . then ( ( r : any ) => r . json ( ) ) ;
176
+ // leave the hash fragment on
177
+ // but evaluate the result after
178
+ const result = await fetch ( ref ) . then ( ( r : any ) => r . json ( ) ) ;
179
+ if ( hashFragment ) {
180
+ try {
181
+ const pointer = Ptr . parse ( hashFragment ) ;
182
+ return Promise . resolve ( pointer . eval ( result ) ) ;
183
+ } catch ( e ) {
184
+ throw new InvalidJsonPointerRefError ( { $ref : ref } ) ;
185
+ }
186
+ }
187
+ return result ;
155
188
} catch ( e ) {
156
189
throw new InvalidRemoteURLError ( ref ) ;
157
190
}
0 commit comments