-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for arrays of mergeable objects in the “with” argument …
…of “$merge”
- Loading branch information
Alex Zaslavsky
committed
Oct 10, 2017
1 parent
d847ec3
commit e84920a
Showing
11 changed files
with
218 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
module.exports = function generateMetaSchema(patchSchema) { | ||
return { | ||
"type": "object", | ||
"required": [ "source", "with" ], | ||
"additionalProperties": false, | ||
"properties": { | ||
"source": { | ||
"anyOf": [ | ||
{ | ||
"type": "object", | ||
"required": [ "$ref" ], | ||
"additionalProperties": false, | ||
"properties": { | ||
"$ref": { | ||
"type": "string", | ||
"format": "uri" | ||
} | ||
} | ||
}, | ||
{ "$ref": "http://json-schema.org/draft-06/schema#" } | ||
] | ||
}, | ||
"with": patchSchema | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var url = require('url'); | ||
module.exports = function getSchema(ajv, it, $ref) { | ||
var id = it.baseId && it.baseId != '#' | ||
? url.resolve(it.baseId, $ref) | ||
: $ref; | ||
var validate = ajv.getSchema(id); | ||
if (validate) return validate.schema; | ||
throw new ajv.constructor.MissingRefError(it.baseId, $ref); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
'use strict'; | ||
|
||
var addKeyword = require('./add_keyword'); | ||
var jsonMergePatch = require('json-merge-patch'); | ||
var generateMetaSchema = require('./generateMetaSchema'); | ||
var getSchema = require('./getSchema'); | ||
var jsonPatch = require('json-merge-patch'); | ||
|
||
module.exports = function(ajv) { | ||
addKeyword(ajv, '$merge', jsonMergePatch, { "type": "object" }); | ||
module.exports = function merge(ajv) { | ||
ajv.addKeyword('$merge', { | ||
macro: function (schema, parentSchema, it) { | ||
var source = schema.source; | ||
var patches = schema.with instanceof Array ? schema.with : [schema.with]; | ||
if (source.$ref) source = JSON.parse(JSON.stringify(getSchema(ajv, it, source.$ref))); | ||
patches.forEach(function(patch) { | ||
if (patch.$ref) patch = getSchema(ajv, it, patch.$ref); | ||
jsonPatch.apply(source, patch, true); | ||
}); | ||
return source; | ||
}, | ||
metaSchema: generateMetaSchema({ | ||
"oneOf": [ | ||
{ | ||
"type": "object" | ||
}, | ||
{ | ||
"type": "array", | ||
"items": { | ||
"type": "object" | ||
} | ||
} | ||
] | ||
}) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
'use strict'; | ||
|
||
var addKeyword = require('./add_keyword'); | ||
var generateMetaSchema = require('./generateMetaSchema'); | ||
var getSchema = require('./getSchema'); | ||
var jsonPatch = require('fast-json-patch/src/json-patch'); | ||
|
||
module.exports = function(ajv) { | ||
addKeyword(ajv, '$patch', jsonPatch, { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"required": [ "op", "path" ], | ||
"properties": { | ||
"op": { "type": "string" }, | ||
"path": { "type": "string", "format": "json-pointer" } | ||
}, | ||
"anyOf": [ | ||
{ | ||
"properties": { "op": { "enum": [ "add", "replace", "test" ] } }, | ||
"required": [ "value" ] | ||
module.exports = function (ajv) { | ||
ajv.addKeyword('$patch', { | ||
macro: function (schema, parentSchema, it) { | ||
var source = schema.source; | ||
var patch = schema.with; | ||
if (source.$ref) source = JSON.parse(JSON.stringify(getSchema(ajv, it, source.$ref))); | ||
if (patch.$ref) patch = getSchema(ajv, it, patch.$ref); | ||
jsonPatch.applyPatch(source, patch, true); | ||
return source; | ||
}, | ||
metaSchema: generateMetaSchema({ | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"required": [ "op", "path" ], | ||
"properties": { | ||
"op": { "type": "string" }, | ||
"path": { "type": "string", "format": "json-pointer" } | ||
}, | ||
{ | ||
"properties": { "op": { "enum": [ "remove" ] } } | ||
}, | ||
{ | ||
"properties": { | ||
"op": { "enum": [ "move", "copy" ] }, | ||
"from": { "type": "string", "format": "json-pointer" } | ||
"anyOf": [ | ||
{ | ||
"properties": { "op": { "enum": [ "add", "replace", "test" ] } }, | ||
"required": [ "value" ] | ||
}, | ||
{ | ||
"properties": { "op": { "enum": [ "remove" ] } } | ||
}, | ||
"required": [ "from" ] | ||
} | ||
] | ||
} | ||
{ | ||
"properties": { | ||
"op": { "enum": [ "move", "copy" ] }, | ||
"from": { "type": "string", "format": "json-pointer" } | ||
}, | ||
"required": [ "from" ] | ||
} | ||
] | ||
} | ||
}) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.