Skip to content

Commit ecd677e

Browse files
fix: throw an error if target schema not found
1 parent 2f10f64 commit ecd677e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ 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(`Cannot resolve ref "${ref.ref}".`)
102+
}
103+
100104
ref.targetSchema = targetSchema
101105
ref.targetSchemaId = refSchemaId
102106
}

test/deref-schema.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,42 @@ 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(error.message, 'Cannot resolve ref "#foo".')
232+
}
233+
})

0 commit comments

Comments
 (0)