Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions .github/workflows/verify-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,31 @@ jobs:
yamllint_strict: false
yamllint_comment: false

- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version: "3.x"

- name: "Install dependencies"
run: sudo apt-get update && sudo apt-get install -y g++-14 libyaml-cpp-dev nlohmann-json3-dev

- name: "Fetch json-schema-validator"
run: |
rm -rf json-schema-validator
git clone --depth 1 https://github.com/pboettch/json-schema-validator.git json-schema-validator

- name: "Build YAML validator"
run: |
python3 -m pip install --upgrade pip
python3 -m pip install PyYAML jsonschema
g++-14 -std=c++23 -Wall -Wextra \
$(pkg-config --cflags yaml-cpp) \
$(pkg-config --cflags nlohmann_json) \
-Ijson-schema-validator/src \
-o validate_yaml_file \
validate_yaml_file.cpp \
json-schema-validator/src/json-uri.cpp \
json-schema-validator/src/json-validator.cpp \
json-schema-validator/src/json-schema-draft7.json.cpp \
json-schema-validator/src/json-patch.cpp \
$(pkg-config --libs yaml-cpp)

- name: "Verify features"
- name: "Validate feature files"
run: |
for file in features_c*.yaml; do
python3 verify_features.py "$file"
echo "Verifying: $file"
./validate_yaml_file "$file"
done
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
validate_yaml_file
81 changes: 81 additions & 0 deletions validate_yaml_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Cem Dervis
// SPDX-License-Identifier: CC0-1.0
// This file is part of https://cppstat.dev.

#include <nlohmann/json-schema.hpp>
#include <nlohmann/json.hpp>
#include <print>
#include <yaml-cpp/yaml.h>

using json = nlohmann::json;

json yaml_to_json(const YAML::Node& node) {
if (node.IsNull()) {
return nullptr;
}
else if (node.IsScalar()) {
try {
return node.as<bool>();
} catch (...) {
}

try {
return node.as<int64_t>();
} catch (...) {
}

try {
return node.as<double>();
} catch (...) {
}

return node.as<std::string>();
}
else if (node.IsSequence()) {
auto arr = json::array();

for (const auto& item : node) {
arr.push_back(yaml_to_json(item));
}

return arr;
}
else if (node.IsMap()) {
auto obj = json::object();

for (const auto& kv : node) {
obj[kv.first.as<std::string>()] = yaml_to_json(kv.second);
}

return obj;
}

return nullptr;
}

int main(int argc, char* argv[]) {
if (argc < 2) {
std::println(stderr, "Usage: {} <data_file.yaml>", argv[0]);
return EXIT_FAILURE;
}

try {
auto schema_yaml = YAML::LoadFile("features.schema.yaml");
auto schema = yaml_to_json(schema_yaml);

auto data_yaml = YAML::LoadFile(argv[1]);
auto data = yaml_to_json(data_yaml);

auto val = nlohmann::json_schema::json_validator();
val.set_root_schema(schema);
val.validate(data);

std::println("Verification successful: {}", argv[1]);

return EXIT_SUCCESS;
} catch (const std::exception& e) {
std::println(stderr, "Verification failed: {}", argv[1]);
std::println(stderr, "Error: {}", e.what());
return EXIT_FAILURE;
}
}
35 changes: 0 additions & 35 deletions verify_features.py

This file was deleted.