Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw more informative error when cannot resolve ref #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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