MQTTSuite
Loading...
Searching...
No Matches
issue-70-root-schema-constructor.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
74
75int main(void)
76{
78
79 validator.validate({{"age", 42}, {"name", "John"}}, err); // OK
80 EXPECT_EQ(err.failed_pointers.size(), 0);
81 err.reset();
82
83 validator.validate({{"age", 42}}, err); // no name
84
85 EXPECT_EQ(err.failed_pointers.size(), 1);
86 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
87 err.reset();
88
89 validator.validate({{"street", "Boulevard"}}, err); // no name and no age
90 EXPECT_EQ(err.failed_pointers.size(), 3);
91 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
92 EXPECT_EQ(err.failed_pointers[1].to_string(), "");
93 EXPECT_EQ(err.failed_pointers[2].to_string(), "");
94 err.reset();
95
96 validator.validate({{"age", 42}, {"name", 12}}, err); // name must be a string
97 EXPECT_EQ(err.failed_pointers.size(), 1);
98 EXPECT_EQ(err.failed_pointers[0].to_string(), "/name");
99 err.reset();
100
102 {"age", 42},
103 {"name", "John"},
104 {"phones", {1234, "223"}},
105 },
106 err); // name must be a string
107 EXPECT_EQ(err.failed_pointers.size(), 1);
108 EXPECT_EQ(err.failed_pointers[0].to_string(), "/phones/1");
109 err.reset();
110
112 {"age", 42},
113 {"name", "John"},
114 {"phones", {0}},
115 {"post-code", 12345},
116 },
117 err); // name must be a string
118 EXPECT_EQ(err.failed_pointers.size(), 1);
119 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
120 err.reset();
121
122 return error_count;
123}
#define EXPECT_EQ(a, b)
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
void error(const json::json_pointer &, const json &, const std::string &) override
json_validator(const json &, schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
int main()
Definition format.cpp:34
static json_validator validator(person_schema)
static int error_count