Skip to content

Commit beb2f98

Browse files
added logger
1 parent fcbe04b commit beb2f98

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build/**
2+
.vscode/launch.json

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

src/sjv/sjv.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
#include <sjv/sjv.h>
33
#include <iostream>
44
#include <filesystem> // C++17
5+
#include <sstream>
56
////////////////////////////////////////////////////////////////////////////////
67

78
namespace sjv
89
{
910
bool sjv::verify_json(const json &input, const json &rules)
1011
{
12+
log.clear();
1113
return verify_json("/", input, rules);
1214
}
1315

1416
bool sjv::verify_json(const string &pointer, const json &input, const json &rules)
1517
{
16-
// Polymorphism on list, for us a single item or a list are indistinguishable
18+
// Polymorphism on list, a single item or a list are indistinguishable
1719
// All the elements in the list must pass the test
1820
if (input.is_array())
1921
{
@@ -35,8 +37,7 @@ namespace sjv
3537
// There must be at least one, otherwise warning and return true if not strict
3638
if (matching_rules.empty())
3739
{
38-
std::cout << "WARNING: "
39-
<< "Unknown entry " << pointer << std::endl;
40+
log.push_back(log_item("warning","Unknown entry " + pointer));
4041

4142
if (strict)
4243
return false;
@@ -51,22 +52,25 @@ namespace sjv
5152

5253
if (count == 0 && !matching_rules.empty())
5354
{
54-
std::cout << "ERROR: No valid rules in this list:" << std::endl;
55+
std::stringstream s;
56+
s << "No valid rules in this list:";
5557
for (auto i : matching_rules)
56-
std::cout << i << std::endl;
58+
s << i << std::endl;
59+
log.push_back(log_item("error",s.str()));
5760
return false;
5861
}
5962

6063
if (count > 1)
6164
{
62-
std::cout << "ERROR: Multiple valid rules in this list, only one should be valid:" << std::endl;
65+
std::stringstream s;
66+
s << "Multiple valid rules in this list, only one should be valid:";
6367
for (auto i : matching_rules)
64-
std::cout << i << std::endl;
68+
s << i << std::endl;
69+
log.push_back(log_item("error",s.str()));
6570
return false;
6671
}
6772

6873
// If it passes and if it is a dictionary, then test all childrens
69-
// TODO: if any of these fails we need to report it somehow
7074
if (input.is_structured())
7175
for (auto &i : input.items())
7276
if (!verify_json(pointer + i.key() + "/", i.value(), rules))
@@ -94,8 +98,7 @@ namespace sjv
9498
return verify_rule_object(input, rule);
9599
else
96100
{
97-
std::cout << "ERROR: Unknown type " << std::endl;
98-
exit(-1);
101+
log.push_back(log_item("error","Unknown rule type " + type));
99102
return false;
100103
}
101104
};

src/sjv/sjv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ namespace sjv
3232

3333
// if strict == false, a json is valid even if it has entries not validated by a rule
3434
bool strict = false;
35+
36+
// message list
37+
typedef std::pair<std::string,std::string> log_item;
38+
std::vector<log_item> log;
3539
};
3640

3741
} // namespace sjv

tags.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Tags supported for each type
22

3+
## all
4+
5+
list
6+
37
## float,int:
48

59
* min: number

0 commit comments

Comments
 (0)