MQTTSuite
Loading...
Searching...
No Matches
issue-149-entry-selection.cpp
Go to the documentation of this file.
1#include <nlohmann/json-schema.hpp>
2
3#include <iostream>
4
5using nlohmann::json;
6using nlohmann::json_uri;
7using nlohmann::json_schema::json_validator;
8
9namespace
10{
11
12static int error_count;
13#define EXPECT_EQ(a, b)
14 do {
15 if (a != b) {
16 std::cerr << "Failed: '" << a << "' != '" << b << "'\n";
17 error_count++;
18 }
19 } while (0)
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 "type": "integer",
26 "definitions": {
27 "A": {
28 "type": "object",
29 "properties": {
30 "b": {
31 "$ref": "#/definitions/B"
32 }
33 }
34 },
35 "B": {
36 "type": "integer"
37 }
38 }
39})"_json;
40
42{
43 void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
44 {
46 std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
47 failed.push_back(ptr);
48 }
49
50public:
51 std::vector<nlohmann::json::json_pointer> failed;
52
53 void reset() override
54 {
56 failed.clear();
57 }
58};
59
60} // namespace
61
63
64int main(void)
65{
67
68 validator.validate(1, err); // OK
69 EXPECT_EQ(err.failed.size(), 0);
70 err.reset();
71
72 validator.validate("1", err); // no name
73 EXPECT_EQ(err.failed.size(), 1);
74 err.reset();
75
76 validator.validate(1, err, json_uri("#/definitions/B"));
77 EXPECT_EQ(err.failed.size(), 0);
78 err.reset();
79
80 validator.validate("1", err, json_uri("#/definitions/B"));
81 EXPECT_EQ(err.failed.size(), 1);
82 err.reset();
83
84 validator.validate({{"b", 1}}, err, json_uri("#/definitions/A"));
85 EXPECT_EQ(err.failed.size(), 0);
86 err.reset();
87
88 validator.validate({{"b", "1"}}, err, json_uri("#/definitions/A"));
89 EXPECT_EQ(err.failed.size(), 1);
90 err.reset();
91
92 return error_count;
93}
#define EXPECT_EQ(a, b)
std::vector< nlohmann::json::json_pointer > failed
void reset() override
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
json_uri(const std::string &uri)
int main()
Definition format.cpp:34
static json_validator validator(person_schema)
static json person_schema
static int error_count