Skip to content

Commit 84a50bd

Browse files
committed
added convert.sync method to make runnign without await easier
1 parent 94d5363 commit 84a50bd

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ async function convert(schema, options = {}) {
3131
return schema;
3232
}
3333

34+
// we want to expose a non-async function that will skip dereferencing
35+
// but because the module only exposes a single function, we attach it to the main function
36+
// so we dont have to introduce a breaking change
37+
convert.sync = function convertSync(schema, options = {}) {
38+
const { cloneSchema = true } = options;
39+
40+
if (cloneSchema) {
41+
schema = JSON.parse(JSON.stringify(schema));
42+
}
43+
44+
const vocab = schemaWalker.getVocabulary(schema, schemaWalker.vocabularies.DRAFT_04);
45+
schemaWalker.schemaWalk(schema, convertSchema, null, vocab);
46+
return schema;
47+
}
48+
49+
3450
function stripIllegalKeywords(schema) {
3551
delete schema['$schema'];
3652
delete schema['$id'];

test/dereference_schema.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ it('not dereferencing schema by default', async () => {
2626
should(result).deepEqual(expected, 'result does not match the expected');
2727
});
2828

29+
it('running convert.sync is sync version equivalent to dereference: false', () => {
30+
const schema = {
31+
$schema: 'http://json-schema.org/draft-04/schema#',
32+
properties: {
33+
foo: {
34+
$ref: '#/definitions/foo',
35+
},
36+
},
37+
definitions: {
38+
foo: ['string', 'null'],
39+
},
40+
};
41+
42+
const result = convert.sync(JSON.parse(JSON.stringify(schema)));
43+
44+
const expected = { ...schema };
45+
delete expected.$schema;
46+
47+
should(result).deepEqual(expected, 'result does not match the expected');
48+
});
49+
2950
it('dereferencing schema with deference option', async () => {
3051
const schema = {
3152
$schema: 'http://json-schema.org/draft-04/schema#',

0 commit comments

Comments
 (0)