Skip to content

convert.sync - sync version that does not dereference #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ async function convert(schema, options = {}) {
return schema;
}

// we want to expose a non-async function that will skip dereferencing
// but because the module only exposes a single function, we attach it to the main function
// so we dont have to introduce a breaking change
convert.sync = function convertSync(schema, options = {}) {
const { cloneSchema = true } = options;

if (cloneSchema) {
schema = JSON.parse(JSON.stringify(schema));
}

const vocab = schemaWalker.getVocabulary(schema, schemaWalker.vocabularies.DRAFT_04);
schemaWalker.schemaWalk(schema, convertSchema, null, vocab);
return schema;
}


function stripIllegalKeywords(schema) {
delete schema['$schema'];
delete schema['$id'];
Expand Down
21 changes: 21 additions & 0 deletions test/dereference_schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ it('not dereferencing schema by default', async () => {
should(result).deepEqual(expected, 'result does not match the expected');
});

it('running convert.sync is sync version equivalent to dereference: false', () => {
const schema = {
$schema: 'http://json-schema.org/draft-04/schema#',
properties: {
foo: {
$ref: '#/definitions/foo',
},
},
definitions: {
foo: ['string', 'null'],
},
};

const result = convert.sync(JSON.parse(JSON.stringify(schema)));

const expected = { ...schema };
delete expected.$schema;

should(result).deepEqual(expected, 'result does not match the expected');
});

it('dereferencing schema with deference option', async () => {
const schema = {
$schema: 'http://json-schema.org/draft-04/schema#',
Expand Down