Skip to content

Commit 3214114

Browse files
committed
embed json spec
1 parent 11d028e commit 3214114

File tree

3 files changed

+25
-49
lines changed

3 files changed

+25
-49
lines changed

src/jse/jse.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,31 @@ namespace jse
100100
enriched.push_back(rule);
101101
}
102102
// if the rule is an include, expand the node with a copy of the included file
103+
// include an embedded rule
104+
else if (embedded_rules.find(rule.at("spec_file")) != embedded_rules.end())
105+
{
106+
json embed_include_rules = embedded_rules[rule.at("spec_file")];
107+
// loop over all rules to add the prefix
108+
for (auto &i_rule : embed_include_rules)
109+
{
110+
string prefix = rule.at("pointer");
111+
string pointer = i_rule.at("pointer");
112+
string new_pointer = prepend_pointer(pointer, prefix);
113+
i_rule.at("pointer") = new_pointer;
114+
}
115+
116+
// save modified rules
117+
for (const auto &i_rule : embed_include_rules)
118+
enriched.push_back(i_rule);
119+
}
120+
// include an rule from a file
103121
else
104122
{
105123
bool replaced = false;
106124
// the include file could be in any of the include directories
125+
string spec_file = rule.at("spec_file");
107126
for (const auto &dir : dirs)
108127
{
109-
string spec_file = rule.at("spec_file");
110128
string f = dir + "/" + spec_file;
111129
// check if the file exists
112130
if (std::filesystem::is_regular_file(f))
@@ -367,7 +385,7 @@ namespace jse
367385
{
368386
assert(rule.at("type") == "float");
369387

370-
if (!input.is_number() && !input.is_null())
388+
if (!input.is_number())
371389
return false;
372390

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

386-
if (!input.is_number_integer() && !input.is_null())
404+
if (!input.is_number_integer())
387405
return false;
388406

389407
if (rule.contains("min") && input < rule["min"])

src/jse/jse.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace jse
3636
// additional directories which can be used for relative paths
3737
std::vector<string> include_directories;
3838

39+
// additional rules that can be used
40+
std::unordered_map<std::string, json> embedded_rules;
41+
3942
// automatic boxing for primitive types
4043
// if all rules fail for a basic type, try boxing it once and try again
4144
bool boxing_primitive = true;

tests/test_validator.cpp

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ TEST_CASE("include_rule", "[validator]")
250250
REQUIRE(new_rules == matching);
251251
}
252252

253+
253254
TEST_CASE("file_01", "[validator]")
254255
{
255256
std::ifstream ifs1(root_path + "/input_01.json");
@@ -610,49 +611,3 @@ TEST_CASE("polymorphism", "[inject]")
610611
INFO(return_json);
611612
REQUIRE(return_json == output);
612613
}
613-
614-
TEST_CASE("null_as_nan", "[validator][inject]")
615-
{
616-
nlohmann::json rules = R"(
617-
[
618-
{
619-
"pointer": "/",
620-
"type": "object",
621-
"required": ["f1"],
622-
"optional": ["f2"]
623-
},
624-
{
625-
"pointer": "/f1",
626-
"type": "float"
627-
},
628-
{
629-
"pointer": "/f2",
630-
"type": "int",
631-
"default": null
632-
}
633-
]
634-
)"_json;
635-
636-
nlohmann::json input = R"(
637-
{
638-
"f1": null,
639-
"f2": null
640-
}
641-
)"_json;
642-
643-
JSE jse;
644-
jse.strict = true;
645-
646-
bool r = jse.verify_json(input, rules);
647-
REQUIRE(r);
648-
649-
input.erase("f2");
650-
r = jse.verify_json(input, rules);
651-
REQUIRE(r);
652-
653-
const json return_json = jse.inject_defaults(input, rules);
654-
CHECK(return_json["f1"].is_null());
655-
CHECK(return_json["f2"].is_null());
656-
657-
input["f2"] = std::nan("");
658-
}

0 commit comments

Comments
 (0)