MQTTSuite
Loading...
Searching...
No Matches
binary-validation.cpp File Reference
#include <iostream>
#include <nlohmann/json-schema.hpp>
#include <nlohmann/json.hpp>
Include dependency graph for binary-validation.cpp:

Go to the source code of this file.

Classes

class  store_ptr_err_handler

Macros

#define EXPECT_EQ(a, b)
#define EXPECT_THROW(foo)

Typedefs

using json = nlohmann::json
using validator = nlohmann::json_schema::json_validator

Functions

static void content (const std::string &contentEncoding, const std::string &contentMediaType, const json &instance)
int main ()

Variables

static int error_count = 0
const json bson_schema
const json array_of_types
const json array_of_types_without_binary

Macro Definition Documentation

◆ EXPECT_EQ

#define EXPECT_EQ ( a,
b )
Value:
do { \
if (a != b) { \
std::cerr << "Failed: '" << a << "' != '" << b << "'\n"; \
} \
} while (0)
static int error_count

Definition at line 9 of file binary-validation.cpp.

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)

◆ EXPECT_THROW

#define EXPECT_THROW ( foo)
Value:
{ \
bool ok = false; \
try { \
foo; \
} catch (std::exception &) { \
ok = true; \
} \
if (ok == false) { \
} \
}

Definition at line 17 of file binary-validation.cpp.

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 }

Typedef Documentation

◆ json

using json = nlohmann::json

Definition at line 30 of file binary-validation.cpp.

◆ validator

Function Documentation

◆ content()

void content ( const std::string & contentEncoding,
const std::string & contentMediaType,
const json & instance )
static

Definition at line 94 of file binary-validation.cpp.

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}
static const auto instance
Definition issue-93.cpp:14

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 109 of file binary-validation.cpp.

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
123 val.set_root_schema(bson_schema);
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
143 // check with array of types
144
145 // check simple types
146 val.set_root_schema(array_of_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
162 // and check that you can't set binary data if contentEncoding don't set
163 val.set_root_schema(array_of_types_without_binary);
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)
nlohmann::json json
const json bson_schema
std::vector< nlohmann::json::json_pointer > failed_pointers

References array_of_types, array_of_types_without_binary, bson_schema, content(), error_count, store_ptr_err_handler::failed_pointers, nlohmann::json_schema::json_validator::json_validator(), store_ptr_err_handler::reset(), nlohmann::json_schema::json_validator::set_root_schema(), and nlohmann::json_schema::json_validator::validate().

Here is the call graph for this function:

Variable Documentation

◆ array_of_types

const json array_of_types
Initial value:
= json::parse(R"(
{
"type": "object",
"properties": {
"something": {
"type": ["string", "number", "boolean"],
"contentEncoding": "binary"
}
}
}
)")

Definition at line 50 of file binary-validation.cpp.

Referenced by main().

◆ array_of_types_without_binary

const json array_of_types_without_binary
Initial value:
= json::parse(R"(
{
"type": "object",
"properties": {
"something": {
"type": ["string", "number", "boolean"]
}
}
}
)")

Definition at line 62 of file binary-validation.cpp.

Referenced by main().

◆ bson_schema

const json bson_schema
Initial value:
= json::parse(R"(
{
"type": "object",
"properties": {
"standard_string": {
"type": "string"
},
"binary_data": {
"type": "string",
"contentEncoding": "binary"
}
},
"additionalProperties": false
}
)")

Definition at line 34 of file binary-validation.cpp.

Referenced by main().

◆ error_count

int error_count = 0
static

Definition at line 7 of file binary-validation.cpp.

Referenced by main().