This commit is contained in:
Cristian Le 2023-07-21 06:11:39 +02:00 committed by GitHub
commit f9817db99b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 3 deletions

View File

@ -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)

View File

@ -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
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)
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)

67
src/cli.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <fstream>
#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;
}

View File

@ -128,6 +128,8 @@ public:
namespace json_schema
{
constexpr std::string_view version = "@PROJECT_VERSION@";
extern json draft7_schema_builtin;
typedef std::function<void(const json_uri & /*id*/, json & /*value*/)> schema_loader;