Skip to content

Commit

Permalink
feat: exception with useful message if $ref cannot be resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 30, 2016
1 parent 5fc705f commit 8fe8390
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
9 changes: 6 additions & 3 deletions keywords/add_keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ module.exports = function (ajv, keyword, jsonPatch, patchSchema) {
return source;

function getSchema($ref) {
if (it.baseId && it.baseId != '#')
$ref = url.resolve(it.baseId, $ref);
return ajv.getSchema($ref).schema;
var id = it.baseId && it.baseId != '#'
? url.resolve(it.baseId, $ref)
: $ref;
var validate = ajv.getSchema(id);
if (validate) return validate.schema;
throw new Error('can\'t resolve reference ' + $ref + ' from id ' + it.baseId);
}
},
metaSchema: {
Expand Down
57 changes: 57 additions & 0 deletions spec/errors.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

var Ajv = require('ajv');
var addKeywords = require('..');
var addMerge = require('../keywords/merge');
var addPatch = require('../keywords/patch');
var assert = require('assert');

describe('errors', function() {
var ajv;

describe('no v5 option', function() {
beforeEach(function() {
ajv = new Ajv;
});

it('adding $merge and $patch should throw without v5 option', function() {
assert.throws(function() {
addKeywords(ajv);
});
});

it('adding $merge only should throw without v5 option', function() {
assert.throws(function() {
addMerge(ajv);
});
});

it('adding $patch only should throw without v5 option', function() {
assert.throws(function() {
addPatch(ajv);
});
});
});

describe('missing $ref', function() {
beforeEach(function() {
ajv = new Ajv({ v5: true });
addKeywords(ajv);
});

it('should throw exception if cannot resolve $ref', function() {
var schema = {
"$merge": {
"source": { "$ref": "obj.json#" },
"with": {
"properties": { "q": { "type": "number" } }
}
}
};

assert.throws(function() {
ajv.compile(schema);
});
});
});
});
33 changes: 0 additions & 33 deletions spec/v5.spec.js

This file was deleted.

0 comments on commit 8fe8390

Please sign in to comment.