From 34a9deb3c25d00d91dc06230ec47934ec94bde60 Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Fri, 20 Oct 2023 22:35:56 +0200 Subject: [PATCH] fix: always return null if sub schema is not found --- index.js | 2 +- ...ref-resolver.test.js => get-schema.test.js} | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) rename test/{ref-resolver.test.js => get-schema.test.js} (92%) diff --git a/index.js b/index.js index c86fe77..f5059c0 100644 --- a/index.js +++ b/index.js @@ -257,7 +257,7 @@ function getDataByJSONPointer (data, jsonPointer) { } current = current[part] } - return current + return current ?? null } module.exports = { RefResolver } diff --git a/test/ref-resolver.test.js b/test/get-schema.test.js similarity index 92% rename from test/ref-resolver.test.js rename to test/get-schema.test.js index 6a40100..1ef7920 100644 --- a/test/ref-resolver.test.js +++ b/test/get-schema.test.js @@ -196,3 +196,21 @@ test('should not use a root $id if it is an anchor', () => { const resolvedSchema3 = refResolver.getSchema(schemaIdArgument, schemaIdProperty) assert.equal(resolvedSchema3, schema) }) + +test('should return null if sub schema by json pointer is not found', () => { + const refResolver = new RefResolver() + + const schemaId = 'schemaId' + const schema = { + $id: 'schemaId', + type: 'object', + properties: { + foo: { type: 'string' } + } + } + + refResolver.addSchema(schema) + + const schemaRefs = refResolver.getSchema(schemaId, '#/missingSchema') + assert.equal(schemaRefs, null) +})