MQTTSuite
Loading...
Searching...
No Matches
json-schema-validate.cpp
Go to the documentation of this file.
1/*
2 * JSON schema validator for JSON for modern C++
3 *
4 * Copyright (c) 2016-2019 Patrick Boettcher <p@yai.se>.
5 *
6 * SPDX-License-Identifier: MIT
7 *
8 */
9#include <nlohmann/json-schema.hpp>
10
11#include <fstream>
12#include <iostream>
13
14using nlohmann::json;
15using nlohmann::json_uri;
16using nlohmann::json_schema::json_validator;
17
18static void usage(const char *name)
19{
20 std::cerr << "Usage: " << name << " <schema> < <document>\n";
21 exit(EXIT_FAILURE);
22}
23
24static void loader(const json_uri &uri, json &schema)
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}
36
38{
39 void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
40 {
42 std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
43 }
44};
45
46int main(int argc, char *argv[])
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
58 json schema;
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}
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
void error(const json::json_pointer &, const json &, const std::string &) override
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
std::string url() const
const std::string & path() const
int main(int argc, char *argv[])
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)