Skip to content

Commit

Permalink
fix: do not swallow errors from dereferencing (#22)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: derefencing errors are thrown
  • Loading branch information
taras committed Jun 3, 2021
1 parent 631342c commit f6cf737
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
23 changes: 18 additions & 5 deletions index.js
Expand Up @@ -8,12 +8,21 @@ const fs = require('fs');
const readFileAsync = require('util').promisify(fs.readFile);
const oas3schema = require('./refs/oas3-schema.json');

function InvalidTypeError(message) {
this.name = 'InvalidTypeError';
this.message = message;
class InvalidTypeError extends Error {
constructor(message) {
super()
this.name = 'InvalidTypeError';
this.message = message;
}
}

InvalidTypeError.prototype = new Error();
class DereferencingError extends Error {
constructor(errors) {
super()
this.name = 'DereferencingError';
this.errors = errors;
}
}

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

if (dereference) {
({ result: schema } = await resolver.resolve(schema));
const result = await resolver.resolve(schema);
if (result.errors && result.errors.length > 0) {
throw new DereferencingError(result.errors)
}
schema = result.result;
}

const vocab = schemaWalker.getVocabulary(schema, schemaWalker.vocabularies.DRAFT_04);
Expand Down
20 changes: 20 additions & 0 deletions test/dereference_schema.test.js
Expand Up @@ -104,3 +104,23 @@ it('dereferencing schema with file references', async () => {

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

it('throws an error when dereferecing fails', async () => {
const schema = {
$schema: "http://json-schema.org/draft-04/schema#",
properties: {
foo: {
$ref: "./bad.json",
},
},
};

let error;
try {
await convert(schema, { dereference: true });
} catch (e) {
error = e;
}

should(error).have.property('errors')
})

0 comments on commit f6cf737

Please sign in to comment.