Skip to content

Commit 0c360c2

Browse files
added second test insertion
1 parent e62c7fa commit 0c360c2

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "cppdbg",
77
"request": "launch",
88
"program": "/home/daniele/git/simple-json-validator/build/tests/unit_tests",
9-
"args": ["simple"],
9+
"args": [""],
1010
"stopAtEntry": false,
1111
"cwd": "/home/daniele/git/simple-json-validator/build/",
1212
"environment": [],

src/sjv/sjv.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,23 @@ namespace sjv
3737
// If the pointer matches a strict subset of it, add it to the flattened
3838
std::tuple<bool, string> subset = is_subset_pointer(e.key(), string(rule["pointer"]));
3939
if (std::get<0>(subset))
40-
out_flat[std::get<1>(subset)] = rule["default"];
40+
if (!out_flat.contains(std::get<1>(subset)))
41+
out_flat[std::get<1>(subset)] = rule["default"];
42+
43+
// Try it also without the last entry to capture object types which are empty
44+
string key_parent = e.key().substr(0,e.key().find_last_of("/\\"));
45+
46+
subset = is_subset_pointer(key_parent, string(rule["pointer"]));
47+
if (std::get<0>(subset))
48+
if (!out_flat.contains(std::get<1>(subset)))
49+
out_flat[std::get<1>(subset)] = rule["default"];
50+
4151
}
4252
// Special case as "/" is not inserted in the flat representation
4353
std::tuple<bool, string> subset = is_subset_pointer("/", string(rule["pointer"]));
4454
if (std::get<0>(subset))
45-
out_flat[std::get<1>(subset)] = rule["default"];
55+
if (!out_flat.contains(std::get<1>(subset)))
56+
out_flat[std::get<1>(subset)] = rule["default"];
4657
}
4758

4859
// Unflatten the input
@@ -389,7 +400,6 @@ namespace sjv
389400

390401
std::tuple<bool, string> SJV::is_subset_pointer(const string &json, const string &pointer)
391402
{
392-
std::cout << "is_subset_pointer(" << json << "," << pointer << ")" << std::endl;
393403
// Splits a string into tokens using the deliminator delim
394404
auto tokenize = [](std::string const &str, const char delim) {
395405
size_t start;

tests/test_validator.cpp

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,26 +306,83 @@ TEST_CASE("simple", "[inject]")
306306

307307
json return_json = sjv.inject_defaults(input,rules);
308308

309-
std::cout << return_json << std::endl;
310-
311309
INFO(return_json);
312310
REQUIRE(return_json == output);
313311
}
314312

315-
// TEST_CASE("file_inject", "[inject]")
316-
// {
317-
// std::ifstream ifs1("../data/input_01.json");
318-
// json input = json::parse(ifs1);
313+
TEST_CASE("list", "[inject]")
314+
{
315+
json input = R"(
316+
{
317+
"list1":
318+
[
319+
{
320+
"count": 0,
321+
"other": 1
322+
},
323+
{
324+
"count": 0
325+
}
326+
]
327+
}
328+
)"_json;
319329

320-
// std::ifstream ifs2("../data/rules_01.json");
321-
// json rules = json::parse(ifs2);
330+
json rules = R"(
331+
[
332+
{
333+
"pointer": "/",
334+
"type": "object",
335+
"required": ["list1"]
336+
},
337+
{
338+
"pointer": "/list1",
339+
"type": "list"
340+
},
341+
{
342+
"pointer": "/list1/*",
343+
"type": "object",
344+
"required": ["count"],
345+
"optional": ["other"]
346+
},
347+
{
348+
"pointer": "/list1/*/count",
349+
"type": "int"
350+
},
351+
{
352+
"pointer": "/list1/*/other",
353+
"type": "int",
354+
"default": 2
355+
}
356+
]
357+
)"_json;
358+
359+
json output = R"(
360+
{
361+
"list1":
362+
[
363+
{
364+
"count": 0,
365+
"other": 1
366+
},
367+
{
368+
"count": 0,
369+
"other": 2
370+
}
371+
]
372+
}
373+
)"_json;
374+
375+
sjv::SJV sjv;
322376

323-
// sjv::SJV sjv;
377+
sjv.strict = false;
378+
379+
bool r = sjv.verify_json(input,rules);
380+
std:: string s = sjv.log2str();
381+
INFO(s);
382+
REQUIRE(r);
324383

325-
// sjv.strict = true;
384+
json return_json = sjv.inject_defaults(input,rules);
326385

327-
// bool r = sjv.verify_json(input,rules);
328-
// std:: string s = sjv.log2str();
329-
// INFO(s);
330-
// REQUIRE(r);
331-
// }
386+
INFO(return_json);
387+
REQUIRE(return_json == output);
388+
}

0 commit comments

Comments
 (0)