json-schema-validator/app/json-schema-validate.cpp
Patrick Boettcher 42a8b00d1d validator: $ref implemented for local references
But needs more work.
2016-12-24 00:10:48 +01:00

60 lines
1.1 KiB
C++

#include "json-schema-validator.hpp"
#include <fstream>
#include <cstdlib>
using nlohmann::json;
using nlohmann::json_validator;
static void usage(const char *name)
{
std::cerr << "Usage: " << name << " <json-document> < <schema>\n";
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
if (argc != 2)
usage(argv[0]);
std::fstream f(argv[1]);
if (!f.good()) {
std::cerr << "could not open " << argv[1] << " for reading\n";
usage(argv[0]);
}
json schema;
try {
f >> schema;
} catch (std::exception &e) {
std::cerr << e.what() << " at " << f.tellp() << "\n";
return EXIT_FAILURE;
}
json document;
try {
std::cin >> document;
} catch (std::exception &e) {
std::cerr << e.what() << " at " << f.tellp() << "\n";
return EXIT_FAILURE;
}
try {
json_validator validator;
validator.set_schema("#", schema);
validator.validate(document);
} catch (std::exception &e) {
std::cerr << "schema validation failed\n";
std::cerr << e.what() << "\n";
return EXIT_FAILURE;
}
std::cerr << std::setw(2) << document << "\n";
return EXIT_SUCCESS;
}