Skip to content

Commit a45ffb8

Browse files
recursive visitor and prototypes
1 parent 300f96c commit a45ffb8

File tree

5 files changed

+169
-22
lines changed

5 files changed

+169
-22
lines changed

.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "C/C++: g++ build and debug active file",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "/home/daniele/git/simple-json-validator/build/tests/unit_tests",
9+
"args": [],
10+
"stopAtEntry": false,
11+
"cwd": "${workspaceFolder}",
12+
"environment": [],
13+
"externalConsole": false,
14+
"MIMode": "gdb",
15+
"miDebuggerPath": "/usr/bin/gdb",
16+
"setupCommands": [
17+
{
18+
"description": "Enable pretty-printing for gdb",
19+
"text": "-enable-pretty-printing",
20+
"ignoreFailures": true
21+
}
22+
],
23+
}
24+
]
25+
}

data/input-rules.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
11
[
22
{
3-
"name": "defaults",
3+
"pointer": "defaults",
44
"type": "file",
55
"format": ["json"],
66
"doc" : "path to another JSON file containing the default parameters (this file will be patched on top of the defaults)"
77
},
88
{
9-
"name": "root_path",
9+
"pointer": "root_path",
1010
"type": "path",
1111
"required": true,
1212
"doc": "path to root director in the simulation (by default this is the directory containing this JSON file, so other paths can be relative to the JSON's path)"
1313
},
1414
{
15-
"name": "geometry",
16-
"type": "[dict]",
15+
"pointer": "geometry",
16+
"type": "dict",
1717
"required": true,
18+
"list_allowed": true
1819
"doc": "This is the geometry"
1920
},
2021
{
21-
"name": "geometry",
22+
"pointer": "geometry",
2223
"type": "dict",
2324
"required": true,
2425
"doc": "This is the geometry"
2526
},
2627
{
27-
"name": "geometry/type",
28+
"pointer": "geometry/type",
2829
"type": "multiplechoice",
2930
"choices": ["mesh", "plane"],
3031
"default": "mesh",
3132
"doc": "What type of geometry is this? (for now only mesh is implemented)"
3233
},
3334
{
34-
"name": "geometry/mesh",
35+
"pointer": "geometry/mesh",
3536
"type": "file",
3637
"required": true,
3738
"fileformat": ["obj","msh"],
3839
"doc": "Path to mesh file"
3940
},
4041
{
41-
"name": "geometry/surface_selection",
42+
"pointer": "geometry/surface_selection",
4243
"type": "dict",
4344
"required_fields": ["id","axis","position"],
4445
"doc": "Describes a surface selection"
4546
},
4647
{
47-
"name": "geometry/surface_selection",
48+
"pointer": "geometry/surface_selection",
4849
"type": "integer",
4950
"min": 0,
5051

5152
},
5253
{
53-
"name": "geometry/surface_selection",
54+
"pointer": "geometry/surface_selection",
5455
"type": "file",
5556
"format": ["txt"],
5657
},
5758
{
58-
"name": "geometry/volume_selection",
59+
"pointer": "geometry/volume_selection",
5960
"like": "geometry/surface_selection",
6061
"doc": "The following are different ways to specify volume_selection"
6162
}
6263
]
6364

6465
* accepts both lists or single elements transparently
65-
* if multiple rules apply to the same name, just one needs to pass
66+
* if multiple rules apply to the same pointer, just one needs to pass
6667
* any type becomes a list with "[]"
6768
* warnings if some fields do not have rules

src/sjv/sjv.cpp

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,91 @@
11
////////////////////////////////////////////////////////////////////////////////
22
#include <sjv/sjv.h>
3-
3+
#include <iostream>
44
////////////////////////////////////////////////////////////////////////////////
55

66
namespace sjv
77
{
8-
9-
}
8+
bool sjv::verify_json(const json &input, const json &rules)
9+
{
10+
return verify_json("/", input, rules);
11+
}
12+
13+
bool sjv::verify_json(const string &pointer, const json &input, const json &rules)
14+
{
15+
try
16+
{
17+
18+
// Polymorphism on list, for us a single item or a list are indistinguishable
19+
if (input.is_array())
20+
for (auto i : input)
21+
verify_json(pointer, i, rules);
22+
23+
// Find all rules that apply for the input node
24+
// TODO: accelerate this
25+
std::vector<json> matching_rules;
26+
for (auto i : rules)
27+
{
28+
if (i.at("pointer") == pointer)
29+
matching_rules.push_back(i);
30+
}
31+
32+
// There must be at least one, otherwise warning
33+
if (matching_rules.empty())
34+
std::cout << "WARNING: "
35+
<< "Unknown entry " << pointer << std::endl;
36+
37+
// Test all rules, only one must pass, otherwise throw exception
38+
int count = 0;
39+
40+
for (auto i : matching_rules)
41+
if (verify_rule(input, i))
42+
count++;
43+
44+
if (count != 1)
45+
throw matching_rules;
46+
47+
// If it passes and if it is a dictionary, then test all childrens
48+
if (input.is_structured())
49+
for (auto &i : input.items())
50+
verify_json(pointer + i.key() + "/", i.value(), rules);
51+
52+
// If they all pass, return true
53+
return true;
54+
}
55+
catch (std::vector<json> e)
56+
{
57+
for (auto i: e)
58+
std::cout << "ERROR: " << i << std::endl;
59+
return false;
60+
}
61+
};
62+
bool sjv::verify_rule(const json &input, const json &rule)
63+
{
64+
return false;
65+
};
66+
bool sjv::verify_rule_file(const json &input, const json &rule)
67+
{
68+
return false;
69+
};
70+
bool sjv::verify_rule_double(const json &input, const json &rule)
71+
{
72+
return false;
73+
};
74+
bool sjv::verify_rule_int(const json &input, const json &rule)
75+
{
76+
return false;
77+
};
78+
bool sjv::verify_rule_path(const json &input, const json &rule)
79+
{
80+
return false;
81+
};
82+
bool sjv::verify_rule_string(const json &input, const json &rule)
83+
{
84+
return false;
85+
};
86+
bool sjv::verify_rule_dict(const json &input, const json &rule)
87+
{
88+
return false;
89+
};
90+
91+
} // namespace sjv

src/sjv/sjv.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
#pragma once
22

3-
////////////////////////////////////////////////////////////////////////////////
43
#include <nlohmann/json.hpp>
5-
using json = nlohmann::json;
6-
4+
#include <string>
75
#include <memory>
8-
////////////////////////////////////////////////////////////////////////////////
6+
7+
using json = nlohmann::json;
8+
using string = std::string;
99

1010
namespace sjv
1111
{
12+
class sjv
13+
{
14+
public:
15+
// verify the input json against the set of rules in specs
16+
bool verify_json(const json &input, const json &rules);
17+
bool verify_json(const string &pointer, const json &input, const json &rules);
18+
19+
// Dispatcher for rule verification
20+
bool verify_rule(const json &input, const json &rule);
21+
22+
// Type-specific rule handlers
23+
bool verify_rule_file(const json &input, const json &rule);
24+
bool verify_rule_double(const json &input, const json &rule);
25+
bool verify_rule_int(const json &input, const json &rule);
26+
bool verify_rule_path(const json &input, const json &rule);
27+
bool verify_rule_string(const json &input, const json &rule);
28+
bool verify_rule_dict(const json &input, const json &rule);
29+
30+
// Working directory
31+
string cwd;
32+
};
1233

1334
} // namespace sjv

tests/test_validator.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,27 @@
99

1010
using namespace sjv;
1111

12-
13-
TEST_CASE("eigen_params", "[solver]")
12+
TEST_CASE("single_rule", "[validator]")
1413
{
15-
REQUIRE(true);
14+
json input = R"(
15+
{
16+
"field1": "string",
17+
"field2": 42
18+
}
19+
)"_json;
20+
21+
json rules = R"(
22+
[
23+
{
24+
"pointer": "/",
25+
"type": "dict",
26+
"key_fields": ["field1"],
27+
"mandatory_fields": ["field1"]
28+
}
29+
]
30+
)"_json;
31+
32+
sjv::sjv sjv;
1633

34+
REQUIRE(sjv.verify_json(input,rules));
1735
}

0 commit comments

Comments
 (0)