Skip to content

Commit f6cf737

Browse files
authored
fix: do not swallow errors from dereferencing (#22)
BREAKING CHANGE: derefencing errors are thrown
1 parent 631342c commit f6cf737

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

Diff for: index.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ const fs = require('fs');
88
const readFileAsync = require('util').promisify(fs.readFile);
99
const oas3schema = require('./refs/oas3-schema.json');
1010

11-
function InvalidTypeError(message) {
12-
this.name = 'InvalidTypeError';
13-
this.message = message;
11+
class InvalidTypeError extends Error {
12+
constructor(message) {
13+
super()
14+
this.name = 'InvalidTypeError';
15+
this.message = message;
16+
}
1417
}
1518

16-
InvalidTypeError.prototype = new Error();
19+
class DereferencingError extends Error {
20+
constructor(errors) {
21+
super()
22+
this.name = 'DereferencingError';
23+
this.errors = errors;
24+
}
25+
}
1726

1827
async function convert(schema, options = {}) {
1928
const { cloneSchema = true, dereference = false } = options;
@@ -23,7 +32,11 @@ async function convert(schema, options = {}) {
2332
}
2433

2534
if (dereference) {
26-
({ result: schema } = await resolver.resolve(schema));
35+
const result = await resolver.resolve(schema);
36+
if (result.errors && result.errors.length > 0) {
37+
throw new DereferencingError(result.errors)
38+
}
39+
schema = result.result;
2740
}
2841

2942
const vocab = schemaWalker.getVocabulary(schema, schemaWalker.vocabularies.DRAFT_04);

Diff for: test/dereference_schema.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,23 @@ it('dereferencing schema with file references', async () => {
104104

105105
should(result).deepEqual(expected, 'result does not match the expected');
106106
});
107+
108+
it('throws an error when dereferecing fails', async () => {
109+
const schema = {
110+
$schema: "http://json-schema.org/draft-04/schema#",
111+
properties: {
112+
foo: {
113+
$ref: "./bad.json",
114+
},
115+
},
116+
};
117+
118+
let error;
119+
try {
120+
await convert(schema, { dereference: true });
121+
} catch (e) {
122+
error = e;
123+
}
124+
125+
should(error).have.property('errors')
126+
})

0 commit comments

Comments
 (0)