mirror of
https://dev.iopsys.eu/feed/iopsys.git
synced 2025-12-09 23:34:51 +01:00
jsonval: 1.0.0
A small CLI tool to validate JSON files against a schema using json-schema-validator and nlohmann/json
This commit is contained in:
parent
2ba04321b3
commit
a440adbef9
2 changed files with 108 additions and 0 deletions
44
jsonval/Makefile
Normal file
44
jsonval/Makefile
Normal file
|
|
@ -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))
|
||||||
64
jsonval/src/main.cpp
Normal file
64
jsonval/src/main.cpp
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <nlohmann/json-schema.hpp>
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
using nlohmann::json_schema::json_validator;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void print_usage(const string& prog_name) {
|
||||||
|
cerr << "Usage: " << prog_name << " -s <schema.json> -j <data.json>" << 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;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue