Skip to content

Allow embedding of json spec and revert Daniel's changes #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions src/jse/jse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,31 @@ namespace jse
enriched.push_back(rule);
}
// if the rule is an include, expand the node with a copy of the included file
// include an embedded rule
else if (embedded_rules.find(rule.at("spec_file")) != embedded_rules.end())
{
json embed_include_rules = embedded_rules[rule.at("spec_file")];
// loop over all rules to add the prefix
for (auto &i_rule : embed_include_rules)
{
string prefix = rule.at("pointer");
string pointer = i_rule.at("pointer");
string new_pointer = prepend_pointer(pointer, prefix);
i_rule.at("pointer") = new_pointer;
}

// save modified rules
for (const auto &i_rule : embed_include_rules)
enriched.push_back(i_rule);
}
// include an rule from a file
else
{
bool replaced = false;
// the include file could be in any of the include directories
string spec_file = rule.at("spec_file");
for (const auto &dir : dirs)
{
string spec_file = rule.at("spec_file");
string f = dir + "/" + spec_file;
// check if the file exists
if (std::filesystem::is_regular_file(f))
Expand Down Expand Up @@ -367,7 +385,7 @@ namespace jse
{
assert(rule.at("type") == "float");

if (!input.is_number() && !input.is_null())
if (!input.is_number())
return false;

if (rule.contains("min") && input < rule["min"])
Expand All @@ -383,7 +401,7 @@ namespace jse
{
assert(rule.at("type") == "int");

if (!input.is_number_integer() && !input.is_null())
if (!input.is_number_integer())
return false;

if (rule.contains("min") && input < rule["min"])
Expand Down
3 changes: 3 additions & 0 deletions src/jse/jse.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace jse
// additional directories which can be used for relative paths
std::vector<string> include_directories;

// additional rules that can be used
std::unordered_map<std::string, json> embedded_rules;

// automatic boxing for primitive types
// if all rules fail for a basic type, try boxing it once and try again
bool boxing_primitive = true;
Expand Down
47 changes: 1 addition & 46 deletions tests/test_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ TEST_CASE("include_rule", "[validator]")
REQUIRE(new_rules == matching);
}


TEST_CASE("file_01", "[validator]")
{
std::ifstream ifs1(root_path + "/input_01.json");
Expand Down Expand Up @@ -610,49 +611,3 @@ TEST_CASE("polymorphism", "[inject]")
INFO(return_json);
REQUIRE(return_json == output);
}

TEST_CASE("null_as_nan", "[validator][inject]")
{
nlohmann::json rules = R"(
[
{
"pointer": "/",
"type": "object",
"required": ["f1"],
"optional": ["f2"]
},
{
"pointer": "/f1",
"type": "float"
},
{
"pointer": "/f2",
"type": "int",
"default": null
}
]
)"_json;

nlohmann::json input = R"(
{
"f1": null,
"f2": null
}
)"_json;

JSE jse;
jse.strict = true;

bool r = jse.verify_json(input, rules);
REQUIRE(r);

input.erase("f2");
r = jse.verify_json(input, rules);
REQUIRE(r);

const json return_json = jse.inject_defaults(input, rules);
CHECK(return_json["f1"].is_null());
CHECK(return_json["f2"].is_null());

input["f2"] = std::nan("");
}
Loading