From 8fe8390933736348351662bb878b5ca17ed47c94 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Tue, 30 Aug 2016 20:43:44 +0100 Subject: [PATCH] feat: exception with useful message if $ref cannot be resolved --- keywords/add_keyword.js | 9 ++++--- spec/errors.spec.js | 57 +++++++++++++++++++++++++++++++++++++++++ spec/v5.spec.js | 33 ------------------------ 3 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 spec/errors.spec.js delete mode 100644 spec/v5.spec.js diff --git a/keywords/add_keyword.js b/keywords/add_keyword.js index eb125d4..1094763 100644 --- a/keywords/add_keyword.js +++ b/keywords/add_keyword.js @@ -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: { diff --git a/spec/errors.spec.js b/spec/errors.spec.js new file mode 100644 index 0000000..f0050c6 --- /dev/null +++ b/spec/errors.spec.js @@ -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); + }); + }); + }); +}); diff --git a/spec/v5.spec.js b/spec/v5.spec.js deleted file mode 100644 index ef0bf12..0000000 --- a/spec/v5.spec.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -var Ajv = require('ajv'); -var addKeywords = require('..'); -var addMerge = require('../keywords/merge'); -var addPatch = require('../keywords/patch'); -var assert = require('assert'); - -describe('v5 option is required', function() { - var ajv; - - 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); - }); - }); -});