Go to the documentation of this file.
2#include <nlohmann/json-schema.hpp>
8{
9 "$schema": "http://json-schema.org/draft-07/schema#",
10 "title": "A person",
11 "properties": {
12 "name": {
13 "description": "Name",
14 "type": "string"
15 },
16 "age": {
17 "description": "Age of the person",
18 "type": "number",
19 "minimum": 2,
20 "maximum": 200
21 },
22 "address": {
23 "type": "object",
24 "default": {},
25 "properties": {
26 "street": {
27 "type": "string",
28 "default": "Abbey Road"
29 }
30 }
31 },
32 "work address": {
33 "type": "object",
34 "default": null,
35 "properties": {
36 "street": {
37 "type": "string",
38 "default": "Abbey Road"
39 }
40 }
41 },
42 "other address": {
43 "type": "object",
44 "properties": {
45 "street": {
46 "type": "string",
47 "default": "Abbey Road"
48 }
49 }
50 }
51 },
52 "required": [
53 "name",
54 "age"
55 ],
56 "additionalProperties": false,
57 "type": "object"
58})"_json;
67 json person_emtpy_address = R"({
68 "name": "Hans",
69 "age": 69,
70 "address": {}
71 })"_json;
73 const auto default_patch = validator
.validate(person_emtpy_address
);
75 if (!default_patch.is_array()) {
76 std::cerr <<
"Patch with defaults is expected to be an array" << std::endl;
80 if (default_patch.size() != 1) {
81 std::cerr <<
"Patch with defaults is expected to contain one opperation" << std::endl;
85 const auto &single_op = default_patch[0];
87 if (!single_op.contains(
"op")) {
88 std::cerr <<
"Patch with defaults is expected to contain opperation entry" << std::endl;
92 if (single_op[
"op"].get<std::string>() !=
"add") {
93 std::cerr <<
"Patch with defaults is expected to contain add opperation" << std::endl;
97 if (!single_op.contains(
"path")) {
98 std::cerr <<
"Patch with defaults is expected to contain a path" << std::endl;
102 const auto &readPath = single_op[
"path"].get<std::string>();
103 if (readPath !=
"/address/street") {
104 std::cerr <<
"Patch with defaults contains wrong path. It is " << readPath <<
" and should be "
105 <<
"/address/street" << std::endl;
109 if (!single_op.contains(
"value")) {
110 std::cerr <<
"Patch with defaults is expected to contain a value" << std::endl;
114 if (single_op[
"value"].get<std::string>() !=
"Abbey Road") {
115 std::cerr <<
"Patch with defaults contains wrong value" << std::endl;
122 json person_missing_address = R"({
123 "name": "Hans",
124 "age": 69
125 })"_json;
127 const auto default_patch = validator
.validate(person_missing_address
);
129 if (!default_patch.is_array()) {
130 std::cerr <<
"Patch with defaults is expected to be an array" << std::endl;
134 if (default_patch.size() != 1) {
135 std::cerr <<
"Patch with defaults is expected to contain one opperation" << std::endl;
139 const auto &single_op = default_patch[0];
141 if (!single_op.contains(
"op")) {
142 std::cerr <<
"Patch with defaults is expected to contain opperation entry" << std::endl;
146 if (single_op[
"op"].get<std::string>() !=
"add") {
147 std::cerr <<
"Patch with defaults is expected to contain add opperation" << std::endl;
151 if (!single_op.contains(
"path")) {
152 std::cerr <<
"Patch with defaults is expected to contain a path" << std::endl;
156 const auto &readPath = single_op[
"path"].get<std::string>();
157 if (readPath !=
"/address") {
158 std::cerr <<
"Patch with defaults contains wrong path. It is " << readPath <<
" and should be "
159 <<
"/address" << std::endl;
163 if (!single_op.contains(
"value")) {
164 std::cerr <<
"Patch with defaults is expected to contain a value" << std::endl;
168 if (!single_op[
"value"].is_object() || !single_op[
"value"].empty()) {
169 std::cerr <<
"Patch with defaults contains wrong value" << std::endl;
void set_root_schema(const json &)
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
json validate(const json &) const
static const json person_schema