Skip to content

Commit

Permalink
refactor: update for Ajv version 5.1.0-beta
Browse files Browse the repository at this point in the history
use Ajv.MissingRefError (closes ajv-validator#6)
remove requirement to use v5 option (closes ajv-validator#7)
update meta-schema URI to draft-06
update compatible Ajv version
use Promise in async test
  • Loading branch information
epoberezkin committed Dec 27, 2016
1 parent 0466383 commit d408771
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 51 deletions.
9 changes: 2 additions & 7 deletions keywords/add_keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -46,6 +46,6 @@
"pre-commit": "^1.1.3"
},
"peerDependencies": {
"ajv": ">=4.6.0"
"ajv": ">=5.1.0-beta.0"
}
}
1 change: 1 addition & 0 deletions spec/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ globals:
describe: false
it: false
beforeEach: false
Promise: false
26 changes: 13 additions & 13 deletions spec/async.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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#" },
Expand All @@ -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#" },
Expand All @@ -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));
}
});
28 changes: 1 addition & 27 deletions spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
2 changes: 1 addition & 1 deletion spec/merge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand Down
2 changes: 1 addition & 1 deletion spec/patch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand Down

0 comments on commit d408771

Please sign in to comment.