Ajout du validateur json + correctif factory metadata

This commit is contained in:
Tom Ray
2026-04-02 00:07:49 +02:00
parent 48348936a8
commit 55a3c05cbe
206 changed files with 17070 additions and 320 deletions

View File

@@ -0,0 +1,7 @@
add_executable(issue-93 issue-93.cpp)
target_link_libraries(issue-93 nlohmann_json_schema_validator)
add_test(NAME issue-93
COMMAND issue-93
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -0,0 +1,11 @@
{
"type":"array",
"items": {
"type":"object",
"properties": {
"renderable": {
"$ref":"/components.schema.json#/Renderable"
}
}
}
}

View File

@@ -0,0 +1,13 @@
{
"Renderable": {
"type":"object",
"properties": {
"fg":{
"$ref":"/types/color.schema.json"
},
"bg":{
"$ref":"/types/color.schema.json"
}
}
}
}

View File

@@ -0,0 +1,55 @@
#include <nlohmann/json-schema.hpp>
#include <fstream>
#include <iostream>
using nlohmann::json;
using nlohmann::json_uri;
using nlohmann::json_schema::json_validator;
static const auto expected_patch = R"(
[{"op":"add","path":"/0/renderable/bg","value":"Black"}]
)"_json;
static const auto instance = R"(
[
{
"name":"player",
"renderable": {
"fg":"White"
}
}
]
)"_json;
static void loader(const json_uri &uri, json &schema)
{
std::string filename = "./" + uri.path();
std::ifstream lf(filename);
if (!lf.good())
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
try {
lf >> schema;
} catch (const std::exception &e) {
throw e;
}
}
int main(void)
{
json_validator validator(loader);
std::fstream f("blueprints.schema.json");
json schema;
f >> schema;
validator.set_root_schema(schema);
auto missing_default_patch = validator.validate(instance);
std::cerr << missing_default_patch << "\n";
std::cerr << expected_patch << "\n";
return missing_default_patch != expected_patch;
}

View File

@@ -0,0 +1,4 @@
{
"type":"string",
"default":"Black"
}