Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest fast-json-patch #23

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can pass jsonPatch.applyPatch here and jsonMergePatch.apply above. Then in add_keyword you would just need to use jsonPatch(null, source, patch, true); without any additional parameters.

"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" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to add "required" keyword and its change with "patch", then it has to be added with "merge" as well (it can replace the whole array) and also tested in "test_validate.js" (object without "q" should fail); as is it is not tested at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the review. Changes submitted

]
}
};
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