Skip to content

Commit e2bd7bf

Browse files
feat: throw more informative error when cannot resolve ref (#4)
1 parent 73ea796 commit e2bd7bf

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class RefResolver {
3333
getSchema (schemaId, jsonPointer = '#') {
3434
const schema = this.#schemas[schemaId]
3535
if (schema === undefined) {
36-
throw new Error(`Schema with id "${schemaId}" is not found.`)
36+
throw new Error(
37+
`Cannot resolve ref "${schemaId}${jsonPointer}". Schema with id "${schemaId}" is not found.`
38+
)
3739
}
3840
if (schema.anchors[jsonPointer] !== undefined) {
3941
return schema.anchors[jsonPointer]

test/anchor.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ test('should fail to find a schema using an anchor instead of schema id', () =>
4949
try {
5050
refResolver.getSchema(subSchemaAnchor)
5151
} catch (err) {
52-
assert.equal(err.message, 'Schema with id "#subSchemaId" is not found.')
52+
assert.equal(
53+
err.message, 'Cannot resolve ref "#subSchemaId#". Schema with id "#subSchemaId" is not found.'
54+
)
5355
}
5456
})

test/get-schema.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ test('root $id has higher priority than a schemaId argument', () => {
164164
refResolver.getSchema(schemaIdArgument)
165165
assert.fail('should have thrown an error')
166166
} catch (err) {
167-
assert.equal(err.message, `Schema with id "${schemaIdArgument}" is not found.`)
167+
assert.equal(
168+
err.message,
169+
`Cannot resolve ref "${schemaIdArgument}#". Schema with id "${schemaIdArgument}" is not found.`
170+
)
168171
}
169172
})
170173

@@ -187,7 +190,10 @@ test('should not use a root $id if it is an anchor', () => {
187190
refResolver.getSchema(schemaIdProperty)
188191
assert.fail('should have thrown an error')
189192
} catch (err) {
190-
assert.equal(err.message, `Schema with id "${schemaIdProperty}" is not found.`)
193+
assert.equal(
194+
err.message,
195+
`Cannot resolve ref "${schemaIdProperty}#". Schema with id "${schemaIdProperty}" is not found.`
196+
)
191197
}
192198

193199
const resolvedSchema2 = refResolver.getSchema(schemaIdArgument)

0 commit comments

Comments
 (0)