diff --git a/index.js b/index.js index 7466bd9..02a9fff 100644 --- a/index.js +++ b/index.js @@ -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']; diff --git a/test/dereference_schema.test.js b/test/dereference_schema.test.js index e7b1111..9564be0 100644 --- a/test/dereference_schema.test.js +++ b/test/dereference_schema.test.js @@ -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#',