From ecd677e71461d8524127c83935a2a0d47c2c39f4 Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Sat, 21 Oct 2023 13:18:20 +0200 Subject: [PATCH 1/3] fix: throw an error if target schema not found --- index.js | 4 ++++ test/deref-schema.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/index.js b/index.js index f5059c0..d1a082e 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/test/deref-schema.test.js b/test/deref-schema.test.js index facfc58..d0b196b 100644 --- a/test/deref-schema.test.js +++ b/test/deref-schema.test.js @@ -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".') + } +}) From c5bd7a40060f9641a69b8955492390f4c951134d Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Sat, 21 Oct 2023 13:40:44 +0200 Subject: [PATCH 2/3] update error message --- index.js | 4 +++- test/deref-schema.test.js | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d1a082e..ea0b3a5 100644 --- a/index.js +++ b/index.js @@ -98,7 +98,9 @@ class RefResolver { const targetSchema = this.getDerefSchema(refSchemaId, refJsonPointer) if (targetSchema === null) { - throw new Error(`Cannot resolve ref "${ref.ref}".`) + throw new Error( + `Cannot resolve ref "${ref.ref}". Ref ${refJsonPointer} is not found in schema "${refSchemaId}".` + ) } ref.targetSchema = targetSchema diff --git a/test/deref-schema.test.js b/test/deref-schema.test.js index d0b196b..8d35d7b 100644 --- a/test/deref-schema.test.js +++ b/test/deref-schema.test.js @@ -228,6 +228,9 @@ test('should throw if target ref schema is not found', () => { try { refResolver.derefSchema('relativeAddress') } catch (error) { - assert.strictEqual(error.message, 'Cannot resolve ref "#foo".') + assert.strictEqual( + error.message, + 'Cannot resolve ref "#foo". Ref #foo is not found in schema "relativeAddress".' + ) } }) From 5beb72c6bd81d967cb55b223fe5c9364612c1132 Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Sat, 21 Oct 2023 13:42:12 +0200 Subject: [PATCH 3/3] update error message --- index.js | 2 +- test/deref-schema.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ea0b3a5..a912273 100644 --- a/index.js +++ b/index.js @@ -99,7 +99,7 @@ class RefResolver { const targetSchema = this.getDerefSchema(refSchemaId, refJsonPointer) if (targetSchema === null) { throw new Error( - `Cannot resolve ref "${ref.ref}". Ref ${refJsonPointer} is not found in schema "${refSchemaId}".` + `Cannot resolve ref "${ref.ref}". Ref "${refJsonPointer}" is not found in schema "${refSchemaId}".` ) } diff --git a/test/deref-schema.test.js b/test/deref-schema.test.js index 8d35d7b..2efd5d7 100644 --- a/test/deref-schema.test.js +++ b/test/deref-schema.test.js @@ -230,7 +230,7 @@ test('should throw if target ref schema is not found', () => { } catch (error) { assert.strictEqual( error.message, - 'Cannot resolve ref "#foo". Ref #foo is not found in schema "relativeAddress".' + 'Cannot resolve ref "#foo". Ref "#foo" is not found in schema "relativeAddress".' ) } })