Basic implementation

Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
This commit is contained in:
Cristian Le 2023-05-10 18:08:58 +02:00
parent 8f2e050c5c
commit f543f18d13
Failed to extract signature

View File

@ -8,18 +8,49 @@
using namespace nlohmann; using namespace nlohmann;
using namespace nlohmann::json_schema; using namespace nlohmann::json_schema;
int main(int argc, char *argv[]) class main_cli : public CLI::App
{ {
std::ifstream schema_input; std::ifstream schema_input;
std::ifstream object_input; std::ifstream object_input;
// TODO: Export this as a built-in loader
static void loader(const json_uri &uri, json &schema)
{
std::string filename = "./" + uri.path();
std::ifstream lf(filename);
if (!lf.good())
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
try {
lf >> schema;
} catch (const std::exception &e) {
throw e;
}
}
CLI::App app{"Json schema validator", "json-validator"}; public:
// TODO: Move to a generated header file json schema;
app.set_version_flag("--version", "2.2.0"); json object;
app.add_option("schema", schema_input, "JSON schema of the object") json_validator validator;
->check(CLI::ExistingFile); main_cli()
app.add_option("object", "JSON object to validate") : CLI::App{"Json schema validator", "json-validator"},
->check(CLI::ExistingFile); validator{loader, default_string_format_check}
{
// TODO: Move to a generated header file
set_version_flag("--version", "2.2.0");
add_option("schema", schema_input, "JSON schema of the object")
->check(CLI::ExistingFile);
add_option("object", object_input, "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 { try {
app.parse(argc, argv); app.parse(argc, argv);
@ -27,13 +58,7 @@ int main(int argc, char *argv[])
return app.exit(e); return app.exit(e);
} }
json schema{}; app.validate();
json object{};
if (!schema_input.good())
throw std::invalid_argument("could not read schema");
if (!object_input.good())
throw std::invalid_argument("could not read object");
schema_input >> schema;
object_input >> object;
return 0; return 0;
} }