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

Go to the source code of this file.

Classes

class  custom_error_handler
class  json_uri

Functions

static void usage (const char *name)
static void loader (const json_uri &uri, json &schema)
int main (int argc, char *argv[])

Function Documentation

◆ loader()

void loader ( const json_uri & uri,
json & schema )
static

Definition at line 24 of file json-schema-validate.cpp.

25{
26 std::string filename = "./" + uri.path();
27 std::ifstream lf(filename);
28 if (!lf.good())
29 throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
30 try {
31 lf >> schema;
32 } catch (const std::exception &e) {
33 throw e;
34 }
35}
std::string url() const
const std::string & path() const
auto schema
Definition id-ref.cpp:69

References nlohmann::json_uri::path(), and nlohmann::json_uri::url().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 46 of file json-schema-validate.cpp.

47{
48 if (argc != 2)
49 usage(argv[0]);
50
51 std::ifstream f(argv[1]);
52 if (!f.good()) {
53 std::cerr << "could not open " << argv[1] << " for reading\n";
54 usage(argv[0]);
55 }
56
57 // 1) Read the schema for the document you want to validate
59 try {
60 f >> schema;
61 } catch (const std::exception &e) {
62 std::cerr << e.what() << " at " << f.tellg() << " - while parsing the schema\n";
63 return EXIT_FAILURE;
64 }
65
66 // 2) create the validator and
69
70 try {
71 // insert this schema as the root to the validator
72 // this resolves remote-schemas, sub-schemas and references via the given loader-function
73 validator.set_root_schema(schema);
74 } catch (const std::exception &e) {
75 std::cerr << "setting root schema failed\n";
76 std::cerr << e.what() << "\n";
77 }
78
79 // 3) do the actual validation of the document
80 json document;
81
82 try {
83 std::cin >> document;
84 } catch (const std::exception &e) {
85 std::cerr << "json parsing failed: " << e.what() << " at offset: " << std::cin.tellg() << "\n";
86 return EXIT_FAILURE;
87 }
88
90 validator.validate(document, err);
91
92 if (err) {
93 std::cerr << "schema validation failed\n";
94 return EXIT_FAILURE;
95 }
96
97 std::cerr << "document is valid\n";
98
99 return EXIT_SUCCESS;
100}
nlohmann::json_schema::json_validator validator
nlohmann::json json
static void loader(const json_uri &uri, json &schema)
static void usage(const char *name)
void default_string_format_check(const std::string &format, const std::string &value)

References nlohmann::json_schema::default_string_format_check(), nlohmann::json_schema::json_validator::json_validator(), loader(), nlohmann::json_schema::json_validator::set_root_schema(), usage(), and nlohmann::json_schema::json_validator::validate().

Here is the call graph for this function:

◆ usage()

void usage ( const char * name)
static

Definition at line 18 of file json-schema-validate.cpp.

19{
20 std::cerr << "Usage: " << name << " <schema> < <document>\n";
21 exit(EXIT_FAILURE);
22}

Referenced by main().

Here is the caller graph for this function: