Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2024 Muhammad Nawaz |
3 |
|
|
* Licensed under the MIT License. See LICENSE file for more information. |
4 |
|
|
*/ |
5 |
|
|
// [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ] |
6 |
|
|
|
7 |
|
|
#ifndef OAS_VALIDATORS_HPP |
8 |
|
|
#define OAS_VALIDATORS_HPP |
9 |
|
|
|
10 |
|
|
#include "utils/common.hpp" |
11 |
|
|
#include "utils/path_trie.hpp" |
12 |
|
|
#include "validators/body_validator.hpp" |
13 |
|
|
#include "validators/param_validators.hpp" |
14 |
|
|
|
15 |
|
|
#include <utility> |
16 |
|
|
#include <vector> |
17 |
|
|
class ValidatorsStore |
18 |
|
|
{ |
19 |
|
|
public: |
20 |
|
402 |
ValidatorsStore() = default; |
21 |
|
|
explicit ValidatorsStore(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys); |
22 |
|
|
ValidatorsStore(const ValidatorsStore&) = delete; |
23 |
|
|
ValidatorsStore& operator=(const ValidatorsStore&) = delete; |
24 |
|
|
void AddParamValidators(const std::string& path, const rapidjson::Value& params, |
25 |
|
|
std::vector<std::string>& ref_keys); |
26 |
|
|
ValidationError ValidateBody(const std::string& json_body, std::string& error_msg); |
27 |
|
|
ValidationError ValidatePathParams(std::unordered_map<size_t, ParamRange>& param_idxs, std::string& error_msg); |
28 |
|
|
ValidationError ValidateQueryParams(const std::string& query, std::string& error_msg); |
29 |
|
|
ValidationError ValidateHeaderParams(const std::unordered_map<std::string, std::string>& headers, |
30 |
|
|
std::string& error_msg); |
31 |
|
|
~ValidatorsStore(); |
32 |
|
|
|
33 |
|
|
private: |
34 |
|
|
struct PathParamValidatorInfo |
35 |
|
|
{ |
36 |
|
210 |
PathParamValidatorInfo(size_t idx, PathParamValidator* validator) |
37 |
|
210 |
: idx(idx) |
38 |
|
210 |
, validator(validator) |
39 |
|
|
{ |
40 |
|
210 |
} |
41 |
|
|
|
42 |
|
|
PathParamValidatorInfo(const PathParamValidatorInfo& other) = default; |
43 |
|
|
|
44 |
|
|
PathParamValidatorInfo& operator=(const PathParamValidatorInfo& other) |
45 |
|
|
{ |
46 |
|
|
if (this == &other) { |
47 |
|
|
return *this; |
48 |
|
|
} |
49 |
|
|
idx = other.idx; |
50 |
|
|
validator = other.validator; |
51 |
|
|
return *this; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
size_t idx = std::string::npos; |
55 |
|
|
PathParamValidator* validator = nullptr; |
56 |
|
|
}; |
57 |
|
|
|
58 |
|
|
struct QueryParamValidatorInfo |
59 |
|
|
{ |
60 |
|
258 |
QueryParamValidatorInfo(std::string name, QueryParamValidator* validator) |
61 |
|
258 |
: name(std::move(name)) |
62 |
|
258 |
, validator(validator) |
63 |
|
|
{ |
64 |
|
258 |
} |
65 |
|
|
|
66 |
|
396 |
QueryParamValidatorInfo(const QueryParamValidatorInfo& other) = default; |
67 |
|
|
|
68 |
|
|
QueryParamValidatorInfo& operator=(const QueryParamValidatorInfo& other) |
69 |
|
|
{ |
70 |
|
|
if (this == &other) { |
71 |
|
|
return *this; |
72 |
|
|
} |
73 |
|
|
name = other.name; |
74 |
|
|
validator = other.validator; |
75 |
|
|
return *this; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
std::string name{}; |
79 |
|
|
QueryParamValidator* validator = nullptr; |
80 |
|
|
|
81 |
|
654 |
~QueryParamValidatorInfo() |
82 |
|
|
{ |
83 |
|
654 |
} |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
BodyValidator* body_validator_ = nullptr; |
87 |
|
|
std::vector<PathParamValidatorInfo> path_param_validators_{}; |
88 |
|
|
std::vector<QueryParamValidatorInfo> query_param_validators_{}; |
89 |
|
|
std::unordered_map<std::string, HeaderParamValidator*> header_param_validators_{}; |
90 |
|
|
|
91 |
|
|
static std::unordered_map<std::string, size_t> GetPathParamIndices(const std::string& path); |
92 |
|
|
}; |
93 |
|
|
|
94 |
|
|
#endif // OAS_VALIDATORS_HPP |
95 |
|
|
|