Skip to content

Commit

Permalink
fix: Applied minimum amount of changes to adapt ajv-merge-patch to aj…
Browse files Browse the repository at this point in the history
…v v7 and v8
  • Loading branch information
rpl committed Mar 29, 2021
1 parent 84cbb85 commit bf90e98
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion keywords/add_keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var url = require('url');

module.exports = function (ajv, keyword, jsonPatch, patchSchema) {
ajv.addKeyword(keyword, {
ajv.addKeyword({
keyword: keyword,
macro: function (schema, parentSchema, it) {
var source = schema.source;
var patch = schema.with;
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": "^6.5.1",
"ajv": "^7.2.4",
"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": ">=6.0.0"
"ajv": ">=7.0.0"
}
}
4 changes: 2 additions & 2 deletions spec/async.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Ajv = require('ajv');
var Ajv = require('ajv').default;
var addKeywords = require('..');
var test = require('./test_validate');
var assert = require('assert');
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('async schema loading', function() {
if (ref == 'obj.json') {
loadCount++;
var schema = {
"id": "obj.json#",
"$id": "obj.json#",
"type": "object",
"properties": { "p": { "type": "string" } },
"additionalProperties": false,
Expand Down
2 changes: 1 addition & 1 deletion spec/errors.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Ajv = require('ajv');
var Ajv = require('ajv').default;
var addKeywords = require('..');
var assert = require('assert');

Expand Down
2 changes: 1 addition & 1 deletion spec/merge.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Ajv = require('ajv');
var Ajv = require('ajv').default;
var addKeywords = require('..');
var addMerge = require('../keywords/merge');
var test = require('./test_validate');
Expand Down
2 changes: 1 addition & 1 deletion spec/patch.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Ajv = require('ajv');
var Ajv = require('ajv').default;
var addKeywords = require('..');
var addPatch = require('../keywords/patch');
var test = require('./test_validate');
Expand Down
22 changes: 16 additions & 6 deletions spec/test_validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@ var assert = require('assert');
module.exports = function (validate, keyword) {
assert.strictEqual(validate({ p: 'abc', q: 1 }), true);

// In ajv <= v7 the property is dataPath, while on ajv >= v8 is instancePath.
var assertDataOrInstancePath = function (err, expectedValue) {
assert.equal(
'dataPath' in err
? err.dataPath
: err.instancePath,
expectedValue
);
};

// property q should be a number
assert.strictEqual(validate({ p: 'foo', q: 'bar' }), false);
var errs = validate.errors;
assert.equal(errs.length, 2);
assert.equal(errs[0].keyword, 'type');
assert.equal(errs[0].dataPath, '.q');
assert.equal(errs[0].schemaPath, '#/properties/q/type');
assert.equal(errs[0].schemaPath, '#/' + keyword + '/properties/q/type');
assertDataOrInstancePath(errs[0], '/q');
assert.equal(errs[1].keyword, keyword);
assert.equal(errs[1].dataPath, '');
assertDataOrInstancePath(errs[1], '');
assert.equal(errs[1].schemaPath, '#/' + keyword);

// an object without q should fail
assert.strictEqual(validate({ p: 'foo' }), false);
errs = validate.errors;
assert.equal(errs.length, 2);
assert.equal(errs[0].keyword, 'required');
assert.equal(errs[0].dataPath, '');
assert.equal(errs[0].schemaPath, '#/required');
assert.equal(errs[0].schemaPath, '#/' + keyword + '/required');
assert.deepEqual(errs[0].params, { missingProperty: 'q' });
assertDataOrInstancePath(errs[0], '');
assert.equal(errs[1].keyword, keyword);
assert.equal(errs[1].dataPath, '');
assert.equal(errs[1].schemaPath, '#/' + keyword);
assertDataOrInstancePath(errs[1], '');
};

0 comments on commit bf90e98

Please sign in to comment.