From d4087719eb12cc9677a6bb4dd373087e633f64fd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Tue, 27 Dec 2016 19:21:23 +0000 Subject: [PATCH] refactor: update for Ajv version 5.1.0-beta use Ajv.MissingRefError (closes #6) remove requirement to use v5 option (closes #7) update meta-schema URI to draft-06 update compatible Ajv version use Promise in async test --- keywords/add_keyword.js | 9 ++------- package.json | 4 ++-- spec/.eslintrc.yml | 1 + spec/async.spec.js | 26 +++++++++++++------------- spec/errors.spec.js | 28 +--------------------------- spec/merge.spec.js | 2 +- spec/patch.spec.js | 2 +- 7 files changed, 21 insertions(+), 51 deletions(-) diff --git a/keywords/add_keyword.js b/keywords/add_keyword.js index b61a3cb..4bd542b 100644 --- a/keywords/add_keyword.js +++ b/keywords/add_keyword.js @@ -3,8 +3,6 @@ var url = require('url'); module.exports = function (ajv, keyword, jsonPatch, patchSchema) { - if (!ajv._opts.v5) - throw new Error('keyword ' + keyword + ' requires v5 option'); ajv.addKeyword(keyword, { macro: function (schema, parentSchema, it) { var source = schema.source; @@ -20,10 +18,7 @@ module.exports = function (ajv, keyword, jsonPatch, patchSchema) { : $ref; var validate = ajv.getSchema(id); if (validate) return validate.schema; - var err = new Error('can\'t resolve reference ' + $ref + ' from id ' + it.baseId); - err.missingRef = it.resolve.url(it.baseId, $ref); - err.missingSchema = it.resolve.normalizeId(it.resolve.fullPath(err.missingRef)); - throw err; + throw new ajv.constructor.MissingRefError(it.baseId, $ref); } }, metaSchema: { @@ -44,7 +39,7 @@ module.exports = function (ajv, keyword, jsonPatch, patchSchema) { } } }, - { "$ref": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#" } + { "$ref": "http://json-schema.org/draft-06/schema#" } ] }, "with": patchSchema diff --git a/package.json b/package.json index 734a2e6..da7b9d8 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "json-merge-patch": "^0.2.3" }, "devDependencies": { - "ajv": "^4.6.0", + "ajv": "^5.1.0-beta.0", "coveralls": "^2.11.12", "eslint": "^3.3.0", "if-node-version": "^1.0.0", @@ -46,6 +46,6 @@ "pre-commit": "^1.1.3" }, "peerDependencies": { - "ajv": ">=4.6.0" + "ajv": ">=5.1.0-beta.0" } } diff --git a/spec/.eslintrc.yml b/spec/.eslintrc.yml index 20871a9..38c1363 100644 --- a/spec/.eslintrc.yml +++ b/spec/.eslintrc.yml @@ -4,3 +4,4 @@ globals: describe: false it: false beforeEach: false + Promise: false diff --git a/spec/async.spec.js b/spec/async.spec.js index 5565be7..2465a85 100644 --- a/spec/async.spec.js +++ b/spec/async.spec.js @@ -9,13 +9,13 @@ describe('async schema loading', function() { var ajv, loadCount; beforeEach(function() { - ajv = new Ajv({v5: true, loadSchema: loadSchema}); + ajv = new Ajv({loadSchema: loadSchema}); addKeywords(ajv); loadCount = 0; }); describe('$merge', function() { - it('should load missing schemas', function (done) { + it('should load missing schemas', function() { var schema = { "$merge": { "source": { "$ref": "obj.json#" }, @@ -25,12 +25,12 @@ describe('async schema loading', function() { } }; - testAsync(schema, '$merge', done); + return testAsync(schema, '$merge'); }); }); describe('$patch', function() { - it('should load missing schemas', function (done) { + it('should load missing schemas', function() { var schema = { "$patch": { "source": { "$ref": "obj.json#" }, @@ -40,29 +40,29 @@ describe('async schema loading', function() { } }; - testAsync(schema, '$patch', done); + return testAsync(schema, '$patch'); }); }); - function testAsync(schema, keyword, done) { - ajv.compileAsync(schema, function (err, validate) { - if (err) done(err); + function testAsync(schema, keyword) { + return ajv.compileAsync(schema) + .then(function (validate) { assert.strictEqual(loadCount, 1); test(validate, keyword); - done(); }); } - function loadSchema(ref, callback) { + function loadSchema(ref) { if (ref == 'obj.json') { loadCount++; - return callback(null, { + var schema = { "id": "obj.json#", "type": "object", "properties": { "p": { "type": "string" } }, "additionalProperties": false - }); + }; + return Promise.resolve(schema); } - callback(new Error('404: ' + ref)); + return Promise.reject(new Error('404: ' + ref)); } }); diff --git a/spec/errors.spec.js b/spec/errors.spec.js index f0050c6..0550a13 100644 --- a/spec/errors.spec.js +++ b/spec/errors.spec.js @@ -2,40 +2,14 @@ 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 }); + ajv = new Ajv; addKeywords(ajv); }); diff --git a/spec/merge.spec.js b/spec/merge.spec.js index 0b2155c..c9a573b 100644 --- a/spec/merge.spec.js +++ b/spec/merge.spec.js @@ -9,7 +9,7 @@ describe('keyword $merge', function() { var ajvInstances; beforeEach(function() { - ajvInstances = [ new Ajv({v5: true}), new Ajv({v5: true}) ]; + ajvInstances = [ new Ajv, new Ajv ]; addKeywords(ajvInstances[0]); addMerge(ajvInstances[1]); }); diff --git a/spec/patch.spec.js b/spec/patch.spec.js index 42f7b09..11ea424 100644 --- a/spec/patch.spec.js +++ b/spec/patch.spec.js @@ -9,7 +9,7 @@ describe('keyword $patch', function() { var ajvInstances; beforeEach(function() { - ajvInstances = [ new Ajv({v5: true}), new Ajv({v5: true}) ]; + ajvInstances = [ new Ajv, new Ajv ]; addKeywords(ajvInstances[0]); addPatch(ajvInstances[1]); });