Basic implementation
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
This commit is contained in:
parent
8f2e050c5c
commit
f543f18d13
57
src/cli.cpp
57
src/cli.cpp
@ -8,18 +8,49 @@
|
||||
using namespace nlohmann;
|
||||
using namespace nlohmann::json_schema;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
class main_cli : public CLI::App
|
||||
{
|
||||
std::ifstream schema_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"};
|
||||
// TODO: Move to a generated header file
|
||||
app.set_version_flag("--version", "2.2.0");
|
||||
app.add_option("schema", schema_input, "JSON schema of the object")
|
||||
->check(CLI::ExistingFile);
|
||||
app.add_option("object", "JSON object to validate")
|
||||
->check(CLI::ExistingFile);
|
||||
public:
|
||||
json schema;
|
||||
json object;
|
||||
json_validator validator;
|
||||
main_cli()
|
||||
: CLI::App{"Json schema validator", "json-validator"},
|
||||
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 {
|
||||
app.parse(argc, argv);
|
||||
@ -27,13 +58,7 @@ int main(int argc, char *argv[])
|
||||
return app.exit(e);
|
||||
}
|
||||
|
||||
json schema{};
|
||||
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;
|
||||
app.validate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user