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
5
using
nlohmann
::json;
6
using
nlohmann
::json_uri;
7
using
nlohmann
::
json_schema
::json_validator;
8
9
namespace
10
{
11
12
static
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
22
static
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
41
class
store_err_handler
:
public
nlohmann
::
json_schema
::
basic_error_handler
42
{
43
void
error
(
const
nlohmann
::json::json_pointer &ptr,
const
json &instance,
const
std::string &message)
override
44
{
45
nlohmann
::
json_schema
::
basic_error_handler
::
error
(
ptr
,
instance
,
message
)
;
46
std::cerr <<
"ERROR: '"
<< ptr <<
"' - '"
<< instance <<
"': "
<< message <<
"\n"
;
47
failed
.push_back(ptr);
48
}
49
50
public
:
51
std::vector<
nlohmann
::json::json_pointer>
failed
;
52
53
void
reset
()
override
54
{
55
nlohmann
::
json_schema
::
basic_error_handler
::
reset
(
)
;
56
failed
.clear();
57
}
58
};
59
60
}
// namespace
61
62
static
json_validator
validator
(
person_schema
)
;
63
64
int
main
(
void
)
65
{
66
store_err_handler
err;
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
}
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition
binary-validation.cpp:9
anonymous_namespace{issue-149-entry-selection.cpp}::store_err_handler
Definition
issue-149-entry-selection.cpp:42
anonymous_namespace{issue-149-entry-selection.cpp}::store_err_handler::failed
std::vector< nlohmann::json::json_pointer > failed
Definition
issue-149-entry-selection.cpp:51
anonymous_namespace{issue-149-entry-selection.cpp}::store_err_handler::reset
void reset() override
Definition
issue-149-entry-selection.cpp:53
anonymous_namespace{issue-149-entry-selection.cpp}::store_err_handler::error
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
Definition
issue-149-entry-selection.cpp:43
nlohmann::json_schema::basic_error_handler
Definition
json-schema.hpp:146
nlohmann::json_schema::basic_error_handler::error
void error(const json::json_pointer &, const json &, const std::string &) override
Definition
json-schema.hpp:150
nlohmann::json_schema::basic_error_handler::reset
virtual void reset()
Definition
json-schema.hpp:155
nlohmann::json_schema::json_validator
Definition
json-schema.hpp:167
nlohmann::json_schema::json_validator::json_validator
json_validator(const json &, schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
Definition
json-validator.cpp:1466
nlohmann::json_schema::json_validator::validate
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
Definition
json-validator.cpp:1511
nlohmann::json_uri
Definition
json-schema.hpp:48
nlohmann::json_uri::json_uri
json_uri(const std::string &uri)
Definition
json-schema.hpp:68
main
int main()
Definition
format.cpp:34
validator
static json_validator validator(person_schema)
anonymous_namespace{issue-149-entry-selection.cpp}::person_schema
static json person_schema
Definition
issue-149-entry-selection.cpp:22
anonymous_namespace{issue-149-entry-selection.cpp}::error_count
static int error_count
Definition
issue-149-entry-selection.cpp:12
nlohmann::json_schema
Definition
json-schema-draft7.json.cpp:14
nlohmann
Definition
json-patch.cpp:72
lib
json-schema-validator
test
issue-149-entry-selection.cpp
Generated on
for MQTTSuite by
1.15.0