Skip to content

Commit 82daafe

Browse files
added list as a basic type
1 parent 90d54c5 commit 82daafe

File tree

6 files changed

+77
-34
lines changed

6 files changed

+77
-34
lines changed

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

data/input_01.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
0,
88
0
99
],
10-
"rotation": -30,
10+
"rotation": [-30],
1111
"scale": [
1212
5,
1313
0.02
@@ -27,8 +27,8 @@
2727
0.26,
2828
0.4503
2929
],
30-
"rotation": -30,
31-
"scale": 1.0
30+
"rotation": [-30],
31+
"scale": [1.0]
3232
},
3333
"volume_selection": "../data/dummy.txt",
3434
"surface_selection": 2,

data/rules_01.json

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,95 @@
66
"doc": "Root of the configuration file."
77
},
88
{
9-
"pointer": "/geometry/",
9+
"pointer": "/geometry",
10+
"type": "list",
11+
"min": 1,
12+
"doc": "List of /geometry objects."
13+
},
14+
{
15+
"pointer": "/geometry/*",
1016
"type": "object",
1117
"required": ["mesh"],
1218
"optional": ["transformation","volume_selection","surface_selection","n_refs","advanced"],
1319
"doc": "Each geometry object stores a mesh, a set of transformations applied to it after loading, and a set of selections, which can be used to specify boundary conditions and materials."
1420
},
1521
{
16-
"pointer": "/geometry/mesh/",
22+
"pointer": "/geometry/*/mesh",
1723
"type": "file",
1824
"extensions": [".obj",".msh"],
1925
"doc": "Path of the mesh file to load."
2026
},
2127
{
22-
"pointer": "/geometry/transformation/",
28+
"pointer": "/geometry/*/transformation",
2329
"type": "object",
2430
"default": null,
2531
"optional": ["translation","rotation","scale"],
2632
"doc": "Geometric transformations applied to the geometry after loading it."
2733
},
2834
{
29-
"pointer": "/geometry/transformation/translation/",
35+
"pointer": "/geometry/*/transformation/translation",
36+
"type": "list",
37+
"default": null
38+
},
39+
{
40+
"pointer": "/geometry/*/transformation/rotation",
41+
"type": "list",
42+
"default": null
43+
},
44+
{
45+
"pointer": "/geometry/*/transformation/scale",
46+
"type": "list",
47+
"default": null
48+
},
49+
{
50+
"pointer": "/geometry/*/transformation/translation/*",
3051
"type": "float",
3152
"default": [0,0,0],
3253
"doc": "Translation vector (2 entries for 2D problems, 3 entries for 3D problems)."
3354
},
3455
{
35-
"pointer": "/geometry/transformation/rotation/",
56+
"pointer": "/geometry/*/transformation/rotation/*",
3657
"default": 0,
3758
"type": "float"
3859
},
3960
{
40-
"pointer": "/geometry/transformation/scale/",
61+
"pointer": "/geometry/*/transformation/scale/*",
4162
"default": 0,
4263
"type": "float"
4364
},
4465
{
45-
"pointer": "/geometry/volume_selection/",
66+
"pointer": "/geometry/*/volume_selection",
4667
"type": "int",
4768
"default": 0
4869
},
4970
{
50-
"pointer": "/geometry/volume_selection/",
71+
"pointer": "/geometry/*/volume_selection",
5172
"type": "file",
5273
"extensions": [".txt"]
5374
},
5475
{
55-
"pointer": "/geometry/surface_selection/",
76+
"pointer": "/geometry/*/surface_selection",
5677
"type": "int",
5778
"default": null
5879
},
5980
{
60-
"pointer": "/geometry/surface_selection/",
81+
"pointer": "/geometry/*/surface_selection",
6182
"type": "file",
6283
"extensions": [".txt"]
6384
},
6485
{
65-
"pointer": "/geometry/n_refs/",
86+
"pointer": "/geometry/*/n_refs",
6687
"type": "int",
6788
"default": 0
6889
},
6990
{
70-
"pointer": "/geometry/advanced/",
91+
"pointer": "/geometry/*/advanced",
7192
"type": "object",
7293
"optional": ["normalize_mesh"],
7394
"default": null
7495
},
7596
{
76-
"pointer": "/geometry/advanced/normalize_mesh/",
97+
"pointer": "/geometry/*/advanced/normalize_mesh",
7798
"type": "bool",
7899
"default": false
79100
}

src/sjv/sjv.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ namespace sjv
1515

1616
bool SJV::verify_json(const string &pointer, const json &input, const json &rules)
1717
{
18-
// Polymorphism on list, a single item or a list are indistinguishable
19-
// All the elements in the list must pass the test
20-
if (input.is_array())
21-
{
22-
for (auto i : input)
23-
if (!verify_json(pointer, i, rules))
24-
return false;
25-
return true;
26-
}
27-
2818
// Find all rules that apply for the input node
2919
// TODO: accelerate this
3020
std::vector<json> matching_rules;
@@ -52,6 +42,8 @@ namespace sjv
5242

5343
if (count == 0 && !matching_rules.empty())
5444
{
45+
// TODO: Before giving up, try again wrapping the entity in a list
46+
5547
std::stringstream s;
5648
s << "No valid rules in this list:";
5749
for (auto i : matching_rules)
@@ -71,10 +63,10 @@ namespace sjv
7163
}
7264

7365
// If it passes and if it is a dictionary, then test all childrens
74-
if (input.is_structured())
66+
if (input.is_object())
7567
for (auto &i : input.items())
7668
{
77-
string new_pointer = pointer + i.key() + "/";
69+
string new_pointer = (pointer == "/" ? "" : pointer) + "/" + i.key();
7870
// first of all, let's check if the specs are correct
7971
json defaults = collect_default_rules(new_pointer,rules);
8072

@@ -110,13 +102,25 @@ namespace sjv
110102

111103
}
112104

105+
// In case of a list
106+
// All the elements in the list must pass the test
107+
if (input.is_array())
108+
{
109+
for (auto i : input)
110+
if (!verify_json((pointer == "/" ? "" : pointer) + "/*", i, rules))
111+
return false;
112+
}
113+
114+
113115
// If they all pass, return true
114116
return true;
115117
};
116118
bool SJV::verify_rule(const json &input, const json &rule)
117119
{
118120
string type = rule.at("type");
119-
if (type == "float")
121+
if (type == "list")
122+
return verify_rule_list(input, rule);
123+
else if (type == "float")
120124
return verify_rule_float(input, rule);
121125
else if (type == "int")
122126
return verify_rule_int(input, rule);
@@ -255,6 +259,23 @@ namespace sjv
255259
return true;
256260
};
257261

262+
bool SJV::verify_rule_list(const json &input, const json &rule)
263+
{
264+
assert(rule.at("type") == "list");
265+
266+
if (!input.is_array())
267+
return false;
268+
269+
if (rule.contains("min") && !(input.size() >= rule["min"]))
270+
return false;
271+
272+
if (rule.contains("max") && !(input.size() <= rule["max"]))
273+
return false;
274+
275+
return true;
276+
}
277+
278+
258279
std::string SJV::log2str()
259280
{
260281
std::stringstream s;

src/sjv/sjv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ namespace sjv
2929
bool verify_rule_string(const json &input, const json &rule);
3030
bool verify_rule_object(const json &input, const json &rule);
3131
bool verify_rule_bool(const json &input, const json &rule);
32-
32+
bool verify_rule_list(const json &input, const json &rule);
33+
3334
// Collect all rules having a default for a given pointer
3435
json collect_default_rules(const string &pointer, const json &rules);
3536

tests/test_validator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ TEST_CASE("min_bound_numeric", "[validator]")
7676
"required": ["field1"]
7777
},
7878
{
79-
"pointer": "/field1/",
79+
"pointer": "/field1",
8080
"type": "float",
8181
"min": 45
8282
}
@@ -112,7 +112,7 @@ TEST_CASE("file_type", "[validator]")
112112
"optional": ["file1"]
113113
},
114114
{
115-
"pointer": "/file1/",
115+
"pointer": "/file1",
116116
"type": "file",
117117
"extensions": [".txt"],
118118
"default": "somestring"
@@ -160,7 +160,7 @@ TEST_CASE("type_string", "[validator]")
160160
"type": "object"
161161
},
162162
{
163-
"pointer": "/string1/",
163+
"pointer": "/string1",
164164
"type": "string"
165165
}
166166
]

0 commit comments

Comments
 (0)