22#include < sjv/sjv.h>
33#include < iostream>
44#include < filesystem> // C++17
5+ #include < sstream>
56// //////////////////////////////////////////////////////////////////////////////
67
78namespace 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 };
0 commit comments