MQTTSuite
Loading...
Searching...
No Matches
errors.cpp
Go to the documentation of this file.
1#include <nlohmann/json-schema.hpp>
2
3#include <iostream>
4
5static int error_count;
6
7#define EXPECT_EQ(a, b)
8 do {
9 if (a != b) {
10 std::cerr << "Failed: '" << a << "' != '" << b << "'\n";
11 error_count++;
12 }
13 } while (0)
14
15using nlohmann::json;
16using nlohmann::json_schema::json_validator;
17
18namespace
19{
20
21// The schema is defined based upon a string literal
22static json person_schema = R"(
23{
24 "$schema": "http://json-schema.org/draft-07/schema#",
25 "title": "A person",
26 "properties": {
27 "name": {
28 "description": "Name",
29 "type": "string"
30 },
31 "age": {
32 "description": "Age of the person",
33 "type": "number",
34 "minimum": 2,
35 "maximum": 200
36 },
37 "phones": {
38 "type": "array",
39 "items": {
40 "type": "number"
41 }
42 }
43 },
44 "required": [
45 "name",
46 "age"
47 ],
48 "additionalProperties": false,
49 "type": "object"
50})"_json;
51
53{
54 void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
55 {
57 std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
58 failed_pointers.push_back(ptr);
59 }
60
61public:
62 std::vector<nlohmann::json::json_pointer> failed_pointers;
63
69};
70
71} // namespace
72
73int main(void)
74{
75 json_validator validator;
76
77 try {
78 validator.set_root_schema(person_schema); // insert root-schema
79 } catch (const std::exception &e) {
80 std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
81 return EXIT_FAILURE;
82 }
83
85
86 validator.validate({{"age", 42}, {"name", "John"}}, err); // OK
87 EXPECT_EQ(err.failed_pointers.size(), 0);
88 err.reset();
89
90 validator.validate({{"age", 42}}, err); // no name
91
92 EXPECT_EQ(err.failed_pointers.size(), 1);
93 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
94 err.reset();
95
96 validator.validate({{"street", "Boulevard"}}, err); // no name and no age
97 EXPECT_EQ(err.failed_pointers.size(), 3);
98 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
99 EXPECT_EQ(err.failed_pointers[1].to_string(), "");
100 EXPECT_EQ(err.failed_pointers[2].to_string(), "");
101 err.reset();
102
103 validator.validate({{"age", 42}, {"name", 12}}, err); // name must be a string
104 EXPECT_EQ(err.failed_pointers.size(), 1);
105 EXPECT_EQ(err.failed_pointers[0].to_string(), "/name");
106 err.reset();
107
108 validator.validate({
109 {"age", 42},
110 {"name", "John"},
111 {"phones", {1234, "223"}},
112 },
113 err); // name must be a string
114 EXPECT_EQ(err.failed_pointers.size(), 1);
115 EXPECT_EQ(err.failed_pointers[0].to_string(), "/phones/1");
116 err.reset();
117
118 validator.validate({
119 {"age", 42},
120 {"name", "John"},
121 {"phones", {0}},
122 {"post-code", 12345},
123 },
124 err); // name must be a string
125 EXPECT_EQ(err.failed_pointers.size(), 1);
126 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
127 err.reset();
128
129 return error_count;
130}
#define EXPECT_EQ(a, b)
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
Definition errors.cpp:54
std::vector< nlohmann::json::json_pointer > failed_pointers
Definition errors.cpp:62
void error(const json::json_pointer &, const json &, const std::string &) override
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
static int error_count
Definition errors.cpp:5
int main()
Definition format.cpp:34