@@ -17,12 +17,7 @@ namespace sjv
1717 {
1818 // Find all rules that apply for the input node
1919 // TODO: accelerate this
20- std::vector<json> matching_rules;
21- for (auto i : rules)
22- {
23- if (i.at (" pointer" ) == pointer)
24- matching_rules.push_back (i);
25- }
20+ std::vector<json> matching_rules = collect_pointer (pointer, rules);
2621
2722 // There must be at least one, otherwise warning and return true if not strict
2823 if (matching_rules.empty ())
@@ -42,7 +37,15 @@ namespace sjv
4237
4338 if (count == 0 && !matching_rules.empty ())
4439 {
45- // TODO: Before giving up, try again wrapping the entity in a list
40+ // Before giving up, try boxing a primitive type
41+ if (boxing_primitive && !input.is_array ())
42+ {
43+ string new_pointer = (pointer == " /" ? " " : pointer) + " /*" ;
44+ // Make sure there are some rules for the boxed version before recursively checking
45+ if (collect_pointer (new_pointer, rules).size () > 0 )
46+ if (verify_json (new_pointer, input, rules))
47+ return true ;
48+ }
4649
4750 std::stringstream s;
4851 s << " No valid rules in this list:" ;
@@ -68,38 +71,37 @@ namespace sjv
6871 {
6972 string new_pointer = (pointer == " /" ? " " : pointer) + " /" + i.key ();
7073 // first of all, let's check if the specs are correct
71- json defaults = collect_default_rules (new_pointer,rules);
74+ json defaults = collect_default_rules (new_pointer, rules);
7275
7376 // if it is mandatory, make sure there are no defaults
74- if (matching_rules[0 ].contains (" required" ) && contained_in_list (i.key (),matching_rules[0 ][" required" ]))
77+ if (matching_rules[0 ].contains (" required" ) && contained_in_list (i.key (), matching_rules[0 ][" required" ]))
7578 {
7679 if (defaults.size () != 0 )
7780 {
78- log.push_back (log_item (" error" ," Inconsistent specifications: " + new_pointer + " is a mandatory field with a default value." ));
81+ log.push_back (log_item (" error" , " Inconsistent specifications: " + new_pointer + " is a mandatory field with a default value." ));
7982 return false ;
8083 }
8184 }
8285 // if it is optional, there should be only one default in the specs
83- else if (matching_rules[0 ].contains (" optional" ) && contained_in_list (i.key (),matching_rules[0 ][" optional" ]))
86+ else if (matching_rules[0 ].contains (" optional" ) && contained_in_list (i.key (), matching_rules[0 ][" optional" ]))
8487 {
8588 if (defaults.size () != 1 )
8689 {
87- log.push_back (log_item (" error" ," Inconsistent specifications: " + new_pointer + " is an optional field with " + std::to_string (defaults.size ()) + " default values." ));
90+ log.push_back (log_item (" error" , " Inconsistent specifications: " + new_pointer + " is an optional field with " + std::to_string (defaults.size ()) + " default values." ));
8891 return false ;
8992 }
9093 }
9194 // if it is not mandatory and not optional, something is wrong
9295 else
9396 {
94- log.push_back (log_item (" warning" ," Inconsistent specifications: " + new_pointer + " is neither an optional or a mandatory field." ));
97+ log.push_back (log_item (" warning" , " Inconsistent specifications: " + new_pointer + " is neither an optional or a mandatory field." ));
9598 if (strict)
9699 return false ;
97100 }
98101
99102 // now let's make sure it can be validated
100103 if (!verify_json (new_pointer, i.value (), rules))
101104 return false ;
102-
103105 }
104106
105107 // In case of a list
@@ -111,7 +113,6 @@ namespace sjv
111113 return false ;
112114 }
113115
114-
115116 // If they all pass, return true
116117 return true ;
117118 };
@@ -156,7 +157,7 @@ namespace sjv
156157 if (strict)
157158 return false ;
158159 }
159-
160+
160161 if (rule.contains (" extensions" ))
161162 {
162163 std::string ext = p.extension ();
@@ -183,7 +184,7 @@ namespace sjv
183184 if (strict)
184185 return false ;
185186 }
186-
187+
187188 return true ;
188189 };
189190 bool SJV::verify_rule_float (const json &input, const json &rule)
@@ -270,11 +271,10 @@ namespace sjv
270271 return false ;
271272
272273 if (rule.contains (" max" ) && !(input.size () <= rule[" max" ]))
273- return false ;
274+ return false ;
274275
275276 return true ;
276277 }
277-
278278
279279 std::string SJV::log2str ()
280280 {
@@ -286,7 +286,7 @@ namespace sjv
286286 }
287287 return s.str ();
288288 };
289-
289+
290290 json SJV::collect_default_rules (const string &pointer, const json &rules)
291291 {
292292 // Find all rules that apply for the input node
@@ -302,8 +302,19 @@ namespace sjv
302302 return matching_rules;
303303 };
304304
305- bool SJV::contained_in_list (string item, const json& list)
305+ bool SJV::contained_in_list (string item, const json &list)
306+ {
307+ return std::find (list.begin (), list.end (), item) != list.end ();
308+ };
309+
310+ std::vector<json> SJV::collect_pointer (const string& pointer, const json& rules)
306311 {
307- return std::find (list.begin (),list.end (),item) != list.end ();
312+ std::vector<json> matching_rules;
313+ for (auto i : rules)
314+ {
315+ if (i.at (" pointer" ) == pointer)
316+ matching_rules.push_back (i);
317+ }
318+ return matching_rules;
308319 };
309320} // namespace sjv
0 commit comments