MQTTSuite
Loading...
Searching...
No Matches
binary-validation.cpp
Go to the documentation of this file.
1// bson-validate.cpp
2
3#include <iostream>
4#include <nlohmann/json-schema.hpp>
5#include <nlohmann/json.hpp>
6
7static int error_count = 0;
8
9#define EXPECT_EQ(a, b)
10 do {
11 if (a != b) {
12 std::cerr << "Failed: '" << a << "' != '" << b << "'\n";
13 error_count++;
14 }
15 } while (0)
16
17#define EXPECT_THROW(foo)
18 {
19 bool ok = false;
20 try {
21 foo;
22 } catch (std::exception &) {
23 ok = true;
24 }
25 if (ok == false) {
26 error_count++;
27 }
28 }
29
30using json = nlohmann::json;
32
33// check binary data validation
34const json bson_schema = json::parse(R"(
35{
36 "type": "object",
37 "properties": {
38 "standard_string": {
39 "type": "string"
40 },
41 "binary_data": {
42 "type": "string",
43 "contentEncoding": "binary"
44 }
45 },
46 "additionalProperties": false
47}
48)");
49
50const json array_of_types = json::parse(R"(
51{
52 "type": "object",
53 "properties": {
54 "something": {
55 "type": ["string", "number", "boolean"],
56 "contentEncoding": "binary"
57 }
58 }
59}
60)");
61
63{
64 "type": "object",
65 "properties": {
66 "something": {
67 "type": ["string", "number", "boolean"]
68 }
69 }
70}
71)");
72
74{
75 void error(const nlohmann::json::json_pointer &ptr, const json &, const std::string &message) override
76 {
78 std::cerr << "ERROR: '" << ptr << "' - '"
79 << ""
80 << "': " << message << "\n";
81 failed_pointers.push_back(ptr);
82 }
83
84public:
85 std::vector<nlohmann::json::json_pointer> failed_pointers;
86
92};
93
94static void content(const std::string &contentEncoding, const std::string &contentMediaType, const json &instance)
95{
96 std::cerr << "mediaType: '" << contentMediaType << "', encoding: '" << contentEncoding << "'\n";
97
98 if (contentEncoding == "binary") {
99 if (instance.type() != json::value_t::binary) {
100 throw std::invalid_argument{"expected binary data"};
101 }
102 } else {
103 if (instance.type() == json::value_t::binary) {
104 throw std::invalid_argument{"expected string, but get binary"};
105 }
106 }
107}
108
109int main()
110{
111 validator val(nullptr, nullptr, content);
112
113 // create some bson doc
114 json::binary_t arr;
115 std::string as_binary = "hello world";
116 std::copy(as_binary.begin(), as_binary.end(), std::back_inserter(arr));
117
118 json binary = json::binary(arr);
119
121
122 /////////////////////////////////////
124
125 // all right
126 val.validate({{"standard_string", "some string"}, {"binary_data", binary}}, err);
127 EXPECT_EQ(err.failed_pointers.size(), 0);
128 err.reset();
129
130 // invalid binary data
131 val.validate({{"binary_data", "string, but expect binary data"}}, err);
132 EXPECT_EQ(err.failed_pointers.size(), 1);
133 EXPECT_EQ(err.failed_pointers[0].to_string(), "/binary_data");
134 err.reset();
135
136 // also check that simple string not accept binary data
137 val.validate({{"standard_string", binary}, {"binary_data", binary}}, err);
138 EXPECT_EQ(err.failed_pointers.size(), 1);
139 EXPECT_EQ(err.failed_pointers[0].to_string(), "/standard_string");
140 err.reset();
141
142 /////////////////////////////////////
143 // check with array of types
144
145 // check simple types
147 val.validate({{"something", 1}}, err);
148 val.validate({{"something", false}}, err);
149 // TODO when we set `string` in array and set `contentEncoding` = "binary" - what it means? We expected string or binary?
150 // Or we expect only binary? Now if you set `contentEncoding` = "binary", then it means that you expect only binary data,
151 // not string
152 // val.validate({{"something", "string"}}, err); -> produce error about type
153 EXPECT_EQ(err.failed_pointers.size(), 0);
154 err.reset();
155
156 // check binary data
157 val.validate({{"something", binary}}, err);
158 EXPECT_EQ(err.failed_pointers.size(), 0);
159 err.reset();
160
161 /////////////////////////////////////
162 // and check that you can't set binary data if contentEncoding don't set
164 val.validate({{"something", binary}}, err);
165 EXPECT_EQ(err.failed_pointers.size(), 1);
166 EXPECT_EQ(err.failed_pointers[0].to_string(), "/something");
167 err.reset();
168
169 // check that without content callback you get exception with schema with contentEncoding or contentMeditType
170 validator val_no_content;
171
173
174 return error_count;
175}
const json array_of_types
const json array_of_types_without_binary
#define EXPECT_EQ(a, b)
nlohmann::json_schema::json_validator validator
#define EXPECT_THROW(foo)
static void content(const std::string &contentEncoding, const std::string &contentMediaType, const json &instance)
static int error_count
nlohmann::json json
const json bson_schema
void error(const json::json_pointer &, const json &, const std::string &) override
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
void error(const nlohmann::json::json_pointer &ptr, const json &, const std::string &message) override
std::vector< nlohmann::json::json_pointer > failed_pointers
int main()
Definition format.cpp:34