feat(schemas): versioned JSON contracts for metadata, describe and explain (G6)
VoidBuildSystem CI / 1 · Requirements (push) Successful in 3s
VoidBuildSystem CI / 2B · Windows Wine build (debug) (push) Successful in 1m48s
VoidBuildSystem CI / 2A · Linux build (debug) (push) Successful in 4m22s
VoidBuildSystem CI / 2A · Linux build (coverage) (push) Successful in 4m46s
VoidBuildSystem CI / 2A · Linux build (sanitizers) (push) Successful in 6m5s
VoidBuildSystem CI / 2B · Windows Wine build (relwithdebinfo) (push) Successful in 3m24s
VoidBuildSystem CI / 2A · Linux build (relwithdebinfo) (push) Successful in 5m57s
VoidBuildSystem CI / 3B · Windows Wine test (debug) (push) Successful in 24s
VoidBuildSystem CI / 3B · Windows Wine test (relwithdebinfo) (push) Successful in 20s
VoidBuildSystem CI / 3A · Linux test (coverage) (push) Failing after 1m39s
VoidBuildSystem CI / 3A · Linux test (sanitizers) (push) Successful in 1m10s
VoidBuildSystem CI / 3A · Linux test (debug) (push) Failing after 7m21s
VoidBuildSystem CI / 3A · Linux test (relwithdebinfo) (push) Failing after 7m25s
VoidBuildSystem CI / 4A2 · VBS installers (linux target) (push) Skipped
VoidBuildSystem CI / 4A2 · VBS installers (windows target) (push) Skipped
VoidBuildSystem CI / 5 · Release (push) Skipped
VoidBuildSystem CI / 1 · Requirements (push) Successful in 3s
VoidBuildSystem CI / 2B · Windows Wine build (debug) (push) Successful in 1m48s
VoidBuildSystem CI / 2A · Linux build (debug) (push) Successful in 4m22s
VoidBuildSystem CI / 2A · Linux build (coverage) (push) Successful in 4m46s
VoidBuildSystem CI / 2A · Linux build (sanitizers) (push) Successful in 6m5s
VoidBuildSystem CI / 2B · Windows Wine build (relwithdebinfo) (push) Successful in 3m24s
VoidBuildSystem CI / 2A · Linux build (relwithdebinfo) (push) Successful in 5m57s
VoidBuildSystem CI / 3B · Windows Wine test (debug) (push) Successful in 24s
VoidBuildSystem CI / 3B · Windows Wine test (relwithdebinfo) (push) Successful in 20s
VoidBuildSystem CI / 3A · Linux test (coverage) (push) Failing after 1m39s
VoidBuildSystem CI / 3A · Linux test (sanitizers) (push) Successful in 1m10s
VoidBuildSystem CI / 3A · Linux test (debug) (push) Failing after 7m21s
VoidBuildSystem CI / 3A · Linux test (relwithdebinfo) (push) Failing after 7m25s
VoidBuildSystem CI / 4A2 · VBS installers (linux target) (push) Skipped
VoidBuildSystem CI / 4A2 · VBS installers (windows target) (push) Skipped
VoidBuildSystem CI / 5 · Release (push) Skipped
- schemas/ publishes JSON Schemas (draft 2020-12, additionalProperties false) for `vbs metadata`, `vbs describe` and `vbs explain --format json`, plus README documenting the versioning rule: any emitted-shape change bumps schema_version and adds a new -vN file; older schemas stay. - `vbs describe` now emits a StrictJson graph summary with schema_version, project/build roots, profile/compiler/os/arch, target and dependency counts and names. - `vbs explain --format json` carries schema_version. - Tests include a schema subset validator (types, required, enum, additionalProperties, items, minimum) and json_schemas_validate_metadata_describe_and_explain validates the real documents against the files, then proves the validator rejects a missing required key, a wrong enum value and an unexpected property. Verified: the schema test, vbs_core_tests and the CLI contract suite pass; clang-format clean.
This commit is contained in:
1 parent
455a403429
commit
f1cf277847
9 files changed
+648
-4
No files matched your search
@@ -121,6 +121,195 @@ std::filesystem::path locate_vbs_binary() {
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace {
|
||||
|
||||
using JsonValue = Void::Json::Strict::Value;
|
||||
|
||||
const JsonValue::Object *as_json_object(const JsonValue &value) {
|
||||
return std::get_if<JsonValue::Object>(&value.value);
|
||||
}
|
||||
|
||||
const JsonValue::Array *as_json_array(const JsonValue &value) {
|
||||
return std::get_if<JsonValue::Array>(&value.value);
|
||||
}
|
||||
|
||||
bool json_type_matches(const JsonValue &value, std::string_view type) {
|
||||
if (type == "object") {
|
||||
return std::holds_alternative<JsonValue::Object>(value.value);
|
||||
}
|
||||
if (type == "array") {
|
||||
return std::holds_alternative<JsonValue::Array>(value.value);
|
||||
}
|
||||
if (type == "string") {
|
||||
return std::holds_alternative<std::string>(value.value);
|
||||
}
|
||||
if (type == "boolean") {
|
||||
return std::holds_alternative<bool>(value.value);
|
||||
}
|
||||
if (type == "null") {
|
||||
return std::holds_alternative<std::nullptr_t>(value.value);
|
||||
}
|
||||
if (type == "integer") {
|
||||
return std::holds_alternative<std::int64_t>(value.value);
|
||||
}
|
||||
if (type == "number") {
|
||||
return std::holds_alternative<std::int64_t>(value.value) ||
|
||||
std::holds_alternative<double>(value.value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool validate_against_schema(const JsonValue &value, const JsonValue &schema,
|
||||
const std::string &path, std::string &error);
|
||||
|
||||
bool validate_object_against_schema(const JsonValue::Object &object, const JsonValue::Object &schema,
|
||||
const std::string &path, std::string &error) {
|
||||
if (const auto required = schema.find("required"); required != schema.end()) {
|
||||
if (const auto *keys = as_json_array(required->second); keys != nullptr) {
|
||||
for (const auto &key_value : *keys) {
|
||||
const auto *key = std::get_if<std::string>(&key_value.value);
|
||||
if (key != nullptr && !object.contains(*key)) {
|
||||
error = path + ": missing required property '" + *key + "'";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const auto properties_it = schema.find("properties");
|
||||
const auto *properties = properties_it == schema.end()
|
||||
? nullptr
|
||||
: as_json_object(properties_it->second);
|
||||
for (const auto &[key, member] : object) {
|
||||
if (properties != nullptr) {
|
||||
if (const auto property = properties->find(key); property != properties->end()) {
|
||||
if (!validate_against_schema(member, property->second, path + "." + key, error)) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (const auto additional = schema.find("additionalProperties");
|
||||
additional != schema.end()) {
|
||||
if (const auto *allowed = std::get_if<bool>(&additional->second.value);
|
||||
allowed != nullptr && !*allowed) {
|
||||
error = path + ": unexpected property '" + key + "'";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool validate_array_against_schema(const JsonValue::Array &array, const JsonValue::Object &schema,
|
||||
const std::string &path, std::string &error) {
|
||||
if (const auto items = schema.find("items"); items != schema.end()) {
|
||||
for (std::size_t i = 0; i < array.size(); ++i) {
|
||||
if (!validate_against_schema(array[i], items->second,
|
||||
path + "[" + std::to_string(i) + "]", error)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (const auto minimum = schema.find("minItems"); minimum != schema.end()) {
|
||||
if (const auto *limit = std::get_if<std::int64_t>(&minimum->second.value);
|
||||
limit != nullptr && array.size() < static_cast<std::size_t>(*limit)) {
|
||||
error = path + ": too few items";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool validate_against_schema(const JsonValue &value, const JsonValue &schema,
|
||||
const std::string &path, std::string &error) {
|
||||
const auto *schema_object = as_json_object(schema);
|
||||
if (schema_object == nullptr) {
|
||||
error = path + ": schema is not an object";
|
||||
return false;
|
||||
}
|
||||
if (const auto type = schema_object->find("type"); type != schema_object->end()) {
|
||||
bool matched = false;
|
||||
if (const auto *single = std::get_if<std::string>(&type->second.value); single != nullptr) {
|
||||
matched = json_type_matches(value, *single);
|
||||
} else if (const auto *multiple = as_json_array(type->second); multiple != nullptr) {
|
||||
for (const auto &entry : *multiple) {
|
||||
const auto *name = std::get_if<std::string>(&entry.value);
|
||||
if (name != nullptr && json_type_matches(value, *name)) {
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!matched) {
|
||||
error = path + ": value does not match the declared type";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (const auto enumeration = schema_object->find("enum");
|
||||
enumeration != schema_object->end()) {
|
||||
const auto *allowed = as_json_array(enumeration->second);
|
||||
bool found = false;
|
||||
if (allowed != nullptr) {
|
||||
auto serialized_value = Void::Json::Strict::serialize(value);
|
||||
for (const auto &entry : *allowed) {
|
||||
auto serialized_entry = Void::Json::Strict::serialize(entry);
|
||||
if (serialized_value && serialized_entry && *serialized_value == *serialized_entry) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
error = path + ": value is not in the schema enum";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (const auto *object = as_json_object(value); object != nullptr) {
|
||||
if (!validate_object_against_schema(*object, *schema_object, path, error)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (const auto *array = as_json_array(value); array != nullptr) {
|
||||
if (!validate_array_against_schema(*array, *schema_object, path, error)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (const auto minimum = schema_object->find("minimum"); minimum != schema_object->end()) {
|
||||
double actual = 0.0;
|
||||
if (const auto *integer = std::get_if<std::int64_t>(&value.value); integer != nullptr) {
|
||||
actual = static_cast<double>(*integer);
|
||||
} else if (const auto *real = std::get_if<double>(&value.value); real != nullptr) {
|
||||
actual = *real;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
if (const auto *limit = std::get_if<std::int64_t>(&minimum->second.value);
|
||||
limit != nullptr && actual < static_cast<double>(*limit)) {
|
||||
error = path + ": value is below the schema minimum";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::filesystem::path locate_repository_root() {
|
||||
std::error_code ec;
|
||||
auto directory = locate_vbs_binary().parent_path();
|
||||
for (int depth = 0; depth < 8 && !directory.empty(); ++depth) {
|
||||
if (std::filesystem::is_regular_file(directory / "schemas" / "metadata-v1.schema.json",
|
||||
ec)) {
|
||||
return directory;
|
||||
}
|
||||
if (!directory.has_parent_path()) {
|
||||
break;
|
||||
}
|
||||
directory = directory.parent_path();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(FullV1Tests, workspace_members_shared_build_and_member_selection) {
|
||||
const auto vbs_bin = locate_vbs_binary();
|
||||
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
|
||||
@@ -5646,3 +5835,154 @@ void build(Void::Build::Project &p) {
|
||||
(tmp / "cov4").string(), "--coverage-diff", low_baseline.string()});
|
||||
Void::Testing::expect(kept.has_value() && kept->succeeded());
|
||||
}
|
||||
|
||||
// === G6: versioned JSON schemas for metadata / describe / explain ===
|
||||
VOID_TEST(FullV1Tests, json_schemas_validate_metadata_describe_and_explain) {
|
||||
const auto vbs_bin = locate_vbs_binary();
|
||||
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
|
||||
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
|
||||
return;
|
||||
}
|
||||
const auto repository_root = locate_repository_root();
|
||||
Void::Testing::require(!repository_root.empty());
|
||||
const auto load_schema = [&](std::string_view name) {
|
||||
auto text = Void::FileSystem::read_file(repository_root / "schemas" / name);
|
||||
Void::Testing::require(text.has_value());
|
||||
auto parsed = Void::Json::Strict::parse(*text);
|
||||
Void::Testing::require(parsed.has_value());
|
||||
return *parsed;
|
||||
};
|
||||
const auto metadata_schema = load_schema("metadata-v1.schema.json");
|
||||
const auto describe_schema = load_schema("describe-v1.schema.json");
|
||||
const auto explain_schema = load_schema("explain-v1.schema.json");
|
||||
|
||||
Void::FileSystem::TempDir tmp{"vbs_schemas_"};
|
||||
const auto proj = tmp / "proj";
|
||||
write_file(proj / "build.cpp", R"cpp(#include <Void/Build.hpp>
|
||||
void build(Void::Build::Project &p) {
|
||||
p.feature("ssl", true);
|
||||
auto lib = p.static_library("demo")
|
||||
.cxx_standard(Void::Build::CxxStandard::Cxx26)
|
||||
.sources({"src/demo.cpp"});
|
||||
p.executable("app")
|
||||
.cxx_standard(Void::Build::CxxStandard::Cxx26)
|
||||
.sources({"src/main.cpp"})
|
||||
.link(lib);
|
||||
p.test("check")
|
||||
.cxx_standard(Void::Build::CxxStandard::Cxx26)
|
||||
.sources({"tests/check.cpp"});
|
||||
}
|
||||
)cpp");
|
||||
write_file(proj / "src" / "demo.cpp", "int demo() { return 1; }\n");
|
||||
write_file(proj / "src" / "main.cpp", "int demo();\nint main() { return demo() == 1 ? 0 : 1; }\n");
|
||||
write_file(proj / "tests" / "check.cpp", "int main() { return 0; }\n");
|
||||
|
||||
auto run_vbs = [&](std::vector<std::string> args) {
|
||||
Platform::ProcessRequest req;
|
||||
req.executable = vbs_bin;
|
||||
req.arguments = std::move(args);
|
||||
req.timeout = std::chrono::seconds{600};
|
||||
return Platform::run_supervised(req);
|
||||
};
|
||||
const auto parse_document = [](const Platform::ProcessResult &result) {
|
||||
auto parsed = Void::Json::Strict::parse(result.stdout_data);
|
||||
Void::Testing::require(parsed.has_value());
|
||||
return *parsed;
|
||||
};
|
||||
const auto schema_version = [](const JsonValue &document) -> std::int64_t {
|
||||
const auto *object = as_json_object(document);
|
||||
Void::Testing::require(object != nullptr);
|
||||
const auto *version = std::get_if<std::int64_t>(&object->at("schema_version").value);
|
||||
Void::Testing::require(version != nullptr);
|
||||
return *version;
|
||||
};
|
||||
|
||||
auto metadata = run_vbs({"metadata", "--project", proj.string(), "--build-dir",
|
||||
(tmp / "build").string()});
|
||||
Void::Testing::expect(metadata.has_value() && metadata->succeeded());
|
||||
Void::Testing::require(metadata.has_value());
|
||||
const auto metadata_document = parse_document(*metadata);
|
||||
{
|
||||
std::string error;
|
||||
const bool valid = validate_against_schema(metadata_document, metadata_schema, "$",
|
||||
error);
|
||||
if (!valid) {
|
||||
std::cerr << "metadata schema: " << error << "\n" << metadata->stdout_data;
|
||||
}
|
||||
Void::Testing::expect(valid);
|
||||
}
|
||||
Void::Testing::expect_eq(schema_version(metadata_document), 1LL);
|
||||
|
||||
auto describe = run_vbs({"describe", "--project", proj.string(), "--build-dir",
|
||||
(tmp / "build").string()});
|
||||
Void::Testing::expect(describe.has_value() && describe->succeeded());
|
||||
Void::Testing::require(describe.has_value());
|
||||
const auto describe_document = parse_document(*describe);
|
||||
{
|
||||
std::string error;
|
||||
const bool valid = validate_against_schema(describe_document, describe_schema, "$",
|
||||
error);
|
||||
if (!valid) {
|
||||
std::cerr << "describe schema: " << error << "\n" << describe->stdout_data;
|
||||
}
|
||||
Void::Testing::expect(valid);
|
||||
}
|
||||
Void::Testing::expect_eq(schema_version(describe_document), 1LL);
|
||||
|
||||
auto explained = run_vbs({"explain", "--project", proj.string(), "--build-dir",
|
||||
(tmp / "build").string(), "--format", "json"});
|
||||
Void::Testing::expect(explained.has_value() && explained->succeeded());
|
||||
Void::Testing::require(explained.has_value());
|
||||
const auto explain_document = parse_document(*explained);
|
||||
{
|
||||
std::string error;
|
||||
const bool valid =
|
||||
validate_against_schema(explain_document, explain_schema, "$", error);
|
||||
if (!valid) {
|
||||
std::cerr << "explain schema: " << error << "\n" << explained->stdout_data;
|
||||
}
|
||||
Void::Testing::expect(valid);
|
||||
}
|
||||
Void::Testing::expect_eq(schema_version(explain_document), 1LL);
|
||||
if (const auto *object = as_json_object(explain_document); object != nullptr) {
|
||||
const auto *actions = as_json_array(object->at("actions"));
|
||||
Void::Testing::require(actions != nullptr);
|
||||
Void::Testing::expect(!actions->empty());
|
||||
}
|
||||
|
||||
// Negative cases: the validator must actually reject drift.
|
||||
auto missing_required = metadata_document;
|
||||
if (auto *object = std::get_if<JsonValue::Object>(&missing_required.value); object != nullptr) {
|
||||
object->erase("targets");
|
||||
std::string error;
|
||||
Void::Testing::expect(
|
||||
!validate_against_schema(missing_required, metadata_schema, "$", error));
|
||||
Void::Testing::expect(error.find("missing required property 'targets'") !=
|
||||
std::string::npos);
|
||||
}
|
||||
auto wrong_enum = metadata_document;
|
||||
if (auto *object = std::get_if<JsonValue::Object>(&wrong_enum.value); object != nullptr) {
|
||||
auto targets = object->find("targets");
|
||||
if (targets != object->end()) {
|
||||
if (auto *array = std::get_if<JsonValue::Array>(&targets->second.value);
|
||||
array != nullptr && !array->empty()) {
|
||||
if (auto *target = std::get_if<JsonValue::Object>(&(*array)[0].value);
|
||||
target != nullptr) {
|
||||
(*target)["kind"] = JsonValue{std::string{"bogus"}};
|
||||
}
|
||||
}
|
||||
}
|
||||
std::string error;
|
||||
Void::Testing::expect(!validate_against_schema(wrong_enum, metadata_schema, "$", error));
|
||||
Void::Testing::expect(error.find("enum") != std::string::npos);
|
||||
}
|
||||
auto extra_property = metadata_document;
|
||||
if (auto *object = std::get_if<JsonValue::Object>(&extra_property.value); object != nullptr) {
|
||||
(*object)["surprise"] = JsonValue{std::string{"drift"}};
|
||||
std::string error;
|
||||
Void::Testing::expect(
|
||||
!validate_against_schema(extra_property, metadata_schema, "$", error));
|
||||
Void::Testing::expect(error.find("unexpected property 'surprise'") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user