Skip to content

Commit

Permalink
Update to latest fast-json-patch
Browse files Browse the repository at this point in the history
* fix the warning 'jsonpatch.apply is deprecated, please use `applyPatch` for applying patch sequences, or `applyOperation` to apply individual operations.'
* Update to 2.0.6 version of fast-json-patch and address the deprecation warning
* Update tests to include patch array of operations
* Update docs with an example reason to use patch with a link to the json-patch spec
  • Loading branch information
donaldjarmstrong committed Mar 2, 2018
1 parent bc61807 commit 49cf145
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The same schema extension using `$patch` keyword:
`$patch` is implemented as a [custom macro keyword](https://github.com/epoberezkin/ajv/blob/master/CUSTOM.md#define-keyword-with-macro-function) using [fast-json-patch](https://github.com/Starcounter-Jack/JSON-Patch) package.


In the majority of cases `$merge` format is easier to understand and to maintain. `$patch` can be used for extensions and changes that cannot be expressed using `$merge`.
In the majority of cases `$merge` format is easier to understand and to maintain. `$patch` can be used for extensions and changes that cannot be expressed using `$merge`, e.g. [Adding an array value](https://tools.ietf.org/html/rfc6902#page-18).

`with` property in keywords can also be a reference to a part of some schema, in which case the resolved value will be used rather than the actual object with property `$ref`.

Expand Down
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, patchSchema) {
module.exports = function (ajv, keyword, jsonPatch, func, 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.apply(source, patch, true);
jsonPatch[func].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, { "type": "object" });
addKeyword(ajv, '$merge', jsonMergePatch, 'apply', { "type": "object" });
};
4 changes: 2 additions & 2 deletions keywords/patch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var addKeyword = require('./add_keyword');
var jsonPatch = require('fast-json-patch/src/json-patch');
var jsonPatch = require('fast-json-patch');

module.exports = function(ajv) {
addKeyword(ajv, '$patch', jsonPatch, {
addKeyword(ajv, '$patch', jsonPatch, 'applyPatch', {
"type": "array",
"items": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"homepage": "https://github.com/epoberezkin/ajv-merge-patch#readme",
"dependencies": {
"fast-json-patch": "^1.0.0",
"fast-json-patch": "^2.0.6",
"json-merge-patch": "^0.2.3"
},
"devDependencies": {
Expand Down
18 changes: 12 additions & 6 deletions spec/patch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ describe('keyword $patch', function() {
"source": {
"type": "object",
"properties": { "p": { "type": "string" } },
"additionalProperties": false
"additionalProperties": false,
"required": [ "p" ]
},
"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 @@ -44,7 +46,8 @@ describe('keyword $patch', function() {
"id": "obj.json#",
"type": "object",
"properties": { "p": { "type": "string" } },
"additionalProperties": false
"additionalProperties": false,
"required": [ "p" ]
};

ajv.addSchema(sourceSchema);
Expand All @@ -53,7 +56,8 @@ describe('keyword $patch', 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 @@ -73,13 +77,15 @@ describe('keyword $patch', function() {
"source": {
"type": "object",
"properties": { "p": { "type": "string" } },
"additionalProperties": false
"additionalProperties": false,
"required": [ "p" ]
}
},
"$patch": {
"source": { "$ref": "#/definitions/source" },
"with": [
{ "op": "add", "path": "/properties/q", "value": { "type": "number" } }
{ "op": "add", "path": "/properties/q", "value": { "type": "number" } },
{ "op": "add", "path": "/required/-", "value": "q" }
]
}
};
Expand Down

0 comments on commit 49cf145

Please sign in to comment.