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

Go to the source code of this file.

Classes

class  json_validator

Functions

static void uri_format_checker (const std::string &format, const std::string &value)
int main ()

Variables

static json uri_schema
static json good_uri = {{"myUri", "http://hostname.com/"}}
static json bad_uri = {{"myUri", "http:/hostname.com/"}}

Function Documentation

◆ main()

int main ( void )

Definition at line 34 of file format.cpp.

35{
36 json_validator validator(nullptr, uri_format_checker); // create validator
37
38 try {
39 validator.set_root_schema(uri_schema); // insert root-schema
40 } catch (const std::exception &e) {
41 std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
42 return EXIT_FAILURE;
43 }
44
45 validator.validate(good_uri);
46
47 try {
48 validator.validate(bad_uri);
49 } catch (const std::exception &e) {
50 std::cerr << "Validation expectedly failed, here is why: " << e.what() << "\n";
51 }
52
53 return EXIT_SUCCESS;
54}
nlohmann::json_schema::json_validator validator
static json uri_schema
Definition format.cpp:9
static json bad_uri
Definition format.cpp:23
static json good_uri
Definition format.cpp:22
static void uri_format_checker(const std::string &format, const std::string &value)
Definition format.cpp:25

References bad_uri, good_uri, nlohmann::json_schema::json_validator::json_validator(), nlohmann::json_schema::json_validator::set_root_schema(), uri_format_checker(), uri_schema, and nlohmann::json_schema::json_validator::validate().

Here is the call graph for this function:

◆ uri_format_checker()

void uri_format_checker ( const std::string & format,
const std::string & value )
static

Definition at line 25 of file format.cpp.

26{
27 if (format == "uri") {
28 if (value.find("://") == std::string::npos)
29 throw std::invalid_argument("URI does not contain :// - invalid");
30 } else
31 throw std::logic_error("Don't know how to validate " + format);
32}

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ bad_uri

json bad_uri = {{"myUri", "http:/hostname.com/"}}
static

Definition at line 23 of file format.cpp.

23{{"myUri", "http:/hostname.com/"}};

Referenced by main().

◆ good_uri

json good_uri = {{"myUri", "http://hostname.com/"}}
static

Definition at line 22 of file format.cpp.

22{{"myUri", "http://hostname.com/"}};

Referenced by main().

◆ uri_schema

json uri_schema
static
Initial value:
= R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"myUri": {
"type":"string",
"format": "uri"
}
}
})"_json

Definition at line 9 of file format.cpp.

Referenced by main().