Skip to content

Commit

Permalink
feat: throw more informative error when cannot resolve ref (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored Oct 22, 2023
1 parent 73ea796 commit e2bd7bf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class RefResolver {
getSchema (schemaId, jsonPointer = '#') {
const schema = this.#schemas[schemaId]
if (schema === undefined) {
throw new Error(`Schema with id "${schemaId}" is not found.`)
throw new Error(
`Cannot resolve ref "${schemaId}${jsonPointer}". Schema with id "${schemaId}" is not found.`
)
}
if (schema.anchors[jsonPointer] !== undefined) {
return schema.anchors[jsonPointer]
Expand Down
4 changes: 3 additions & 1 deletion test/anchor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ test('should fail to find a schema using an anchor instead of schema id', () =>
try {
refResolver.getSchema(subSchemaAnchor)
} catch (err) {
assert.equal(err.message, 'Schema with id "#subSchemaId" is not found.')
assert.equal(
err.message, 'Cannot resolve ref "#subSchemaId#". Schema with id "#subSchemaId" is not found.'
)
}
})
10 changes: 8 additions & 2 deletions test/get-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ test('root $id has higher priority than a schemaId argument', () => {
refResolver.getSchema(schemaIdArgument)
assert.fail('should have thrown an error')
} catch (err) {
assert.equal(err.message, `Schema with id "${schemaIdArgument}" is not found.`)
assert.equal(
err.message,
`Cannot resolve ref "${schemaIdArgument}#". Schema with id "${schemaIdArgument}" is not found.`
)
}
})

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

const resolvedSchema2 = refResolver.getSchema(schemaIdArgument)
Expand Down

0 comments on commit e2bd7bf

Please sign in to comment.