Skip to content

Commit 607c1bd

Browse files
added minimal float check
1 parent a45ffb8 commit 607c1bd

File tree

3 files changed

+104
-17
lines changed

3 files changed

+104
-17
lines changed

src/sjv/sjv.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ namespace sjv
1414
{
1515
try
1616
{
17-
1817
// Polymorphism on list, for us a single item or a list are indistinguishable
18+
// All the elements in the list must pass the test
1919
if (input.is_array())
20+
{
2021
for (auto i : input)
21-
verify_json(pointer, i, rules);
22+
if (!verify_json(pointer, i, rules))
23+
return false;
24+
return true;
25+
}
2226

2327
// Find all rules that apply for the input node
2428
// TODO: accelerate this
@@ -29,11 +33,16 @@ namespace sjv
2933
matching_rules.push_back(i);
3034
}
3135

32-
// There must be at least one, otherwise warning
36+
// There must be at least one, otherwise warning and return true if not strict
3337
if (matching_rules.empty())
38+
{
3439
std::cout << "WARNING: "
3540
<< "Unknown entry " << pointer << std::endl;
3641

42+
if (strict)
43+
return false;
44+
}
45+
3746
// Test all rules, only one must pass, otherwise throw exception
3847
int count = 0;
3948

@@ -42,34 +51,57 @@ namespace sjv
4251
count++;
4352

4453
if (count != 1)
45-
throw matching_rules;
54+
{
55+
std::cout << "ERROR: Multiple valid rules in this list, only one should be valid:" << std::endl;
56+
for (auto i : matching_rules)
57+
std::cout << i << std::endl;
58+
return false;
59+
}
4660

4761
// If it passes and if it is a dictionary, then test all childrens
62+
// TODO: if any of these fails we need to report it somehow
4863
if (input.is_structured())
4964
for (auto &i : input.items())
50-
verify_json(pointer + i.key() + "/", i.value(), rules);
65+
if (!verify_json(pointer + i.key() + "/", i.value(), rules))
66+
return false;
5167

5268
// If they all pass, return true
5369
return true;
5470
}
5571
catch (std::vector<json> e)
5672
{
57-
for (auto i: e)
58-
std::cout << "ERROR: " << i << std::endl;
5973
return false;
6074
}
6175
};
6276
bool sjv::verify_rule(const json &input, const json &rule)
6377
{
64-
return false;
78+
string type = rule.at("type");
79+
if (type == "skip_check")
80+
return true;
81+
else if (type == "float")
82+
return verify_rule_float(input, rule);
83+
else
84+
{
85+
std::cout << "ERROR: Unknown type " << std::endl;
86+
exit(-1);
87+
return false;
88+
}
6589
};
6690
bool sjv::verify_rule_file(const json &input, const json &rule)
6791
{
6892
return false;
6993
};
70-
bool sjv::verify_rule_double(const json &input, const json &rule)
94+
bool sjv::verify_rule_float(const json &input, const json &rule)
7195
{
72-
return false;
96+
assert(rule.at("type") == "float");
97+
98+
if (!input.is_number_float())
99+
return false;
100+
101+
if (rule.contains("min") && input < rule["min"])
102+
return false;
103+
104+
return true;
73105
};
74106
bool sjv::verify_rule_int(const json &input, const json &rule)
75107
{

src/sjv/sjv.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ namespace sjv
2121

2222
// Type-specific rule handlers
2323
bool verify_rule_file(const json &input, const json &rule);
24-
bool verify_rule_double(const json &input, const json &rule);
24+
bool verify_rule_float(const json &input, const json &rule);
2525
bool verify_rule_int(const json &input, const json &rule);
2626
bool verify_rule_path(const json &input, const json &rule);
2727
bool verify_rule_string(const json &input, const json &rule);
2828
bool verify_rule_dict(const json &input, const json &rule);
2929

3030
// Working directory
31-
string cwd;
31+
string cwd = "";
32+
33+
// if strict == false, a json is valid even if it has entries not validated by a rule
34+
bool strict = false;
3235
};
3336

3437
} // namespace sjv

tests/test_validator.cpp

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ TEST_CASE("single_rule", "[validator]")
1313
{
1414
json input = R"(
1515
{
16-
"field1": "string",
17-
"field2": 42
1816
}
1917
)"_json;
2018

2119
json rules = R"(
2220
[
2321
{
2422
"pointer": "/",
25-
"type": "dict",
26-
"key_fields": ["field1"],
27-
"mandatory_fields": ["field1"]
23+
"type": "skip_check"
2824
}
2925
]
3026
)"_json;
@@ -33,3 +29,59 @@ TEST_CASE("single_rule", "[validator]")
3329

3430
REQUIRE(sjv.verify_json(input,rules));
3531
}
32+
33+
TEST_CASE("only_one_rule_can_be_valid", "[validator]")
34+
{
35+
json input = R"(
36+
{
37+
}
38+
)"_json;
39+
40+
json rules = R"(
41+
[
42+
{
43+
"pointer": "/",
44+
"type": "skip_check"
45+
},
46+
{
47+
"pointer": "/",
48+
"type": "skip_check"
49+
}
50+
]
51+
)"_json;
52+
53+
sjv::sjv sjv;
54+
55+
REQUIRE(!sjv.verify_json(input,rules));
56+
}
57+
58+
TEST_CASE("min_bound_numeric", "[validator]")
59+
{
60+
json input = R"(
61+
{
62+
"field1": 48.5
63+
}
64+
)"_json;
65+
66+
json rules = R"(
67+
[
68+
{
69+
"pointer": "/",
70+
"type": "skip_check"
71+
},
72+
{
73+
"pointer": "/field1/",
74+
"type": "float",
75+
"min": 45
76+
}
77+
]
78+
)"_json;
79+
80+
sjv::sjv sjv;
81+
82+
REQUIRE(sjv.verify_json(input,rules));
83+
84+
input["field1"] = 40.5;
85+
86+
REQUIRE(!sjv.verify_json(input,rules));
87+
}

0 commit comments

Comments
 (0)