Something went wrong. Try again.
R package to validate JSON with schemas
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116#include "json-patch.h"
#include "nlohmann/json-schema.hpp"
namespace{
// originally from http://jsonpatch.com/, http://json.schemastore.org/json-patch// with fixesconst nlohmann::json patch_schema = R"patch({ "title": "JSON schema for JSONPatch files", "$schema": "http://json-schema.org/draft-04/schema#", "type": "array",
"items": { "oneOf": [ { "additionalProperties": false, "required": [ "value", "op", "path"], "properties": { "path" : { "$ref": "#/definitions/path" }, "op": { "description": "The operation to perform.", "type": "string", "enum": [ "add", "replace", "test" ] }, "value": { "description": "The value to add, replace or test." } } }, { "additionalProperties": false, "required": [ "op", "path"], "properties": { "path" : { "$ref": "#/definitions/path" }, "op": { "description": "The operation to perform.", "type": "string", "enum": [ "remove" ] } } }, { "additionalProperties": false, "required": [ "from", "op", "path" ], "properties": { "path" : { "$ref": "#/definitions/path" }, "op": { "description": "The operation to perform.", "type": "string", "enum": [ "move", "copy" ] }, "from": { "$ref": "#/definitions/path", "description": "A JSON Pointer path pointing to the location to move/copy from." } } } ] }, "definitions": { "path": { "description": "A JSON Pointer path.", "type": "string" } }})patch"_json;} // namespace
namespace nlohmann{
json_patch::json_patch(json &&patch) : j_(std::move(patch)){ validateJsonPatch(j_);}
json_patch::json_patch(const json &patch) : j_(std::move(patch)){ validateJsonPatch(j_);}
json_patch &json_patch::add(const json::json_pointer &ptr, json value){ j_.push_back(json{{"op", "add"}, {"path", ptr.to_string()}, {"value", std::move(value)}}); return *this;}
json_patch &json_patch::replace(const json::json_pointer &ptr, json value){ j_.push_back(json{{"op", "replace"}, {"path", ptr.to_string()}, {"value", std::move(value)}}); return *this;}
json_patch &json_patch::remove(const json::json_pointer &ptr){ j_.push_back(json{{"op", "remove"}, {"path", ptr.to_string()}}); return *this;}
void json_patch::validateJsonPatch(json const &patch){ // static put here to have it created at the first usage of validateJsonPatch static nlohmann::json_schema::json_validator patch_validator(patch_schema);
patch_validator.validate(patch);
for (auto const &op : patch) json::json_pointer(op["path"].get<std::string>());}
} // namespace nlohmann