Skip to content

Commit

Permalink
chore: change schema loader internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomourar committed Jul 20, 2021
1 parent c07e686 commit 7a756e9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/schemaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ module.exports.loader = () => {

const slugger = ghslugger();

return (schema, filename) => {
return (filename, schema) => {
// console.log('loading', filename);
const proxied = new Proxy(schema, handler({ filename, schemas, slugger }));
schemas.loaded.push(proxied);
Expand Down
8 changes: 4 additions & 4 deletions test/cyclicSchemaIntegration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ describe('Integration Test: Cyclic References', () => {
const myloader = loader();

console.log('Loading Schemas');
proxiedone = myloader(one, path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'));
proxiedtwo = myloader(two, path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'));
proxiedthree = myloader(three, path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));
proxiedone = myloader(path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'), one);
proxiedtwo = myloader(path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'), two);
proxiedthree = myloader(path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'), three);

console.log('Traversing Schemas');

Expand Down Expand Up @@ -72,7 +72,7 @@ describe('Integration Test: Cyclic References', () => {
});

it('Schemas with cyclic references get written to disk', () => {
const writer = writeSchema({ origindir: path.resolve(__dirname, 'fixtures', 'cyclic'), schemadir: path.resolve(__dirname, 'fixtures', 'cyclic-out') });
const writer = writeSchema({ originDir: path.resolve(__dirname, 'fixtures', 'cyclic'), schemaDir: path.resolve(__dirname, 'fixtures', 'cyclic-out') });
writer(allschemas);
const schemaone = fs.readJsonSync(path.resolve(__dirname, 'fixtures', 'cyclic-out', 'one.schema.json'));
assert.deepStrictEqual(schemaone, {
Expand Down
34 changes: 17 additions & 17 deletions test/markdownBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*/
/* eslint-env mocha */
/* eslint-disable no-unused-expressions */
const { assertMarkdown, loadschemas } = require('./testUtils');
const { assertMarkdown, loadSchemas } = require('./testUtils');

const build = require('../lib/markdownBuilder');

describe('Testing Markdown Builder: content', () => {
let results;

before(async () => {
const schemas = await loadschemas('content');
const schemas = await loadSchemas('content');
const builder = build({ header: false });
results = builder(schemas);
});
Expand All @@ -45,7 +45,7 @@ describe('Testing Markdown Builder: not', () => {
let results;

before(async () => {
const schemas = await loadschemas('not');
const schemas = await loadSchemas('not');
const builder = build({ header: false });
results = builder(schemas);
});
Expand All @@ -66,7 +66,7 @@ describe('Testing Markdown Builder: nullable', () => {
let results;

before(async () => {
const schemas = await loadschemas('nullable');
const schemas = await loadSchemas('nullable');
const builder = build({ header: false });
results = builder(schemas);
});
Expand All @@ -81,7 +81,7 @@ describe('Testing Markdown Builder: title', () => {
let results;

before(async () => {
const schemas = await loadschemas('title');
const schemas = await loadSchemas('title');
const builder = build({ header: false });
results = builder(schemas);
});
Expand All @@ -101,7 +101,7 @@ describe('Testing Markdown Builder: type', () => {
let results;

before(async () => {
const schemas = await loadschemas('type');
const schemas = await loadSchemas('type');
const builder = build({ header: false });
results = builder(schemas);
});
Expand All @@ -123,7 +123,7 @@ describe('Testing Markdown Builder: format', () => {
let results;

before(async () => {
const schemas = await loadschemas('format');
const schemas = await loadSchemas('format');
const builder = build({ header: false });
results = builder(schemas);
});
Expand All @@ -150,7 +150,7 @@ describe('Testing Markdown Builder: YAML examples', () => {
let results;

before(async () => {
const schemas = await loadschemas('format');
const schemas = await loadSchemas('format');
const builder = build({ header: false, exampleFormat: 'yaml' });
results = builder(schemas);
});
Expand All @@ -171,7 +171,7 @@ describe('Testing Markdown Builder: enums', () => {
let results;

before(async () => {
const schemas = await loadschemas('enums');
const schemas = await loadSchemas('enums');
const builder = build({ header: true, includeProperties: ['foo', 'bar'] });
results = builder(schemas);
});
Expand All @@ -195,7 +195,7 @@ describe('Testing Markdown Builder: null', () => {
let results;

before(async () => {
const schemas = await loadschemas('null');
const schemas = await loadSchemas('null');
const builder = build({ header: true });
results = builder(schemas);
});
Expand All @@ -212,7 +212,7 @@ describe('Testing Markdown Builder: additionalprops', () => {
let results;

before(async () => {
const schemas = await loadschemas('additionalprops');
const schemas = await loadSchemas('additionalprops');
const builder = build({ header: true });
results = builder(schemas);
});
Expand All @@ -236,7 +236,7 @@ describe('Testing Markdown Builder: types', () => {
let results;

before(async () => {
const schemas = await loadschemas('types');
const schemas = await loadSchemas('types');
const builder = build({ header: true });
results = builder(schemas);
});
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('Testing Markdown Builder: identifiable', () => {
let results;

before(async () => {
const schemas = await loadschemas('identifiable');
const schemas = await loadSchemas('identifiable');
const builder = build({ header: true });
results = builder(schemas);
});
Expand All @@ -287,7 +287,7 @@ describe('Testing Markdown Builder: arrays', () => {
let results;

before(async () => {
const schemas = await loadschemas('arrays');
const schemas = await loadSchemas('arrays');
const builder = build({ header: true });
results = builder(schemas);
});
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('Testing Markdown Builder: stringformats', () => {
let results;

before(async () => {
const schemas = await loadschemas('stringformats');
const schemas = await loadSchemas('stringformats');
const builder = build({ header: true });
results = builder(schemas);
});
Expand All @@ -356,7 +356,7 @@ describe('Testing Markdown Builder: readme-1', () => {
let results;

before(async () => {
const schemas = await loadschemas('readme-1');
const schemas = await loadSchemas('readme-1');
const builder = build({ header: true, links: { abstract: 'fooabstract.html' } });
results = builder(schemas);
});
Expand Down Expand Up @@ -416,7 +416,7 @@ describe('Testing Markdown Builder: Skip properties', () => {
let schemas;

before(async () => {
schemas = await loadschemas('skipproperties');
schemas = await loadSchemas('skipproperties');
});

it('Skipped properties exist', () => {
Expand Down
10 changes: 5 additions & 5 deletions test/readmeBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/* eslint-env mocha */
/* eslint-disable no-unused-expressions */
const assert = require('assert');
const { assertMarkdown, loadschemas } = require('./testUtils');
const { assertMarkdown, loadSchemas } = require('./testUtils');

const build = require('../lib/readmeBuilder');
const { loader } = require('../lib/schemaProxy');
Expand All @@ -28,7 +28,7 @@ describe('Testing Readme Builder', () => {
});

it('Readme Builder builds a README for type', async () => {
const schemas = await loadschemas('type');
const schemas = await loadSchemas('type');
const builder = build({ readme: true });
const result = builder(schemas);

Expand All @@ -38,7 +38,7 @@ describe('Testing Readme Builder', () => {
});

it('Readme Builder builds a medium README for multiple Schemas', async () => {
const schemas = await loadschemas('readme-1');
const schemas = await loadSchemas('readme-1');
const builder = build({ readme: true });
const result = builder(schemas);

Expand All @@ -58,7 +58,7 @@ describe('Testing Readme Builder', () => {
const builder = build({ readme: true });
const schemaloader = loader();
const schemas = [
schemaloader({
schemaloader('example.schema.json', {
type: 'object',
title: 'Test Schema',
description: 'Not much',
Expand All @@ -75,7 +75,7 @@ describe('Testing Readme Builder', () => {
title: 'An Array',
},
},
}, 'example.schema.json'),
}),
];

const result = builder(schemas);
Expand Down
30 changes: 15 additions & 15 deletions test/schemaProxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ const referencing = {

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

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

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

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

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

assert.equal(proxied[pointer], '');
assert.equal(proxied.properties[pointer], '/properties');
Expand All @@ -59,7 +59,7 @@ describe('Testing Schema Proxy', () => {
});

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

assert.equal(proxied[id], 'https://example.com/schemas/example');
assert.equal(proxied.properties[pointer], '/properties');
Expand All @@ -73,7 +73,7 @@ describe('Testing Schema Proxy', () => {
});

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

assert.equal(proxied[filename], 'example.schema.json');
assert.equal(proxied.properties[filename], 'example.schema.json');
Expand All @@ -83,15 +83,15 @@ describe('Testing Schema Proxy', () => {
});

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

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');
const proxied1 = myloader('example.schema.json', example);

assert.deepStrictEqual(proxied1.properties, proxied1[resolve]('/properties'));
assert.deepStrictEqual(proxied1.properties.foo, proxied1[resolve]('/properties/foo'));
Expand All @@ -100,8 +100,8 @@ describe('Testing Schema Proxy', () => {

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

assert.deepStrictEqual(new Set(Object.keys(proxied2.properties)), new Set([
'$ref', 'zap', // the two properties from the original declaration
Expand All @@ -111,8 +111,8 @@ describe('Testing Schema Proxy', () => {

it('Schema proxy generates unique names', () => {
const myloader = loader();
const proxied1 = myloader(example, 'example.schema.json');
const proxied2 = myloader(referencing, 'referencing.schema.json');
const proxied1 = myloader('example.schema.json', example);
const proxied2 = myloader('referencing.schema.json', referencing);
const proxied3 = myloader({
title: 'Referencing',
}, 'anotherreference.schema.json');
Expand All @@ -132,7 +132,7 @@ describe('Testing Schema Proxy', () => {

const examplefile = path.resolve(__dirname, '..', 'examples', 'schemas', 'definitions.schema.json');
// eslint-disable-next-line import/no-dynamic-require, global-require
const exampleschema = myloader(require(examplefile), examplefile);
const exampleschema = myloader(examplefile, require(examplefile));

assert.equal(exampleschema[meta].shortdescription, 'This is an example of using a definitions object within a schema');
});
Expand All @@ -152,7 +152,7 @@ describe('Testing Schema Proxy', () => {
const schemas = files.map((file) => {
const fname = path.resolve(__dirname, '..', 'examples', 'schemas', file);
// eslint-disable-next-line import/no-dynamic-require, global-require
return myloader(require(fname), fname);
return myloader(fname, require(fname));
});

assert.equal(schemas[0][slug], 'abstract');
Expand All @@ -178,7 +178,7 @@ describe('Testing Schema Proxy', () => {
const schemas = files.map((file) => {
const fname = path.resolve(__dirname, 'fixtures', 'deadref', file);
// eslint-disable-next-line import/no-dynamic-require, global-require
return myloader(require(fname), fname);
return myloader(fname, require(fname));
});

assert.equal(schemas[0][slug], 'deadref');
Expand Down
17 changes: 8 additions & 9 deletions test/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,15 @@ ${inspect(node)}`);
return null;
}

async function loadschemas(dir) {
async function loadSchemas(dir) {
const schemaloader = loader();
const schemadir = path.resolve(__dirname, 'fixtures', dir);
const schemas = await readdirp.promise(schemadir, { fileFilter: '*.schema.json' });
const schemaDir = path.resolve(__dirname, 'fixtures', dir);
const schemas = await readdirp.promise(schemaDir, { fileFilter: '*.schema.json' });

return traverse(schemas
.map(({ fullPath }) => schemaloader(
// eslint-disable-next-line global-require, import/no-dynamic-require
require(fullPath), fullPath,
)));
return traverse(schemas.map(({ fullPath }) => schemaloader(
// eslint-disable-next-line global-require, import/no-dynamic-require
fullPath, require(fullPath),
)));
}

module.exports = { assertMarkdown, loadschemas };
module.exports = { assertMarkdown, loadSchemas };
8 changes: 4 additions & 4 deletions test/traverseSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const example = {

describe('Testing Schema Traversal', () => {
it('Schema Traversal generates a list', () => {
const proxied = loader()(example, 'example.schema.json');
const proxied = loader()('example.schema.json', example);
const schemas = traverse([proxied]);

assert.equal(schemas.length, 11);
Expand All @@ -98,9 +98,9 @@ describe('Testing Schema Traversal', () => {
const three = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));

const myloader = loader();
const proxiedone = myloader(one, path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'));
const proxiedtwo = myloader(two, path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'));
const proxiedthree = myloader(three, path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));
const proxiedone = myloader(path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'), one);
const proxiedtwo = myloader(path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'), two);
const proxiedthree = myloader(path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'), three);

const schemas = traverse([proxiedone, proxiedtwo, proxiedthree]);

Expand Down

0 comments on commit 7a756e9

Please sign in to comment.