Class OASValidator#
Defined in File oas_validator.hpp
Class Documentation#
-
class OASValidator#
Class that provides API for HTTP requests validation against OAS validation.
The OASValidator class offers various methods for validating REST requests against a defined OAS (OpenAPI Specification) file.
Public Functions
-
explicit OASValidator(const std::string &oas_specs)#
Constructor that takes the path to the OAS specification file.
Note
The OAS specification can be provided as a file path or as a JSON string.
- Parameters
oas_specs – File path to the OAS specification in JSON format or JSON string containing the OAS specification.
-
ValidationError ValidateRoute(const std::string &method, const std::string &http_path, std::string &error_msg)#
Validates the HTTP method and route against the OpenAPI specification.
This function performs the following validation sequence:
HTTP method
Route
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “GET”, “POST”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
-
ValidationError ValidateBody(const std::string &method, const std::string &http_path, const std::string &json_body, std::string &error_msg)#
Validates the JSON body of the HTTP request against the OpenAPI specification.
This function performs the following validation sequence:
HTTP method
Route
Body schema
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “POST”, “PUT”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
json_body – The JSON body of the HTTP request as a std::string.
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_BODY: Invalid request body.
-
ValidationError ValidatePathParam(const std::string &method, const std::string &http_path, std::string &error_msg)#
Validates the path parameters of the HTTP request against the OpenAPI specification.
This function performs the following validation sequence:
HTTP method
Route
Path parameters (in the sequence provided in the OpenAPI spec)
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “GET”, “DELETE”).
http_path – The HTTP path with parameters as a std::string (e.g., “/api/v1/resource/{id}”).
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_PATH_PARAM: Invalid path parameter.
-
ValidationError ValidateQueryParam(const std::string &method, const std::string &http_path, std::string &error_msg)#
Validates the query parameters of the HTTP request against the OpenAPI specification.
This function performs the following validation sequence:
HTTP method
Route
Query parameters (in the sequence provided in the OpenAPI spec)
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “GET”, “DELETE”).
http_path – The HTTP path including query parameters as a std::string (e.g., “/api/v1/resource?name=value”).
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_QUERY_PARAM: Invalid query parameter.
-
ValidationError ValidateHeaders(const std::string &method, const std::string &http_path, const std::unordered_map<std::string, std::string> &headers, std::string &error_msg)#
Validates the HTTP headers of the request against the OpenAPI specification.
This function performs the following validation sequence:
HTTP method
Route
Header parameters
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “GET”, “POST”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
headers – The HTTP headers as an std::unordered_map from std::string to std::string.
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible return values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_HEADER_PARAM: Invalid header parameter.
-
ValidationError ValidateRequest(const std::string &method, const std::string &http_path, std::string &error_msg)#
Validates the entire HTTP request against the OpenAPI specification.
This function performs a comprehensive validation of the entire HTTP request based on the following sequence:
HTTP method
Route
Path parameters (if specified in specs)
Query parameters (if specified in specs)
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “POST”, “PUT”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible return values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_PATH_PARAM: Invalid path parameter.
ValidationError::INVALID_QUERY_PARAM: Invalid query parameter.
-
ValidationError ValidateRequest(const std::string &method, const std::string &http_path, const std::string &json_body, std::string &error_msg)#
Validates the entire HTTP request including JSON body against the OpenAPI specification.
This overloaded function performs a comprehensive validation of the entire HTTP request, including the JSON body, based on the following sequence:
HTTP method
Route
Body schema
Path parameters (if specified in specs)
Query parameters (if specified in specs)
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “POST”, “PUT”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
json_body – The JSON body of the HTTP request as a std::string.
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible return values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_BODY: Invalid JSON body.
ValidationError::INVALID_PATH_PARAM: Invalid path parameter.
ValidationError::INVALID_QUERY_PARAM: Invalid query parameter.
-
ValidationError ValidateRequest(const std::string &method, const std::string &http_path, const std::unordered_map<std::string, std::string> &headers, std::string &error_msg)#
Validates the entire HTTP request, including headers, against the OpenAPI specification.
This overloaded function performs a comprehensive validation of the entire HTTP request, including HTTP headers, based on the following sequence:
HTTP method
Route
Path parameters (if specified in specs)
Query parameters (if specified in specs)
Header parameters
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “GET”, “DELETE”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
headers – The HTTP headers as an std::unordered_map of std::string to std::string.
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible return values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_PATH_PARAM: Invalid path parameter.
ValidationError::INVALID_QUERY_PARAM: Invalid query parameter.
ValidationError::INVALID_HEADER_PARAM: Invalid header parameter.
-
ValidationError ValidateRequest(const std::string &method, const std::string &http_path, const std::string &json_body, const std::unordered_map<std::string, std::string> &headers, std::string &error_msg)#
Validates the entire HTTP request, including JSON body and headers, against the OpenAPI specification.
This overloaded function performs a comprehensive validation of the entire HTTP request, including the JSON body and HTTP headers, based on the following sequence:
HTTP method
Route
Body schema
Path parameters (if specified in specs)
Query parameters (if specified in specs)
Header parameters
Note
The error_msg argument will be populated with a JSON string in case of a validation error.
- Parameters
method – The HTTP method as a std::string (e.g., “POST”, “PUT”).
http_path – The HTTP path as a std::string (e.g., “/api/v1/resource”).
json_body – The JSON body of the HTTP request as a std::string.
headers – The HTTP headers as an std::unordered_map of std::string to std::string.
error_msg – Reference to a std::string where the error message will be stored in case of a validation error.
- Returns
ValidationError enum indicating the result of the validation. Possible return values include:
ValidationError::NONE: No validation error.
ValidationError::INVALID_METHOD: Invalid HTTP method.
ValidationError::INVALID_ROUTE: Invalid route.
ValidationError::INVALID_BODY: Invalid JSON body.
ValidationError::INVALID_PATH_PARAM: Invalid path parameter.
ValidationError::INVALID_QUERY_PARAM: Invalid query parameter.
ValidationError::INVALID_HEADER_PARAM: Invalid header parameter.
-
~OASValidator()#
-
explicit OASValidator(const std::string &oas_specs)#