Skip to content

Commit

Permalink
feat(proxy): resolve references
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 5, 2019
1 parent 1b30584 commit 4cea068
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 78 deletions.
63 changes: 50 additions & 13 deletions lib/schemaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ const symbols = {
filename: Symbol('filename'),
id: Symbol('id'),
titles: Symbol('titles'),
resolve: Symbol('resolve'),
};

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

meta[symbols.pointer] = () => root;
Expand All @@ -31,7 +34,7 @@ const handler = ({ root = '', filename = '.', schemas, parent}) => {
return parent[symbols.id];
}
return undefined;
}
};
meta[symbols.titles] = (target) => {
if (parent) {
// if we can determine the parent titles
Expand All @@ -40,7 +43,18 @@ const handler = ({ root = '', filename = '.', schemas, parent}) => {
}
// otherwise, it's just our own
return [target.title];
}
};

meta[symbols.resolve] = (target, prop, receiver) => (path) => {
const [head, ...tail] = typeof path === 'string' ? path.split('/') : path;
if ((head === '' || head === undefined) && tail.length === 0) {
return receiver;
} else if (head === '' || head === undefined) {
return receiver[symbols.resolve](tail);
} else {
return receiver[head][symbols.resolve](tail);
}
};

return {
ownKeys: target => Reflect.ownKeys(target),
Expand All @@ -52,12 +66,31 @@ const handler = ({ root = '', filename = '.', schemas, parent}) => {

const retval = Reflect.get(target, prop, receiver);
if (typeof retval === 'object') {
//console.log('making new proxy from', target, prop, 'receiver', receiver[symbols.id]);
const subschema = new Proxy(retval, handler({
root: `${root}/${prop}`,
if (retval.$ref) {
const [uri, pointer] = retval.$ref.split('#');
const basedoc = uri || receiver[symbols.id];
if (schemas.known[basedoc]) {
const referenced = schemas.known[basedoc][symbols.resolve](pointer);
// inject the referenced schema into the current schema
Object.assign(retval, referenced);
} else {
console.error('cannot resolve', basedoc);
}
}

// console.log('making new proxy from', target, prop, 'receiver', receiver[symbols.id]);
const subschema = new Proxy(retval, handler({
root: `${root}/${prop}`,
parent: receiver,
filename,
schemas }));
filename,
schemas,
}));

if (subschema.$id) {
// stow away the schema for lookup
// eslint-disable-next-line no-param-reassign
schemas.known[subschema.$id] = subschema;
}
return subschema;
}
return retval;
Expand All @@ -66,18 +99,22 @@ const handler = ({ root = '', filename = '.', schemas, parent}) => {
};

module.exports = {
...symbols
}
...symbols,
};

module.exports.loader = () => {
const schemas = {
loaded: [],
known: {}
known: {},
};

return (schema, filename) => {
const proxied = new Proxy(schema, handler({filename, schemas}));
const proxied = new Proxy(schema, handler({ filename, schemas }));
schemas.loaded.push(proxied);
if (proxied.$id) {
// stow away the schema for lookup
schemas.known[proxied.$id] = proxied;
}
return proxied;
};
}
};
162 changes: 97 additions & 65 deletions test/schemaProxy.test.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,148 @@
/*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */

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

const example = {
"meta:license": [
"Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
'meta:license': [
'Copyright 2017 Adobe Systems Incorporated. All rights reserved.',
"This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
"you may not use this file except in compliance with the License. You may obtain a copy",
"of the License at http://www.apache.org/licenses/LICENSE-2.0"
'you may not use this file except in compliance with the License. You may obtain a copy',
'of the License at http://www.apache.org/licenses/LICENSE-2.0',
],
$schema: "http://json-schema.org/draft-06/schema#",
$id: "https://example.com/schemas/example",
title: "Example",
type: "object",
$schema: 'http://json-schema.org/draft-06/schema#',
$id: 'https://example.com/schemas/example',
title: 'Example',
type: 'object',
description:
"This is an example schema with examples. Too many examples? There can never be too many examples!",
'This is an example schema with examples. Too many examples? There can never be too many examples!',
properties: {
foo: {
type: "string",
description: "A simple string.",
examples: ["bar"],
version: "1.0.0",
testProperty: "test"
type: 'string',
description: 'A simple string.',
examples: ['bar'],
version: '1.0.0',
testProperty: 'test',
},
bar: {
type: "string",
description: "A simple string.",
examples: ["bar", "baz"],
version: "1.0.0",
testProperty: "test"
type: 'string',
description: 'A simple string.',
examples: ['bar', 'baz'],
version: '1.0.0',
testProperty: 'test',
},
zip: {
type: "object",
title: 'An object'
type: 'object',
title: 'An object',
},
baz: {
"anyOf": [
{ "$ref": "#/properties/foo"},
{ "$ref": "#/properties/bar"}
]
}
}
anyOf: [
{ $ref: '#/properties/foo' },
{ $ref: '#/properties/bar' },
],
},
},
};

const referencing = {
$schema: "http://json-schema.org/draft-06/schema#",
$id: "https://example.com/schemas/referencing",
$schema: 'http://json-schema.org/draft-06/schema#',
$id: 'https://example.com/schemas/referencing',
properties: {
$ref: 'https://example.com/schemas/referencing#/properties'
}
}

describe("Testing Schema Proxy", () => {
$ref: 'https://example.com/schemas/example#/properties',
zap: {
type: 'boolean',
},
},
};

it("Schema Proxy creates a JSON schema", () => {
describe('Testing Schema Proxy', () => {
it('Schema Proxy creates a JSON schema', () => {
const proxied = loader()(example, 'example.schema.json');

assert.equal(proxied.title, "Example");
assert.equal(proxied.properties.foo.type, "string");
assert.equal(proxied.title, 'Example');
assert.equal(proxied.properties.foo.type, 'string');
});

it("Schema Proxy loads multiple JSON schemas", () => {
it('Schema Proxy loads multiple JSON schemas', () => {
const myloader = loader();
const proxied1 = myloader(example, 'example.schema.json');
const proxied2 = myloader(referencing, 'referencing.schema.json');

assert.equal(proxied1.title, "Example");
assert.equal(proxied2.$id, "https://example.com/schemas/referencing");

assert.equal(proxied1.title, 'Example');
assert.equal(proxied2.$id, 'https://example.com/schemas/referencing');
});

it("Schema Proxy creates a JSON schema with Pointers", () => {
it('Schema Proxy creates a JSON schema with Pointers', () => {
const proxied = loader()(example, 'example.schema.json');

assert.equal(proxied[pointer], "");
assert.equal(proxied.properties[pointer], "/properties");
assert.equal(proxied.properties.foo[pointer], "/properties/foo");
assert.equal(proxied['meta:license'][pointer], "/meta:license");
assert.equal(proxied.properties.baz.anyOf[0][pointer], "/properties/baz/anyOf/0");
assert.equal(proxied[pointer], '');
assert.equal(proxied.properties[pointer], '/properties');
assert.equal(proxied.properties.foo[pointer], '/properties/foo');
assert.equal(proxied['meta:license'][pointer], '/meta:license');
assert.equal(proxied.properties.baz.anyOf[0][pointer], '/properties/baz/anyOf/0');
});

it("Schema Proxy creates a JSON schema with ID References", () => {
it('Schema Proxy creates a JSON schema with ID References', () => {
const proxied = loader()(example, 'example.schema.json');

assert.equal(proxied[id], "https://example.com/schemas/example");
assert.equal(proxied.properties[pointer], "/properties");
assert.equal(proxied[id], 'https://example.com/schemas/example');
assert.equal(proxied.properties[pointer], '/properties');

assert.equal(proxied.properties[id], "https://example.com/schemas/example");
assert.equal(proxied.properties[pointer], "/properties");
assert.equal(proxied.properties[id], 'https://example.com/schemas/example');
assert.equal(proxied.properties[pointer], '/properties');

assert.equal(proxied.properties.foo[id], "https://example.com/schemas/example");
assert.equal(proxied['meta:license'][id], "https://example.com/schemas/example");
assert.equal(proxied.properties.baz.anyOf[0][id], "https://example.com/schemas/example");
assert.equal(proxied.properties.foo[id], 'https://example.com/schemas/example');
assert.equal(proxied['meta:license'][id], 'https://example.com/schemas/example');
assert.equal(proxied.properties.baz.anyOf[0][id], 'https://example.com/schemas/example');
});

it("Schema Proxy creates a JSON schema with Filename References", () => {
it('Schema Proxy creates a JSON schema with Filename References', () => {
const proxied = loader()(example, 'example.schema.json');

assert.equal(proxied[filename], "example.schema.json");
assert.equal(proxied.properties[filename], "example.schema.json");
assert.equal(proxied.properties.foo[filename], "example.schema.json");
assert.equal(proxied['meta:license'][filename], "example.schema.json");
assert.equal(proxied.properties.baz.anyOf[0][filename], "example.schema.json");
assert.equal(proxied[filename], 'example.schema.json');
assert.equal(proxied.properties[filename], 'example.schema.json');
assert.equal(proxied.properties.foo[filename], 'example.schema.json');
assert.equal(proxied['meta:license'][filename], 'example.schema.json');
assert.equal(proxied.properties.baz.anyOf[0][filename], 'example.schema.json');
});

it("Schema Proxy creates a JSON schema with title References", () => {
it('Schema Proxy creates a JSON schema with title References', () => {
const proxied = loader()(example, 'example.schema.json');

assert.deepStrictEqual(proxied[titles], ['Example']);
assert.deepStrictEqual(proxied.properties.zip[titles], ['Example', undefined, 'An object']);
});


it('Schema proxy resolves JSON Pointers', () => {
const myloader = loader();
const proxied1 = myloader(example, 'example.schema.json');

assert.deepStrictEqual(proxied1.properties, proxied1[resolve]('/properties'));
assert.deepStrictEqual(proxied1.properties.foo, proxied1[resolve]('/properties/foo'));
assert.deepStrictEqual(proxied1.properties.foo, proxied1.properties[resolve]('/foo'));
});

it('Schema proxy resolves Reference Pointers', () => {
const myloader = loader();
myloader(example, 'example.schema.json');
const proxied2 = myloader(referencing, 'referencing.schema.json');

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
]));
});
});

0 comments on commit 4cea068

Please sign in to comment.