Skip to content

Commit

Permalink
Changes infoemed by review by epoberezkin
Browse files Browse the repository at this point in the history
* remove extra parameter from add_keyword; instead change the invoke targets to pass function
* make the tests all require 'q' property; confirm it in test_validate.
  • Loading branch information
donaldjarmstrong committed Mar 12, 2018
1 parent 49cf145 commit 5708ba9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions keywords/add_keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

var url = require('url');

module.exports = function (ajv, keyword, jsonPatch, func, patchSchema) {
module.exports = function (ajv, keyword, jsonPatch, patchSchema) {
ajv.addKeyword(keyword, {
macro: function (schema, parentSchema, it) {
var source = schema.source;
var patch = schema.with;
if (source.$ref) source = JSON.parse(JSON.stringify(getSchema(source.$ref)));
if (patch.$ref) patch = getSchema(patch.$ref);
jsonPatch[func].call(null, source, patch, true);
jsonPatch.call(null, source, patch, true);
return source;

function getSchema($ref) {
Expand Down
2 changes: 1 addition & 1 deletion keywords/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ var addKeyword = require('./add_keyword');
var jsonMergePatch = require('json-merge-patch');

module.exports = function(ajv) {
addKeyword(ajv, '$merge', jsonMergePatch, 'apply', { "type": "object" });
addKeyword(ajv, '$merge', jsonMergePatch.apply, { "type": "object" });
};
2 changes: 1 addition & 1 deletion keywords/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var addKeyword = require('./add_keyword');
var jsonPatch = require('fast-json-patch');

module.exports = function(ajv) {
addKeyword(ajv, '$patch', jsonPatch, 'applyPatch', {
addKeyword(ajv, '$patch', jsonPatch.applyPatch, {
"type": "array",
"items": {
"type": "object",
Expand Down
9 changes: 6 additions & 3 deletions spec/async.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('async schema loading', function() {
"$merge": {
"source": { "$ref": "obj.json#" },
"with": {
"properties": { "q": { "type": "number" } }
"properties": { "q": { "type": "number" } },
"required": [ "q" ]
}
}
};
Expand All @@ -35,7 +36,8 @@ describe('async schema loading', function() {
"$patch": {
"source": { "$ref": "obj.json#" },
"with": [
{ "op": "add", "path": "/properties/q", "value": { "type": "number" } }
{ "op": "add", "path": "/properties/q", "value": { "type": "number" } },
{ "op": "add", "path": "/required/-", "value": "q" }
]
}
};
Expand All @@ -59,7 +61,8 @@ describe('async schema loading', function() {
"id": "obj.json#",
"type": "object",
"properties": { "p": { "type": "string" } },
"additionalProperties": false
"additionalProperties": false,
"required": [ "p" ]
};
return Promise.resolve(schema);
}
Expand Down
18 changes: 12 additions & 6 deletions spec/merge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('keyword $merge', function() {
"additionalProperties": false
},
"with": {
"properties": { "q": { "type": "number" } }
"properties": { "q": { "type": "number" } },
"required": [ "q" ]
}
}
};
Expand All @@ -53,7 +54,8 @@ describe('keyword $merge', function() {
"$merge": {
"source": { "$ref": "obj.json#" },
"with": {
"properties": { "q": { "type": "number" } }
"properties": { "q": { "type": "number" } },
"required": [ "q" ]
}
}
};
Expand All @@ -79,7 +81,8 @@ describe('keyword $merge', function() {
"$merge": {
"source": { "$ref": "#/definitions/source" },
"with": {
"properties": { "q": { "type": "number" } }
"properties": { "q": { "type": "number" } },
"required": [ "q" ]
}
}
};
Expand All @@ -96,13 +99,15 @@ describe('keyword $merge', function() {
var sourceSchema = {
"type": "object",
"properties": { "p": { "type": "string" } },
"additionalProperties": false
"additionalProperties": false,
"required": [ "p" ]
};

var patchSchema = {
"type": "object",
"properties": { "q": { "type": "number" } },
"additionalProperties": false
"additionalProperties": false,
"required": [ "q" ]
};

ajv.addSchema(sourceSchema, "obj1.json#");
Expand Down Expand Up @@ -138,7 +143,8 @@ describe('keyword $merge', function() {
"patch":{
"type": "object",
"properties": { "q": { "type": "number" } },
"additionalProperties": false
"additionalProperties": false,
"required" : [ "q" ]
}
},
"$merge": {
Expand Down
14 changes: 14 additions & 0 deletions spec/test_validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var assert = require('assert');

module.exports = function (validate, keyword) {
assert.strictEqual(validate({ p: 'abc', q: 1 }), true);

// property q should be a number
assert.strictEqual(validate({ p: 'foo', q: 'bar' }), false);
var errs = validate.errors;
assert.equal(errs.length, 2);
Expand All @@ -13,4 +15,16 @@ module.exports = function (validate, keyword) {
assert.equal(errs[1].keyword, keyword);
assert.equal(errs[1].dataPath, '');
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.deepEqual(errs[0].params, { missingProperty: 'q' });
assert.equal(errs[1].keyword, keyword);
assert.equal(errs[1].dataPath, '');
assert.equal(errs[1].schemaPath, '#/' + keyword);
};

0 comments on commit 5708ba9

Please sign in to comment.