From a440adbef9d723484f49341f778f806de7b87ef1 Mon Sep 17 00:00:00 2001 From: Sukru Senli Date: Tue, 17 Jun 2025 15:38:32 +0200 Subject: [PATCH] jsonval: 1.0.0 A small CLI tool to validate JSON files against a schema using json-schema-validator and nlohmann/json --- jsonval/Makefile | 44 ++++++++++++++++++++++++++++++ jsonval/src/main.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 jsonval/Makefile create mode 100644 jsonval/src/main.cpp diff --git a/jsonval/Makefile b/jsonval/Makefile new file mode 100644 index 000000000..c2a2001b3 --- /dev/null +++ b/jsonval/Makefile @@ -0,0 +1,44 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=jsonval +PKG_VERSION:=1.0.0 +PKG_RELEASE:=1 + +PKG_LICENSE:=MIT +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) + +include $(INCLUDE_DIR)/package.mk + +define Package/jsonval + SECTION:=utils + CATEGORY:=Utilities + TITLE:=Command-line JSON Schema Validator + DEPENDS:=+nlohmann-json +json-schema-validator +libstdcpp +endef + +define Package/jsonval/description + A small CLI tool to validate JSON files against a schema using json-schema-validator and nlohmann/json. +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + cp -r ./src/* $(PKG_BUILD_DIR)/ +endef + +define Build/Compile + $(TARGET_CXX) \ + $(TARGET_CXXFLAGS) -std=c++17 \ + -I$(STAGING_DIR)/usr/include \ + -I$(STAGING_DIR)/usr/include/nlohmann \ + -L$(STAGING_DIR)/usr/lib \ + $(PKG_BUILD_DIR)/main.cpp \ + -o $(PKG_BUILD_DIR)/jsonval \ + -lnlohmann_json_schema_validator +endef + +define Package/jsonval/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/jsonval $(1)/usr/bin/ +endef + +$(eval $(call BuildPackage,jsonval)) diff --git a/jsonval/src/main.cpp b/jsonval/src/main.cpp new file mode 100644 index 000000000..ed914935b --- /dev/null +++ b/jsonval/src/main.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +using nlohmann::json; +using nlohmann::json_schema::json_validator; +using namespace std; + +void print_usage(const string& prog_name) { + cerr << "Usage: " << prog_name << " -s -j " << endl; +} + +int main(int argc, char* argv[]) { + string schema_path, data_path; + + // Simple argument parsing + for (int i = 1; i < argc; ++i) { + string arg = argv[i]; + if ((arg == "-s" || arg == "--schema") && i + 1 < argc) { + schema_path = argv[++i]; + } else if ((arg == "-j" || arg == "--json") && i + 1 < argc) { + data_path = argv[++i]; + } else { + print_usage(argv[0]); + return 1; + } + } + + if (schema_path.empty() || data_path.empty()) { + print_usage(argv[0]); + return 1; + } + + ifstream schema_file(schema_path); + ifstream data_file(data_path); + + if (!schema_file.is_open() || !data_file.is_open()) { + cerr << "Error: Could not open one or both files." << endl; + return 2; + } + + json schema, document; + try { + schema_file >> schema; + data_file >> document; + } catch (const json::parse_error& e) { + cerr << "Parse error: " << e.what() << endl; + return 3; + } + + try { + json_validator validator; + validator.set_root_schema(schema); + validator.validate(document); + cout << "Valid" << endl; + } catch (const std::exception& e) { + cerr << "Validation failed: " << e.what() << endl; + return 4; + } + + return 0; +}