Skip to content

Commit

Permalink
fix: throw an error if target schema not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Oct 21, 2023
1 parent 2f10f64 commit ecd677e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class RefResolver {
} = this.#parseSchemaRef(ref.ref, ref.sourceSchemaId)

const targetSchema = this.getDerefSchema(refSchemaId, refJsonPointer)
if (targetSchema === null) {
throw new Error(`Cannot resolve ref "${ref.ref}".`)
}

ref.targetSchema = targetSchema
ref.targetSchemaId = refSchemaId
}
Expand Down
39 changes: 39 additions & 0 deletions test/deref-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,42 @@ test('should clone schema without refs', () => {
}
})
})

test('should throw if target ref schema is not found', () => {
const inputSchema = {
$id: 'http://example.com/root.json',
definitions: {
A: { $id: '#foo' },
B: {
$id: 'other.json',
definitions: {
X: { $id: '#bar', type: 'string' },
Y: { $id: 't/inner.json' }
}
},
C: {
$id: 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f',
type: 'object'
}
}
}

const addresSchema = {
$id: 'relativeAddress', // Note: prefer always absolute URI like: http://mysite.com
type: 'object',
properties: {
zip: { $ref: 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f' },
city2: { $ref: '#foo' }
}
}

const refResolver = new RefResolver()
refResolver.addSchema(inputSchema)
refResolver.addSchema(addresSchema)

try {
refResolver.derefSchema('relativeAddress')
} catch (error) {
assert.strictEqual(error.message, 'Cannot resolve ref "#foo".')
}
})

0 comments on commit ecd677e

Please sign in to comment.