File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,10 @@ class RefResolver {
97
97
} = this . #parseSchemaRef( ref . ref , ref . sourceSchemaId )
98
98
99
99
const targetSchema = this . getDerefSchema ( refSchemaId , refJsonPointer )
100
+ if ( targetSchema === null ) {
101
+ throw new Error ( `Cannot resolve ref "${ ref . ref } ".` )
102
+ }
103
+
100
104
ref . targetSchema = targetSchema
101
105
ref . targetSchemaId = refSchemaId
102
106
}
Original file line number Diff line number Diff line change @@ -192,3 +192,42 @@ test('should clone schema without refs', () => {
192
192
}
193
193
} )
194
194
} )
195
+
196
+ test ( 'should throw if target ref schema is not found' , ( ) => {
197
+ const inputSchema = {
198
+ $id : 'http://example.com/root.json' ,
199
+ definitions : {
200
+ A : { $id : '#foo' } ,
201
+ B : {
202
+ $id : 'other.json' ,
203
+ definitions : {
204
+ X : { $id : '#bar' , type : 'string' } ,
205
+ Y : { $id : 't/inner.json' }
206
+ }
207
+ } ,
208
+ C : {
209
+ $id : 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f' ,
210
+ type : 'object'
211
+ }
212
+ }
213
+ }
214
+
215
+ const addresSchema = {
216
+ $id : 'relativeAddress' , // Note: prefer always absolute URI like: http://mysite.com
217
+ type : 'object' ,
218
+ properties : {
219
+ zip : { $ref : 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f' } ,
220
+ city2 : { $ref : '#foo' }
221
+ }
222
+ }
223
+
224
+ const refResolver = new RefResolver ( )
225
+ refResolver . addSchema ( inputSchema )
226
+ refResolver . addSchema ( addresSchema )
227
+
228
+ try {
229
+ refResolver . derefSchema ( 'relativeAddress' )
230
+ } catch ( error ) {
231
+ assert . strictEqual ( error . message , 'Cannot resolve ref "#foo".' )
232
+ }
233
+ } )
You can’t perform that action at this time.
0 commit comments