json-schema-validator/src/cli.cpp
Cristian Le ff35afd5a5
Add appropriate project version evaluation
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
2023-05-11 15:24:34 +02:00

68 lines
1.5 KiB
C++

#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <fstream>
#include "nlohmann/json-schema.hpp"
using namespace nlohmann;
using namespace nlohmann::json_schema;
class main_cli : public CLI::App
{
std::string version;
std::ifstream schema_input;
std::filesystem::path object_path;
// TODO: Export this as a built-in loader
void loader(const json_uri &uri, json &sch)
{
std::string filename = object_path.parent_path().append(uri.path());
std::ifstream lf(filename);
if (!lf.good())
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
try {
lf >> sch;
} catch (const std::exception &e) {
throw e;
}
}
public:
json schema;
json object;
json_validator validator;
main_cli()
: CLI::App{"Json schema validator", "json-validator"},
validator{
[this](const json_uri &u, json &s) { this->loader(u, s); },
default_string_format_check},
version{nlohmann::json_schema::version}
{
set_version_flag("--version", version);
add_option("schema", schema_input, "JSON schema of the object")
->check(CLI::ExistingFile);
add_option("object", object_path, "JSON object to validate")
->check(CLI::ExistingFile);
}
void validate()
{
validator.set_root_schema(schema);
validator.validate(object);
}
};
int main(int argc, char *argv[])
{
main_cli app{};
try {
app.parse(argc, argv);
} catch (const CLI::ParseError &e) {
return app.exit(e);
}
app.validate();
return 0;
}