Skip to content

Commit

Permalink
feat(proxy): generate slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 6, 2019
1 parent 4cea068 commit eacbf38
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
25 changes: 23 additions & 2 deletions lib/schemaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const ghslugger = require('github-slugger');

const symbols = {
pointer: Symbol('pointer'),
filename: Symbol('filename'),
id: Symbol('id'),
titles: Symbol('titles'),
resolve: Symbol('resolve'),
slug: Symbol('slug'),
};

const myslug = Symbol('myslug');

const handler = ({
root = '', filename = '.', schemas, parent,
root = '', filename = '.', schemas, parent, slugger,
}) => {
const meta = {};

Expand Down Expand Up @@ -56,6 +61,19 @@ const handler = ({
}
};

meta[symbols.slug] = (target, prop, receiver) => {
if (!receiver[myslug] && !parent && receiver[symbols.filename]) {
receiver[myslug] = slugger.slug(receiver[symbols.filename].replace(/\..*$/, ''));
}
if (!receiver[myslug]) {
const parentslug = parent[symbols.slug];
const title = receiver.title;
const name = receiver[symbols.pointer].split('/').pop();
receiver[myslug] = slugger.slug(parentslug + '-' + (title ? title : name));
}
return receiver[myslug];
};

return {
ownKeys: target => Reflect.ownKeys(target),

Expand Down Expand Up @@ -84,6 +102,7 @@ const handler = ({
parent: receiver,
filename,
schemas,
slugger,
}));

if (subschema.$id) {
Expand All @@ -108,8 +127,10 @@ module.exports.loader = () => {
known: {},
};

const slugger = ghslugger();

return (schema, filename) => {
const proxied = new Proxy(schema, handler({ filename, schemas }));
const proxied = new Proxy(schema, handler({ filename, schemas, slugger }));
schemas.loaded.push(proxied);
if (proxied.$id) {
// stow away the schema for lookup
Expand Down
27 changes: 25 additions & 2 deletions test/schemaProxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

const assert = require('assert');
const {
loader, pointer, filename, id, titles, resolve,
loader, pointer, filename, id, titles, resolve, slug,
} = require('../lib/schemaProxy');

const example = {
Expand Down Expand Up @@ -48,6 +48,10 @@ const example = {
type: 'object',
title: 'An object',
},
zup: {
type: 'object',
title: 'An object',
},
baz: {
anyOf: [
{ $ref: '#/properties/foo' },
Expand All @@ -60,6 +64,7 @@ const example = {
const referencing = {
$schema: 'http://json-schema.org/draft-06/schema#',
$id: 'https://example.com/schemas/referencing',
title: 'Referencing',
properties: {
$ref: 'https://example.com/schemas/example#/properties',
zap: {
Expand Down Expand Up @@ -142,7 +147,25 @@ describe('Testing Schema Proxy', () => {

assert.deepStrictEqual(new Set(Object.keys(proxied2.properties)), new Set([
'$ref', 'zap', // the two properties from the original declaration
'foo', 'bar', 'zip', 'baz', // plus all the referenced properties
'foo', 'bar', 'zip', 'zup', 'baz', // plus all the referenced properties
]));
});

it('Schema proxy generates unique names', () => {
const myloader = loader();
const proxied1 = myloader(example, 'example.schema.json');
const proxied2 = myloader(referencing, 'referencing.schema.json');
const proxied3 = myloader({
title: 'Referencing'
}, 'anotherreference.schema.json');

assert.equal(proxied1[slug], 'example');

assert.equal(proxied1.properties.zip[slug], 'example-properties-an-object');
assert.equal(proxied1.properties.zup[slug], 'example-properties-an-object-1'); //avoid duplicates

assert.equal(proxied2[slug], 'referencing');
assert.equal(proxied2[slug], 'referencing'); // make sure the slug stays stable
assert.equal(proxied3[slug], 'anotherreference');
});
});

0 comments on commit eacbf38

Please sign in to comment.