@@ -31,7 +31,7 @@ static const std::string kRequestFieldParameters{"params"};
31
31
static const std::string kRequestRequiredFields {
32
32
kRequestFieldJsonRpc + " ," + kRequestFieldId + " ," + kRequestFieldMethod + " ," + kRequestFieldParameters };
33
33
34
- void JsonRpcValidator ::load_specification () {
34
+ void Validator ::load_specification () {
35
35
const auto spec = nlohmann::json::parse (specification_json, nullptr , /* allow_exceptions=*/ false );
36
36
if (spec.contains (" methods" )) {
37
37
for (const auto & method : spec[" methods" ]) {
@@ -43,15 +43,15 @@ void JsonRpcValidator::load_specification() {
43
43
}
44
44
}
45
45
46
- JsonRpcValidationResult JsonRpcValidator ::validate (const nlohmann::json& request) {
46
+ ValidationResult Validator ::validate (const nlohmann::json& request) {
47
47
if (auto valid_result{check_request_fields (request)}; !valid_result) {
48
48
return valid_result;
49
49
}
50
50
51
51
return validate_params (request);
52
52
}
53
53
54
- JsonRpcValidationResult JsonRpcValidator ::check_request_fields (const nlohmann::json& request) {
54
+ ValidationResult Validator ::check_request_fields (const nlohmann::json& request) {
55
55
// Expected fields: jsonrpc, id, method, params (optional)
56
56
auto required_fields = 0b111 ;
57
57
@@ -87,7 +87,7 @@ JsonRpcValidationResult JsonRpcValidator::check_request_fields(const nlohmann::j
87
87
return {};
88
88
}
89
89
90
- JsonRpcValidationResult JsonRpcValidator ::validate_params (const nlohmann::json& request) {
90
+ ValidationResult Validator ::validate_params (const nlohmann::json& request) {
91
91
const auto & method = request.find (kRequestFieldMethod ).value ().get <std::string>();
92
92
const auto & params_field = request.find (kRequestFieldParameters );
93
93
const auto & params = params_field != request.end () ? params_field.value () : nlohmann::json::array ();
@@ -128,7 +128,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_params(const nlohmann::json&
128
128
return {};
129
129
}
130
130
131
- JsonRpcValidationResult JsonRpcValidator ::validate_schema (const nlohmann::json& value, const nlohmann::json& schema) {
131
+ ValidationResult Validator ::validate_schema (const nlohmann::json& value, const nlohmann::json& schema) {
132
132
if (schema.contains (" type" )) {
133
133
if (auto result{validate_type (value, schema)}; !result) {
134
134
return result;
@@ -140,7 +140,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_schema(const nlohmann::json&
140
140
schema_of_collection = schema.find (" oneOf" );
141
141
}
142
142
143
- JsonRpcValidationResult result;
143
+ ValidationResult result;
144
144
if (schema_of_collection != schema.end ()) {
145
145
for (const auto & schema_of : schema_of_collection.value ()) {
146
146
result = validate_type (value, schema_of);
@@ -152,7 +152,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_schema(const nlohmann::json&
152
152
return result;
153
153
}
154
154
155
- JsonRpcValidationResult JsonRpcValidator ::validate_type (const nlohmann::json& value, const nlohmann::json& schema) {
155
+ ValidationResult Validator ::validate_type (const nlohmann::json& value, const nlohmann::json& schema) {
156
156
const auto & schema_type = schema[" type" ].get <std::string>();
157
157
158
158
if (schema_type == " string" ) {
@@ -172,7 +172,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_type(const nlohmann::json& va
172
172
}
173
173
}
174
174
175
- JsonRpcValidationResult JsonRpcValidator ::validate_string (const nlohmann::json& string, const nlohmann::json& schema) {
175
+ ValidationResult Validator ::validate_string (const nlohmann::json& string, const nlohmann::json& schema) {
176
176
if (!string.is_string ()) {
177
177
return tl::make_unexpected (" Invalid string: " + string.dump ());
178
178
}
@@ -213,12 +213,12 @@ JsonRpcValidationResult JsonRpcValidator::validate_string(const nlohmann::json&
213
213
return {};
214
214
}
215
215
216
- JsonRpcValidationResult JsonRpcValidator ::validate_array (const nlohmann::json& array, const nlohmann::json& schema) {
216
+ ValidationResult Validator ::validate_array (const nlohmann::json& array, const nlohmann::json& schema) {
217
217
if (!array.is_array () && !array.is_null () && array.empty ()) {
218
218
return tl::make_unexpected (" Invalid array: " + array.dump ());
219
219
}
220
220
221
- JsonRpcValidationResult result;
221
+ ValidationResult result;
222
222
const auto & schema_items = schema[" items" ];
223
223
for (const auto & item : array) {
224
224
result = validate_schema (item, schema_items);
@@ -230,7 +230,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_array(const nlohmann::json& a
230
230
return result;
231
231
}
232
232
233
- JsonRpcValidationResult JsonRpcValidator ::validate_object (const nlohmann::json& object, const nlohmann::json& schema) {
233
+ ValidationResult Validator ::validate_object (const nlohmann::json& object, const nlohmann::json& schema) {
234
234
if (!object.is_object ()) {
235
235
return tl::make_unexpected (" Invalid object: " + object.dump ());
236
236
}
@@ -262,21 +262,21 @@ JsonRpcValidationResult JsonRpcValidator::validate_object(const nlohmann::json&
262
262
return {};
263
263
}
264
264
265
- JsonRpcValidationResult JsonRpcValidator ::validate_boolean (const nlohmann::json& boolean) {
265
+ ValidationResult Validator ::validate_boolean (const nlohmann::json& boolean) {
266
266
if (!boolean.is_boolean ()) {
267
267
return tl::make_unexpected (" Invalid boolean: " + boolean.dump ());
268
268
}
269
269
return {};
270
270
}
271
271
272
- JsonRpcValidationResult JsonRpcValidator ::validate_number (const nlohmann::json& number) {
272
+ ValidationResult Validator ::validate_number (const nlohmann::json& number) {
273
273
if (!number.is_number ()) {
274
274
return tl::make_unexpected (" Invalid number: " + number.dump ());
275
275
}
276
276
return {};
277
277
}
278
278
279
- JsonRpcValidationResult JsonRpcValidator ::validate_null (const nlohmann::json& value) {
279
+ ValidationResult Validator ::validate_null (const nlohmann::json& value) {
280
280
if (value.is_null ()) {
281
281
return {};
282
282
}
0 commit comments