File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,12 @@ class RefResolver {
9797 } = this . #parseSchemaRef( ref . ref , ref . sourceSchemaId )
9898
9999 const targetSchema = this . getDerefSchema ( refSchemaId , refJsonPointer )
100+ if ( targetSchema === null ) {
101+ throw new Error (
102+ `Cannot resolve ref "${ ref . ref } ". Ref "${ refJsonPointer } " is not found in schema "${ refSchemaId } ".`
103+ )
104+ }
105+
100106 ref . targetSchema = targetSchema
101107 ref . targetSchemaId = refSchemaId
102108 }
Original file line number Diff line number Diff line change @@ -192,3 +192,45 @@ test('should clone schema without refs', () => {
192192 }
193193 } )
194194} )
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 (
232+ error . message ,
233+ 'Cannot resolve ref "#foo". Ref "#foo" is not found in schema "relativeAddress".'
234+ )
235+ }
236+ } )
You can’t perform that action at this time.
0 commit comments