From 7d77f9da942499f38399f05dc3cd116253e985fb Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Tue, 5 Jul 2022 09:35:41 +0300 Subject: [PATCH] fix: set hash sign as default json pointer (#485) * fix: set hash sign as default json pointer * change test name --- index.js | 2 +- test/ref.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 083f0fc2..69b1bac6 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ function resolveRef (location, ref) { } const schemaId = ref.slice(0, hashIndex) || location.schemaId - const jsonPointer = ref.slice(hashIndex) + const jsonPointer = ref.slice(hashIndex) || '#' const schemaRef = schemaId + jsonPointer diff --git a/test/ref.test.js b/test/ref.test.js index fce9d20e..3158b6aa 100644 --- a/test/ref.test.js +++ b/test/ref.test.js @@ -1926,3 +1926,30 @@ test('anyOf inside allOf', (t) => { t.equal(output, JSON.stringify(object)) }) + +test('should resolve absolute $refs', (t) => { + t.plan(1) + + const externalSchema = { + FooSchema: { + $id: 'FooSchema', + type: 'object', + properties: { + type: { + anyOf: [ + { type: 'string', const: 'bar' }, + { type: 'string', const: 'baz' } + ] + } + } + } + } + + const schema = { $ref: 'FooSchema' } + + const object = { type: 'bar' } + const stringify = build(schema, { schema: externalSchema }) + const output = stringify(object) + + t.equal(output, JSON.stringify(object)) +})