Skip to content

Commit e62c7fa

Browse files
simple injection test passing
1 parent ee197c5 commit e62c7fa

File tree

3 files changed

+120
-15
lines changed

3 files changed

+120
-15
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": ["type_string"],
9+
"args": ["simple"],
1010
"stopAtEntry": false,
1111
"cwd": "/home/daniele/git/simple-json-validator/build/",
1212
"environment": [],

src/sjv/sjv.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ namespace sjv
3535
for (auto e : flat.items())
3636
{
3737
// If the pointer matches a strict subset of it, add it to the flattened
38-
std::tuple<bool, string> subset = is_subset_pointer(string(rule["pointer"]), e.key());
38+
std::tuple<bool, string> subset = is_subset_pointer(e.key(), string(rule["pointer"]));
3939
if (std::get<0>(subset))
4040
out_flat[std::get<1>(subset)] = rule["default"];
4141
}
42+
// Special case as "/" is not inserted in the flat representation
43+
std::tuple<bool, string> subset = is_subset_pointer("/", string(rule["pointer"]));
44+
if (std::get<0>(subset))
45+
out_flat[std::get<1>(subset)] = rule["default"];
4246
}
4347

4448
// Unflatten the input
@@ -76,6 +80,8 @@ namespace sjv
7680

7781
if (strict)
7882
return false;
83+
else
84+
return true;
7985
}
8086

8187
// Test all rules, only one must pass, otherwise throw exception
@@ -85,7 +91,7 @@ namespace sjv
8591
if (verify_rule(input, i))
8692
count++;
8793

88-
if (count == 0 && !matching_rules.empty())
94+
if (count == 0 && !matching_rules.empty())
8995
{
9096
// Before giving up, try boxing a primitive type
9197
if (boxing_primitive && !input.is_array())
@@ -383,7 +389,7 @@ namespace sjv
383389

384390
std::tuple<bool, string> SJV::is_subset_pointer(const string &json, const string &pointer)
385391
{
386-
392+
std::cout << "is_subset_pointer(" << json << "," << pointer << ")" << std::endl;
387393
// Splits a string into tokens using the deliminator delim
388394
auto tokenize = [](std::string const &str, const char delim) {
389395
size_t start;
@@ -427,25 +433,31 @@ namespace sjv
427433
|| (pointer_t.size() == 0))
428434
return {false, ""};
429435

430-
std::string buf = "/";
436+
std::string buf = "";
431437
// if it is shorter, match every entry
432438
for (unsigned i = 0; i < pointer_t.size(); ++i)
433439
{
434440
// if there is no corresponding entry on json, copy and move on
435-
if (json_t.size() < i)
436-
buf.append(replace_all(pointer_t[i], "*", "0") + "/");
437-
441+
if (json_t.size() <= i)
442+
{
443+
buf.append("/" + replace_all(pointer_t[i], "*", "0"));
444+
}
438445
// if there is an entry on json and it is the same, move on
439-
if (json_t[i] == pointer_t[i])
440-
buf.append(pointer_t[i] + "/");
441-
446+
else if (json_t[i] == pointer_t[i])
447+
{
448+
buf.append("/" + pointer_t[i]);
449+
}
442450
// if the pointer contains a star, then accept any integer on json
443-
if (pointer_t[i] == "*")
451+
else if (pointer_t[i] == "*")
452+
{
444453
if (is_number(json_t[i]))
445-
buf.append(json_t[i] + "/");
446-
454+
buf.append("/" + json_t[i]);
455+
}
447456
// if no rule matches it is not a match
448-
return {false, ""};
457+
else
458+
{
459+
return {false, ""};
460+
}
449461
}
450462

451463
return {true, buf};

tests/test_validator.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,96 @@ TEST_CASE("file_01", "[validator]")
236236
INFO(s);
237237
REQUIRE(r);
238238
}
239+
240+
TEST_CASE("simple", "[inject]")
241+
{
242+
json input = R"(
243+
{
244+
"string1": "teststring",
245+
"geometry":
246+
{
247+
"nested": 3
248+
}
249+
}
250+
)"_json;
251+
252+
json rules = R"(
253+
[
254+
{
255+
"pointer": "/",
256+
"type": "object",
257+
"required": ["string1"],
258+
"optional":["geometry","other"]
259+
},
260+
{
261+
"pointer": "/geometry",
262+
"type": "object",
263+
"default": null,
264+
"optional": ["nested"]
265+
},
266+
{
267+
"pointer": "/geometry/nested",
268+
"type": "int",
269+
"default": 3
270+
},
271+
{
272+
"pointer": "/other",
273+
"type": "int",
274+
"default": null
275+
},
276+
{
277+
"pointer": "/other/nested",
278+
"type": "int",
279+
"default": 3
280+
}
281+
]
282+
)"_json;
283+
284+
json output = R"(
285+
{
286+
"string1": "teststring",
287+
"geometry":
288+
{
289+
"nested": 3
290+
},
291+
"other":
292+
{
293+
"nested": 3
294+
}
295+
}
296+
)"_json;
297+
298+
sjv::SJV sjv;
299+
300+
sjv.strict = false;
301+
302+
bool r = sjv.verify_json(input,rules);
303+
std:: string s = sjv.log2str();
304+
INFO(s);
305+
REQUIRE(r);
306+
307+
json return_json = sjv.inject_defaults(input,rules);
308+
309+
std::cout << return_json << std::endl;
310+
311+
INFO(return_json);
312+
REQUIRE(return_json == output);
313+
}
314+
315+
// TEST_CASE("file_inject", "[inject]")
316+
// {
317+
// std::ifstream ifs1("../data/input_01.json");
318+
// json input = json::parse(ifs1);
319+
320+
// std::ifstream ifs2("../data/rules_01.json");
321+
// json rules = json::parse(ifs2);
322+
323+
// sjv::SJV sjv;
324+
325+
// sjv.strict = true;
326+
327+
// bool r = sjv.verify_json(input,rules);
328+
// std:: string s = sjv.log2str();
329+
// INFO(s);
330+
// REQUIRE(r);
331+
// }

0 commit comments

Comments
 (0)