-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathschema_validator.cpp
More file actions
219 lines (186 loc) · 6.92 KB
/
schema_validator.cpp
File metadata and controls
219 lines (186 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
MIT License
Copyright (c) 2022 Jason Turner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <filesystem>
#include <fstream>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#include <functional>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include <iostream>
#include <CLI/CLI.hpp>
#include <spdlog/spdlog.h>
#include <json2cpp/json2cpp_adapter.hpp>
#include <valijson/adapters/nlohmann_json_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validator.hpp>
#include "schema.hpp"
bool validate(const std::filesystem::path &schema_file_name, const std::filesystem::path &file_to_validate)
{
using valijson::Schema;
using valijson::SchemaParser;
using valijson::Validator;
using valijson::adapters::json2cppJsonAdapter;
using valijson::adapters::NlohmannJsonAdapter;
// Parse JSON schema content using valijson
spdlog::info("Creating Schema object");
Schema mySchema;
spdlog::info("Creating SchemaParser object");
SchemaParser parser;
spdlog::info("Creating nlohmann::json object");
nlohmann::json schema;
spdlog::info("Opening json file");
std::ifstream schema_file(schema_file_name);
spdlog::info("Loading json file");
schema_file >> schema;
spdlog::info("Creating NlohmannJsonAdapter object");
NlohmannJsonAdapter mySchemaAdapter(schema);
spdlog::info("parser.populateSchema object");
parser.populateSchema(mySchemaAdapter, mySchema);
spdlog::info("Creating Validator object");
Validator validator;
spdlog::info("Creating nlohmann::json object");
nlohmann::json document;
spdlog::info("Opening json file");
std::ifstream input_file(file_to_validate);
spdlog::info("Loading json file");
input_file >> document;
spdlog::info("Creating NlohmannJsonAdapter object");
NlohmannJsonAdapter myTargetAdapter(document);
spdlog::info("validator.validate");
const auto result = validator.validate(mySchema, myTargetAdapter, nullptr);
spdlog::info("returning result {}", result);
return result;
}
bool validate_internal(const std::filesystem::path &file_to_validate)
{
using valijson::Schema;
using valijson::SchemaParser;
using valijson::Validator;
using valijson::adapters::json2cppJsonAdapter;
using valijson::adapters::NlohmannJsonAdapter;
// Parse JSON schema content using valijson
spdlog::info("Creating Schema object");
Schema mySchema;
spdlog::info("Creating SchemaParser object");
SchemaParser parser;
spdlog::info("Creating json2cppJsonAdapter object");
json2cppJsonAdapter mySchemaAdapter(compiled_json::energyplus_schema::get());
spdlog::info("parser.populateSchema object");
parser.populateSchema(mySchemaAdapter, mySchema);
spdlog::info("Creating Validator object");
Validator validator;
spdlog::info("Creating nlohmann::json object");
nlohmann::json document;
spdlog::info("Opening json file");
std::ifstream input_file(file_to_validate);
spdlog::info("Loading json file");
input_file >> document;
spdlog::info("Creating NlohmannJsonAdapter object");
NlohmannJsonAdapter myTargetAdapter(document);
spdlog::info("validator.validate");
const auto result = validator.validate(mySchema, myTargetAdapter, nullptr);
spdlog::info("returning result {}", result);
return result;
}
template<typename JSON>
void walk_internal(std::int64_t &int_sum,
double &double_sum,
std::size_t &string_sizes,
int &null_count,
int &array_count,
int &object_count,
const JSON &obj)
{
if (obj.is_number_integer()) {
int_sum += obj.template get<std::int64_t>();
} else if (obj.is_number_float()) {
double_sum += obj.template get<double>();
} else if (obj.is_string()) {
string_sizes += obj.template get<std::string_view>().size();
} else if (obj.is_null()) {
++null_count;
} else if (obj.is_array()) {
++array_count;
for (const auto &child : obj) {
walk_internal(int_sum, double_sum, string_sizes, null_count, array_count, object_count, child);
}
} else if (obj.is_object()) {
++object_count;
for (const auto &child : obj) {
walk_internal(int_sum, double_sum, string_sizes, null_count, array_count, object_count, child);
}
}
}
template<typename JSON> void walk(const JSON &objects)
{
std::int64_t int_sum{};
double double_sum{};
std::size_t string_sizes{};
int null_count{};
int array_count{};
int object_count{};
spdlog::info("Starting tree walk");
walk_internal(int_sum, double_sum, string_sizes, null_count, array_count, object_count, objects);
spdlog::info("{} {} {} {} {} {}", int_sum, double_sum, string_sizes, null_count, array_count, object_count);
}
int main(int argc, const char **argv)
{
try {
CLI::App app("schema_validator version 0.0.1");
std::string document_name;
std::filesystem::path schema_file_name;
std::filesystem::path document_to_validate;
bool do_walk = false;
bool internal = false;
bool show_version = false;
app.add_option("<schema_file>", schema_file_name);
auto *doc = app.add_option("<document_to_validate>", document_to_validate);
app.add_flag("--version", show_version, "Show version information");
app.add_flag("--walk", do_walk, "Just walk the schema and count objects (perf test)")->excludes(doc);
app.add_flag("--internal", internal, "Use internal schema");
CLI11_PARSE(app, argc, argv);
if (do_walk) {
if (internal) {
walk(compiled_json::energyplus_schema::get());
} else {
spdlog::info("Creating nlohmann::json object");
nlohmann::json schema;
spdlog::info("Opening json file");
std::ifstream schema_file(schema_file_name);
spdlog::info("Loading json file");
schema_file >> schema;
walk(schema);
}
return EXIT_SUCCESS;
}
if (internal) {
validate_internal(document_to_validate);
} else {
validate(schema_file_name, document_to_validate);
}
} catch (const std::exception &e) {
spdlog::error("Unhandled exception in main: {}", e.what());
}
}