Basic implementation
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
This commit is contained in:
parent
8f2e050c5c
commit
f543f18d13
51
src/cli.cpp
51
src/cli.cpp
@ -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:
|
||||||
|
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
|
// TODO: Move to a generated header file
|
||||||
app.set_version_flag("--version", "2.2.0");
|
set_version_flag("--version", "2.2.0");
|
||||||
app.add_option("schema", schema_input, "JSON schema of the object")
|
add_option("schema", schema_input, "JSON schema of the object")
|
||||||
->check(CLI::ExistingFile);
|
->check(CLI::ExistingFile);
|
||||||
app.add_option("object", "JSON object to validate")
|
add_option("object", object_input, "JSON object to validate")
|
||||||
->check(CLI::ExistingFile);
|
->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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user