Class OASValidator

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, const std::unordered_map<std::string, std::unordered_set<std::string>> &method_map = {})

Constructor that takes the path to the OAS specification file and an optional method mapping.

For example:

std::unordered_map<std::string, std::unordered_set<std::string>> method_map = {
    {"OPTIONS", {"GET", "POST", "PUT", "DELETE", "HEAD", "PATCH"}},
    {"HEAD", {"GET"}} // Treat HEAD request as GET
};
OASValidator validator(oas_specs, method_map);

Note

The OAS specification can be provided as a file path or as a JSON string. If the method map is provided, it allows certain HTTP methods to be treated as others. For instance, with the mapping {“HEAD”, {“GET”}}, a HEAD request can be validated as the GET request, if HEAD method is not defined.

Parameters:
  • oas_specs – File path to the OAS specification in JSON format or JSON string containing the OAS specification.

  • method_map – An optional unordered_map where each key is an HTTP method and the value is an unordered_set of methods that can be treated as the key method. This allows certain HTTP methods to be treated as others.

OASValidator(const OASValidator &other)

Copy constructor.

Parameters:

other – The OASValidator object to be copied.

OASValidator &operator=(const OASValidator &other)

Copy assignment operator.

Parameters:

other – The OASValidator object to be copied.

Returns:

Reference to the copied OASValidator object.

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:

  1. HTTP method

  2. 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 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:

  1. HTTP method

  2. Route

  3. 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 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:

  1. HTTP method

  2. Route

  3. 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 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:

  1. HTTP method

  2. Route

  3. 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 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:

  1. HTTP method

  2. Route

  3. 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 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:

  1. HTTP method

  2. Route

  3. Path parameters (if specified in specs)

  4. 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 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:

  1. HTTP method

  2. Route

  3. Body schema

  4. Path parameters (if specified in specs)

  5. 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 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:

  1. HTTP method

  2. Route

  3. Path parameters (if specified in specs)

  4. Query parameters (if specified in specs)

  5. 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 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:

  1. HTTP method

  2. Route

  3. Body schema

  4. Path parameters (if specified in specs)

  5. Query parameters (if specified in specs)

  6. 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:

~OASValidator()