MQTTSuite
Loading...
Searching...
No Matches
readme.cpp
Go to the documentation of this file.
1#include <iomanip>
2#include <iostream>
3
4#include <nlohmann/json-schema.hpp>
5
6using nlohmann::json;
7using nlohmann::json_schema::json_validator;
8
9// The schema is defined based upon a string literal
10static json person_schema = R"(
11{
12 "$schema": "http://json-schema.org/draft-07/schema#",
13 "title": "A person",
14 "properties": {
15 "name": {
16 "description": "Name",
17 "type": "string"
18 },
19 "age": {
20 "description": "Age of the person",
21 "type": "number",
22 "minimum": 2,
23 "maximum": 200
24 },
25 "address":{
26 "type": "object",
27 "properties":{
28 "street":{
29 "type": "string",
30 "default": "Abbey Road"
31 }
32 }
33 }
34 },
35 "required": [
36 "name",
37 "age"
38 ],
39 "type": "object"
40}
41
42)"_json;
43
44// The people are defined with brace initialization
45static json bad_person = {{"age", 42}};
46static json good_person = {{"name", "Albert"}, {"age", 42}, {"address", {{"street", "Main Street"}}}};
47static json good_defaulted_person = {{"name", "Knut"}, {"age", 69}, {"address", {}}};
48
49int main()
50{
51 /* json-parse the schema */
52
53 json_validator validator; // create validator
54
55 try {
56 validator.set_root_schema(person_schema); // insert root-schema
57 } catch (const std::exception &e) {
58 std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
59 return EXIT_FAILURE;
60 }
61
62 /* json-parse the people - API of 1.0.0, default throwing error handler */
63
64 for (auto &person : {bad_person, good_person, good_defaulted_person}) {
65 std::cout << "About to validate this person:\n"
66 << std::setw(2) << person << std::endl;
67 try {
68 auto defaultPatch = validator.validate(person); // validate the document - uses the default throwing error-handler
69 std::cout << "Validation succeeded\n";
70 std::cout << "Patch with defaults: " << defaultPatch.dump(2) << std::endl;
71 } catch (const std::exception &e) {
72 std::cerr << "Validation failed, here is why: " << e.what() << "\n";
73 }
74 }
75
76 /* json-parse the people - with custom error handler */
77 class custom_error_handler : public nlohmann::json_schema::basic_error_handler
78 {
79 void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
80 {
82 std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
83 }
84 };
85
86 for (auto &person : {bad_person, good_person}) {
87 std::cout << "About to validate this person:\n"
88 << std::setw(2) << person << std::endl;
89
90 custom_error_handler err;
91 validator.validate(person, err); // validate the document
92
93 if (err)
94 std::cerr << "Validation failed\n";
95 else
96 std::cout << "Validation succeeded\n";
97 }
98
99 return EXIT_SUCCESS;
100}
void error(const json::json_pointer &, const json &, const std::string &) override
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
int main()
Definition format.cpp:34
static json person_schema
Definition readme.cpp:10
static json good_defaulted_person
Definition readme.cpp:47
static json bad_person
Definition readme.cpp:45
static json good_person
Definition readme.cpp:46