Skip to content

Commit 9f6bf10

Browse files
added file type support
1 parent 4121331 commit 9f6bf10

File tree

6 files changed

+167
-7
lines changed

6 files changed

+167
-7
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"type": "cppdbg",
77
"request": "launch",
88
"program": "/home/daniele/git/simple-json-validator/build/tests/unit_tests",
9-
"args": [],
9+
"args": ["debug"],
1010
"stopAtEntry": false,
11-
"cwd": "${workspaceFolder}",
11+
"cwd": "/home/daniele/git/simple-json-validator/build/",
1212
"environment": [],
1313
"externalConsole": false,
1414
"MIMode": "gdb",

.vscode/settings.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"bit": "cpp",
6+
"*.tcc": "cpp",
7+
"bitset": "cpp",
8+
"cctype": "cpp",
9+
"chrono": "cpp",
10+
"clocale": "cpp",
11+
"cmath": "cpp",
12+
"codecvt": "cpp",
13+
"condition_variable": "cpp",
14+
"cstdarg": "cpp",
15+
"cstddef": "cpp",
16+
"cstdint": "cpp",
17+
"cstdio": "cpp",
18+
"cstdlib": "cpp",
19+
"cstring": "cpp",
20+
"ctime": "cpp",
21+
"cwchar": "cpp",
22+
"cwctype": "cpp",
23+
"deque": "cpp",
24+
"forward_list": "cpp",
25+
"map": "cpp",
26+
"set": "cpp",
27+
"unordered_map": "cpp",
28+
"vector": "cpp",
29+
"exception": "cpp",
30+
"algorithm": "cpp",
31+
"functional": "cpp",
32+
"iterator": "cpp",
33+
"memory": "cpp",
34+
"memory_resource": "cpp",
35+
"numeric": "cpp",
36+
"optional": "cpp",
37+
"random": "cpp",
38+
"ratio": "cpp",
39+
"regex": "cpp",
40+
"string": "cpp",
41+
"string_view": "cpp",
42+
"system_error": "cpp",
43+
"tuple": "cpp",
44+
"type_traits": "cpp",
45+
"utility": "cpp",
46+
"fstream": "cpp",
47+
"future": "cpp",
48+
"initializer_list": "cpp",
49+
"iomanip": "cpp",
50+
"iosfwd": "cpp",
51+
"iostream": "cpp",
52+
"istream": "cpp",
53+
"limits": "cpp",
54+
"mutex": "cpp",
55+
"new": "cpp",
56+
"ostream": "cpp",
57+
"shared_mutex": "cpp",
58+
"sstream": "cpp",
59+
"stdexcept": "cpp",
60+
"streambuf": "cpp",
61+
"thread": "cpp",
62+
"typeinfo": "cpp",
63+
"valarray": "cpp",
64+
"variant": "cpp"
65+
}
66+
}

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ target_link_libraries(sjv PUBLIC nlohmann::json)
123123
# Compiler options
124124
################################################################################
125125

126-
# Use C++14
127-
target_compile_features(sjv PUBLIC cxx_std_14)
126+
# Use C++17
127+
target_compile_features(sjv PUBLIC cxx_std_17)
128128

129129
################################################################################
130130
# Tests

src/sjv/sjv.cpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
////////////////////////////////////////////////////////////////////////////////
22
#include <sjv/sjv.h>
33
#include <iostream>
4+
#include <filesystem> // C++17
45
////////////////////////////////////////////////////////////////////////////////
56

67
namespace sjv
@@ -50,7 +51,15 @@ namespace sjv
5051
if (verify_rule(input, i))
5152
count++;
5253

53-
if (count != 1)
54+
if (count == 0)
55+
{
56+
std::cout << "ERROR: No valid rules in this list:" << std::endl;
57+
for (auto i : matching_rules)
58+
std::cout << i << std::endl;
59+
return false;
60+
}
61+
62+
if (count > 1)
5463
{
5564
std::cout << "ERROR: Multiple valid rules in this list, only one should be valid:" << std::endl;
5665
for (auto i : matching_rules)
@@ -80,6 +89,10 @@ namespace sjv
8089
return true;
8190
else if (type == "float")
8291
return verify_rule_float(input, rule);
92+
else if (type == "int")
93+
return verify_rule_float(input, rule);
94+
else if (type == "file")
95+
return verify_rule_file(input, rule);
8396
else
8497
{
8598
std::cout << "ERROR: Unknown type " << std::endl;
@@ -89,7 +102,26 @@ namespace sjv
89102
};
90103
bool sjv::verify_rule_file(const json &input, const json &rule)
91104
{
92-
return false;
105+
assert(rule.at("type") == "file");
106+
107+
std::string p_str = cwd + "/" + string(input);
108+
std::filesystem::path p = std::filesystem::path(p_str);
109+
110+
if (!std::filesystem::is_regular_file(p))
111+
return false;
112+
113+
if (rule.contains("extensions"))
114+
{
115+
std::string ext = p.extension();
116+
int count = 0;
117+
for (auto e : rule["extensions"])
118+
if (e == ext)
119+
count++;
120+
if (count != 1)
121+
return false;
122+
}
123+
124+
return true;
93125
};
94126
bool sjv::verify_rule_float(const json &input, const json &rule)
95127
{
@@ -101,11 +133,25 @@ namespace sjv
101133
if (rule.contains("min") && input < rule["min"])
102134
return false;
103135

136+
if (rule.contains("max") && input > rule["max"])
137+
return false;
138+
104139
return true;
105140
};
106141
bool sjv::verify_rule_int(const json &input, const json &rule)
107142
{
108-
return false;
143+
assert(rule.at("type") == "int");
144+
145+
if (!input.is_number_integer())
146+
return false;
147+
148+
if (rule.contains("min") && input < rule["min"])
149+
return false;
150+
151+
if (rule.contains("max") && input > rule["max"])
152+
return false;
153+
154+
return true;
109155
};
110156
bool sjv::verify_rule_path(const json &input, const json &rule)
111157
{

tags.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Tags supported for each type
2+
3+
## float,int:
4+
5+
* min
6+
* max
7+
8+
## file
9+
10+
* extensions

tests/test_validator.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <fstream>
66
#include <vector>
77
#include <ctime>
8+
#include <filesystem>
89
//////////////////////////////////////////////////////////////////////////
910

1011
using namespace sjv;
@@ -85,3 +86,40 @@ TEST_CASE("min_bound_numeric", "[validator]")
8586

8687
REQUIRE(!sjv.verify_json(input,rules));
8788
}
89+
90+
TEST_CASE("file_type", "[validator]")
91+
{
92+
json input = R"(
93+
{
94+
"file1": "CMakeCach.txt"
95+
}
96+
)"_json;
97+
98+
json rules = R"(
99+
[
100+
{
101+
"pointer": "/",
102+
"type": "skip_check"
103+
},
104+
{
105+
"pointer": "/file1/",
106+
"type": "file",
107+
"extensions": [".txt"]
108+
}
109+
]
110+
)"_json;
111+
112+
sjv::sjv sjv;
113+
sjv.cwd = std::filesystem::current_path();
114+
115+
REQUIRE(!sjv.verify_json(input,rules));
116+
117+
input["file1"] = "CMakeCache.txt";
118+
119+
REQUIRE(sjv.verify_json(input,rules));
120+
121+
rules[1]["extensions"][0] = ".msh";
122+
123+
REQUIRE(!sjv.verify_json(input,rules));
124+
125+
}

0 commit comments

Comments
 (0)