/
JsonValidation.cpp
363 lines (315 loc) · 10.5 KB
/
JsonValidation.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <sstream>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <fmt/format.h>
#include <mariana-trench/Assert.h>
#include <mariana-trench/JsonValidation.h>
#include <mariana-trench/Log.h>
#include <mariana-trench/Redex.h>
namespace marianatrench {
namespace {
std::string invalid_argument_message(
const Json::Value& value,
const std::optional<std::string>& field,
const std::string& expected) {
auto field_information = field ? fmt::format(" for field `{}`", *field) : "";
return fmt::format(
"Error validating `{}`. Expected {}{}.",
boost::algorithm::trim_copy(JsonValidation::to_styled_string(value)),
expected,
field_information);
}
} // namespace
JsonValidationError::JsonValidationError(
const Json::Value& value,
const std::optional<std::string>& field,
const std::string& expected)
: std::invalid_argument(invalid_argument_message(value, field, expected)) {}
void JsonValidation::validate_object(
const Json::Value& value,
const std::string& expected) {
if (value.isNull() || !value.isObject()) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ expected);
}
}
void JsonValidation::validate_object(const Json::Value& value) {
validate_object(value, /* expected */ "non-null object");
}
const Json::Value& JsonValidation::object(
const Json::Value& value,
const std::string& field) {
validate_object(value, fmt::format("non-null object with field `{}`", field));
const auto& attribute = value[field];
if (attribute.isNull() || !attribute.isObject()) {
throw JsonValidationError(value, field, /* expected */ "non-null object");
}
return attribute;
}
std::string JsonValidation::string(const Json::Value& value) {
if (value.isNull() || !value.isString()) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ "string");
}
return value.asString();
}
std::string JsonValidation::string(
const Json::Value& value,
const std::string& field) {
validate_object(
value, fmt::format("non-null object with string field `{}`", field));
const auto& string = value[field];
if (string.isNull() || !string.isString()) {
throw JsonValidationError(value, field, /* expected */ "string");
}
return string.asString();
}
int JsonValidation::integer(const Json::Value& value) {
if (value.isNull() || !value.isInt()) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ "integer");
}
return value.asInt();
}
int JsonValidation::integer(
const Json::Value& value,
const std::string& field) {
validate_object(
value, fmt::format("non-null object with integer field `{}`", field));
const auto& integer = value[field];
if (integer.isNull() || !integer.isInt()) {
throw JsonValidationError(value, field, /* expected */ "integer");
}
return integer.asInt();
}
bool JsonValidation::boolean(const Json::Value& value) {
if (value.isNull() || !value.isBool()) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ "boolean");
}
return value.asBool();
}
bool JsonValidation::boolean(
const Json::Value& value,
const std::string& field) {
validate_object(
value, fmt::format("non-null object with boolean field `{}`", field));
const auto& boolean = value[field];
if (boolean.isNull() || !boolean.isBool()) {
throw JsonValidationError(value, field, /* expected */ "boolean");
}
return boolean.asBool();
}
const Json::Value& JsonValidation::null_or_array(const Json::Value& value) {
if (!value.isNull() && !value.isArray()) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ "null or array");
}
return value;
}
const Json::Value& JsonValidation::null_or_array(
const Json::Value& value,
const std::string& field) {
validate_object(
value,
fmt::format("non-null object with null or array field `{}`", field));
const auto& null_or_array = value[field];
if (!null_or_array.isNull() && !null_or_array.isArray()) {
throw JsonValidationError(value, field, /* expected */ "null or array");
}
return null_or_array;
}
const Json::Value& JsonValidation::nonempty_array(const Json::Value& value) {
if (value.isNull() || !value.isArray() || value.empty()) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ "non-empty array");
}
return value;
}
const Json::Value& JsonValidation::nonempty_array(
const Json::Value& value,
const std::string& field) {
validate_object(
value,
fmt::format("non-null object with non-empty array field `{}`", field));
const auto& nonempty_array = value[field];
if (nonempty_array.isNull() || !nonempty_array.isArray() ||
nonempty_array.empty()) {
throw JsonValidationError(value, field, /* expected */ "non-empty array");
}
return nonempty_array;
}
const Json::Value& JsonValidation::null_or_object(
const Json::Value& value,
const std::string& field) {
validate_object(
value,
fmt::format("non-null object with null or object field `{}`", field));
if (!value.isMember(field)) {
return Json::Value::nullSingleton();
}
return object(value, field);
}
const Json::Value& JsonValidation::object_or_string(
const Json::Value& value,
const std::string& field) {
validate_object(
value,
fmt::format("non-null object with object or string field `{}`", field));
const auto& attribute = value[field];
if (attribute.isNull() || (!attribute.isObject() && !attribute.isString())) {
throw JsonValidationError(value, field, /* expected */ "object or string");
}
return attribute;
}
DexType* JsonValidation::dex_type(const Json::Value& value) {
auto type_name = JsonValidation::string(value);
auto* type = redex::get_type(type_name);
if (!type) {
throw JsonValidationError(
value,
/* field */ std::nullopt,
/* expected */ "existing type name");
}
return type;
}
DexType* JsonValidation::dex_type(
const Json::Value& value,
const std::string& field) {
auto type_name = JsonValidation::string(value, field);
auto* type = redex::get_type(type_name);
if (!type) {
throw JsonValidationError(
value,
field,
/* expected */ "existing type name");
}
return type;
}
DexFieldRef* JsonValidation::dex_field(const Json::Value& value) {
auto field_name = JsonValidation::string(value);
auto* dex_field = redex::get_field(field_name);
if (!dex_field) {
throw JsonValidationError(
value, /* field */ std::nullopt, /* expected */ "existing field name");
}
return dex_field;
}
DexFieldRef* JsonValidation::dex_field(
const Json::Value& value,
const std::string& field) {
auto field_name = JsonValidation::string(value, field);
auto* dex_field = redex::get_field(field_name);
if (!dex_field) {
throw JsonValidationError(
value, field, /* expected */ "existing field name");
}
return dex_field;
}
Json::Value JsonValidation::parse_json(std::string string) {
std::istringstream stream(std::move(string));
static const auto reader = Json::CharReaderBuilder();
std::string errors;
Json::Value json;
if (!Json::parseFromStream(reader, stream, &json, &errors)) {
throw std::invalid_argument(fmt::format("Invalid json: {}", errors));
}
return json;
}
Json::Value JsonValidation::parse_json_file(
const boost::filesystem::path& path) {
boost::filesystem::ifstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
file.open(path, std::ios_base::binary);
} catch (const std::ifstream::failure&) {
ERROR(1, "Could not open json file: `{}`.", path.string());
throw;
}
static const auto reader = Json::CharReaderBuilder();
std::string errors;
Json::Value json;
if (!Json::parseFromStream(reader, file, &json, &errors)) {
throw std::invalid_argument(
fmt::format("File `{}` is not valid json: {}", path.string(), errors));
}
return json;
}
Json::Value JsonValidation::parse_json_file(const std::string& path) {
return parse_json_file(boost::filesystem::path(path));
}
namespace {
Json::StreamWriterBuilder compact_writer_builder() {
Json::StreamWriterBuilder writer;
writer["indentation"] = "";
return writer;
}
Json::StreamWriterBuilder styled_writer_builder() {
Json::StreamWriterBuilder writer;
writer["indentation"] = " ";
return writer;
}
} // namespace
std::unique_ptr<Json::StreamWriter> JsonValidation::compact_writer() {
static const auto writer_builder = compact_writer_builder();
return std::unique_ptr<Json::StreamWriter>(writer_builder.newStreamWriter());
}
std::unique_ptr<Json::StreamWriter> JsonValidation::styled_writer() {
static const auto writer_builder = styled_writer_builder();
return std::unique_ptr<Json::StreamWriter>(writer_builder.newStreamWriter());
}
void JsonValidation::write_json_file(
const boost::filesystem::path& path,
const Json::Value& value) {
boost::filesystem::ofstream file;
file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
file.open(path, std::ios_base::binary);
compact_writer()->write(value, &file);
file << "\n";
file.close();
}
std::string JsonValidation::to_styled_string(const Json::Value& value) {
std::ostringstream string;
styled_writer()->write(value, &string);
return string.str();
}
void JsonValidation::update_object(
Json::Value& left,
const Json::Value& right) {
mt_assert(left.isObject());
mt_assert(right.isObject());
for (const auto& key : right.getMemberNames()) {
left[key] = right[key];
}
}
void JsonValidation::check_unexpected_members(
const Json::Value& value,
const std::unordered_set<std::string>& valid_members) {
validate_object(value);
for (const std::string& member : value.getMemberNames()) {
if (valid_members.find(member) == valid_members.end()) {
throw JsonValidationError(
value,
/* field */ std::nullopt,
/* expected */
fmt::format(
"fields {}, got `{}`",
fmt::join(
boost::adaptors::transform(
valid_members,
[](const std::string& member) {
return fmt::format("`{}`", member);
}),
", "),
member));
}
}
}
} // namespace marianatrench