From cd1ce34e5356cb3fe4c59a1ab544d3b7d0f9ea2f Mon Sep 17 00:00:00 2001 From: Chris Deely Date: Sun, 29 Jan 2023 12:08:43 -0500 Subject: [PATCH] #371 first attempt at using property name as default title --- lib/schemaProxy.js | 5 ++++- test/schemaProxy.test.js | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/schemaProxy.js b/lib/schemaProxy.js index 28692f56..136dd3cd 100644 --- a/lib/schemaProxy.js +++ b/lib/schemaProxy.js @@ -54,10 +54,13 @@ const handler = ({ return undefined; }; meta[symbols.titles] = (target) => { + let selfName = meta[symbols.pointer]().split('/').pop(); + if (selfName === '' || selfName === 'properties') selfName = undefined; + if (parent) { // if we can determine the parent titles // then we append our own - return [...parent[symbols.titles], target.title]; + return [...parent[symbols.titles], target.title || selfName]; } // otherwise, it's just our own if (typeof target.title === 'string') { diff --git a/test/schemaProxy.test.js b/test/schemaProxy.test.js index e0b54bb2..37c7e8f5 100644 --- a/test/schemaProxy.test.js +++ b/test/schemaProxy.test.js @@ -96,6 +96,14 @@ describe('Testing Schema Proxy', () => { assert.deepStrictEqual(proxied.properties.zip[titles], ['Example Proxy', undefined, 'An object']); }); + it('Schema Proxy creates a JSON schema with title References using property name as default value', () => { + const proxied = loader()('proxy.schema.json', example); + + assert.deepStrictEqual(proxied[titles], ['Example Proxy']); + assert.deepStrictEqual(proxied.properties.foo[titles], ['Example Proxy', undefined, 'foo']); + assert.deepStrictEqual(proxied.properties.bar[titles], ['Example Proxy', undefined, 'bar']); + }); + it('Schema proxy resolves JSON Pointers', () => { const myloader = loader(); const proxied1 = myloader('proxy.schema.json', example);