MQTTSuite
Loading...
Searching...
No Matches
issue-25-default-values.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <nlohmann/json-schema.hpp>
3
4using nlohmann::json;
5using nlohmann::json_schema::json_validator;
6
7static const json person_schema = R"(
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;
59
60int main(void)
61{
62 json_validator validator{};
64
65 {
66 // add address which is optional that should generate a diff containing a default street
67 json person_emtpy_address = R"({
68 "name": "Hans",
69 "age": 69,
70 "address": {}
71 })"_json;
72
73 const auto default_patch = validator.validate(person_emtpy_address);
74
75 if (!default_patch.is_array()) {
76 std::cerr << "Patch with defaults is expected to be an array" << std::endl;
77 return 1;
78 }
79
80 if (default_patch.size() != 1) {
81 std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl;
82 return 1;
83 }
84
85 const auto &single_op = default_patch[0];
86
87 if (!single_op.contains("op")) {
88 std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl;
89 return 1;
90 }
91
92 if (single_op["op"].get<std::string>() != "add") {
93 std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl;
94 return 1;
95 }
96
97 if (!single_op.contains("path")) {
98 std::cerr << "Patch with defaults is expected to contain a path" << std::endl;
99 return 1;
100 }
101
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;
106 return 1;
107 }
108
109 if (!single_op.contains("value")) {
110 std::cerr << "Patch with defaults is expected to contain a value" << std::endl;
111 return 1;
112 }
113
114 if (single_op["value"].get<std::string>() != "Abbey Road") {
115 std::cerr << "Patch with defaults contains wrong value" << std::endl;
116 return 1;
117 }
118 }
119 {
120 // add address which is optional that should generate a diff containing a empty object
121 // but not work address which is null or other address which has no default
122 json person_missing_address = R"({
123 "name": "Hans",
124 "age": 69
125 })"_json;
126
127 const auto default_patch = validator.validate(person_missing_address);
128
129 if (!default_patch.is_array()) {
130 std::cerr << "Patch with defaults is expected to be an array" << std::endl;
131 return 1;
132 }
133
134 if (default_patch.size() != 1) {
135 std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl;
136 return 1;
137 }
138
139 const auto &single_op = default_patch[0];
140
141 if (!single_op.contains("op")) {
142 std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl;
143 return 1;
144 }
145
146 if (single_op["op"].get<std::string>() != "add") {
147 std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl;
148 return 1;
149 }
150
151 if (!single_op.contains("path")) {
152 std::cerr << "Patch with defaults is expected to contain a path" << std::endl;
153 return 1;
154 }
155
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;
160 return 1;
161 }
162
163 if (!single_op.contains("value")) {
164 std::cerr << "Patch with defaults is expected to contain a value" << std::endl;
165 return 1;
166 }
167
168 if (!single_op["value"].is_object() || !single_op["value"].empty()) {
169 std::cerr << "Patch with defaults contains wrong value" << std::endl;
170 return 1;
171 }
172 }
173 return 0;
174}
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
int main()
Definition format.cpp:34
static const json person_schema