diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b81049..00caeb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,12 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) GIT_TAG ${JSON_FETCH_VERSION} FIND_PACKAGE_ARGS ) - list(APPEND fetch_packages nlohmann_json) + FetchContent_Declare(CLI11 + GIT_REPOSITORY https://github.com/CLIUtils/CLI11 + GIT_TAG v2.3.2 + FIND_PACKAGE_ARGS + ) + list(APPEND fetch_packages nlohmann_json CLI11) else () # Try to get system installed version find_package(nlohmann_json QUIET) @@ -100,6 +105,15 @@ else () ) list(APPEND fetch_packages nlohmann_json) endif () + find_package(CLI11 QUIET) + if (NOT CLI11_FOUND) + # If failed fetch the desired version + FetchContent_Declare(CLI11 + GIT_REPOSITORY https://github.com/CLIUtils/CLI11 + GIT_TAG v2.3.2 + ) + list(APPEND fetch_packages nlohmann_json CLI11) + endif () endif () # Handle configure flags @@ -141,6 +155,12 @@ set_target_properties(nlohmann_json_schema_validator PROPERTIES # OUTPUT_NAME nlohmann_json_validator ) +add_executable(nlohmann_json_schema_validator_cli) +set_target_properties(nlohmann_json_schema_validator_cli PROPERTIES + EXPORT_NAME cli + OUTPUT_NAME json-validator + ) + # Main definitions in here add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f895d66..8387d2d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,19 @@ target_sources(nlohmann_json_schema_validator PRIVATE json-patch.cpp string-format-check.cpp ) +target_sources(nlohmann_json_schema_validator_cli PRIVATE + cli.cpp) +configure_file(nlohmann/json-schema.hpp.in nlohmann/json-schema.hpp) + target_include_directories(nlohmann_json_schema_validator PUBLIC $ $ + $ ) - set_target_properties(nlohmann_json_schema_validator PROPERTIES - PUBLIC_HEADER nlohmann/json-schema.hpp) + PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/nlohmann/json-schema.hpp) +target_link_libraries(nlohmann_json_schema_validator_cli PRIVATE + nlohmann_json_schema_validator CLI11::CLI11) # TODO: Why would this need to be if guarded? if (JSON_VALIDATOR_SHARED_LIBS) diff --git a/src/cli.cpp b/src/cli.cpp new file mode 100644 index 0000000..ac08681 --- /dev/null +++ b/src/cli.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#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; +} diff --git a/src/nlohmann/json-schema.hpp b/src/nlohmann/json-schema.hpp.in similarity index 98% rename from src/nlohmann/json-schema.hpp rename to src/nlohmann/json-schema.hpp.in index 07befd3..d1695d6 100644 --- a/src/nlohmann/json-schema.hpp +++ b/src/nlohmann/json-schema.hpp.in @@ -128,6 +128,8 @@ public: namespace json_schema { +constexpr std::string_view version = "@PROJECT_VERSION@"; + extern json draft7_schema_builtin; typedef std::function schema_loader;