cpp-oasvalidator
offers a comprehensive API for validating HTTP requests in accordance with OpenAPI specifications. It encompasses various validation categories including Path, Body, Parameters, and Response.
The API functions return a ValidationError
type and accept a reference to a std::string
for populating error messages in case of validation failure. A successful validation returns ValidationError::NONE
. Otherwise, an error code is returned and the error message is populated. The error message follows this JSON schema:
{
"errorCode": "INVALID_BODY",
"details": {
"specRef": "#/paths/%2Fpet/put/requestBody/content/application%2Fjson/schema",
"code": "type",
"description": "The property type 'string' is not on the approved list: 'array'.",
"instance": "#/photoUrls",
"schema": "#/properties/photoUrls"
}
}
cpp-oasvalidator
defines the following ValidationError
enum to categorize various types of validation errors:
enum class validationError
{
NONE = 0,
INVALID_METHOD = -1,
INVALID_ROUTE = -2,
INVALID_PATH_PARAM = -3,
INVALID_QUERY_PARAM = -4,
INVALID_HEADER_PARAM = -5,
INVALID_BODY = -6,
INVALID_RSP = -7
};
errorCode
: Corresponds to theValidationError
enum value.details
: Provides aspecRef
pointing to the exact location in the OpenAPI spec where the request failed. This is URI-encoded to handle special characters.- Additional fields in
details
offer context-specific insights such as the type of error and relevant references.
- Additional fields in
Designed with performance and optimization as priorities, cpp-oasvalidator
performs lazy deserialization and parsing of request components (path, query, header parameters, and body) only when all preceding validations pass.
Four overloaded methods are available to validate different combinations of request components (e.g., with/without body or headers) to accommodate various HTTP methods and use-cases.
The following API reference outlines each function and its sequence of validation checks.
- Constructor
- Validate Route
- Validate Body
- Validate Path Parameters
- Validate Query Parameters
- Validate Header Parameters
- Validate Request
- Validate Request (Overloaded)
- Validate Request (Overloaded)
- Validate Request (Overloaded)
Initializes an OASValidator
object with the OpenAPI specification from the provided file path.
explicit OASValidator(const std::string& oas_specs);
oas_specs
: The file path to the OpenAPI specification or aJSON
string containing the OpenAPI specification.
OASValidator oas_validator("/path/to/openapi/spec.json");
This constructor will throw an ValidatorInitExc
if it fails to initialize with the provided OpenAPI specification.
Ensure that the OpenAPI specification file exists at the provided path and is in a valid JSON
format.
Validates the HTTP method and path/route against the OpenAPI specification, and populates error_msg
with details if validation fails.
- HTTP method
- Route
ValidationError ValidateRoute(
const std::string& method,
const std::string& http_path,
std::string& error_msg
);
method
: The HTTP method (e.g., "GET", "POST") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, whereas other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
std::string error_msg;
ValidationError result = oas_validator.ValidateRoute("GET", "/api/v1/resource", error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the JSON
body of the HTTP request against JSON
schema the OpenAPI specification for the specific route
, and sets error_msg
with a descriptive error message if validation fails.
- HTTP method
- Route
- Body schema
ValidationError ValidateBody(
const std::string& method,
const std::string& http_path,
const std::string& json_body,
std::string& error_msg
);
method
: The HTTP method (e.g., "POST", "PUT") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.json_body
: The JSON body of the HTTP request as astd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_BODY
std::string error_msg;
ValidationError result = oas_validator.ValidateBody("POST", "/api/v1/resource", "{\"key\": \"value\"}", error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the path parameters of the HTTP request against the OpenAPI specification, and populates error_msg
with a descriptive message if validation fails.
- HTTP method
- Route
- Path parameters (in the sequence of provided in OpenAPI spec)
ValidationError ValidatePathParam(
const std::string& method,
const std::string& http_path,
std::string& error_msg
);
method
: The HTTP method (e.g., "GET", "DELETE") as astd::string
.http_path
: The HTTP path with parameters (e.g., "/api/v1/resource/{id}") as astd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, whereas other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_PATH_PARAM
std::string error_msg;
ValidationError result = oas_validator.ValidatePathParam("GET", "/api/v1/resource/123", error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the query parameters of the HTTP request against the OpenAPI specification, and sets error_msg
with a descriptive error message if validation fails.
- HTTP method
- Route
- Query parameters (in the sequence of provided in OpenAPI spec)
ValidationError ValidateQueryParam(
const std::string& method,
const std::string& http_path,
std::string& error_msg
);
method
: The HTTP method (e.g., "GET", "DELETE") as astd::string
.http_path
: The HTTP path including query parameters (e.g., "/api/v1/resource?name=value") as astd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_QUERY_PARAM
std::string error_msg;
ValidationError result = oas_validator.ValidateQueryParam("GET", "/api/v1/resource?name=value", error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the HTTP headers against the OpenAPI specification, and populates error_msg
with a descriptive message if validation fails.
- HTTP method
- Route
- Header parameters
ValidationError ValidateHeaders(
const std::string& method,
const std::string& http_path,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg
);
method
: The HTTP method (e.g., "GET", "POST") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.headers
: The HTTP headers as anstd::unordered_map
ofstd::string
tostd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_HEADER_PARAM
std::unordered_map<std::string, std::string> headers = {{"Authorization", "Bearer token"}};
std::string error_msg;
ValidationError result = oas_validator.ValidateHeaders("GET", "/api/v1/resource", headers, error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the entire HTTP request including method, route, path params, and/or query params against the OpenAPI specification, and sets error_msg
with a descriptive error message if validation fails.
- HTTP method
- Route
- Path parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
- Query parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
ValidationError ValidateRequest(
const std::string& method,
const std::string& http_path,
std::string& error_msg
);
method
: The HTTP method (e.g., "POST", "PUT") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_PATH_PARAM
ValidationError::INVALID_QUERY_PARAM
std::string error_msg;
ValidationError result = oas_validator.ValidateRequest("POST", "/api/v1/resource", error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the entire HTTP request including method, route, JSON body, path/query params (if specified) against the OpenAPI specification, and sets error_msg
with a descriptive error message if validation fails.
- HTTP method
- Route
- Body schema
- Path parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
- Query parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
ValidationError ValidateRequest(
const std::string& method,
const std::string& http_path,
const std::string& json_body,
std::string& error_msg
);
method
: The HTTP method (e.g., "POST", "PUT") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.json_body
: The JSON body of the HTTP request as astd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_BODY
ValidationError::INVALID_PATH_PARAM
ValidationError::INVALID_QUERY_PARAM
std::string error_msg;
ValidationError result = oas_validator.ValidateRequest("POST", "/api/v1/resource", "{\"key\": \"value\"}", error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the entire HTTP request including method, route, path/query params (if specified) and headers against the OpenAPI specification, and sets error_msg
with a descriptive error message if validation fails.
- HTTP method
- Route
- Path parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
- Query parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
- Header parameters
ValidationError ValidateRequest(
const std::string& method,
const std::string& http_path,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg
);
method
: The HTTP method (e.g., "GET", "DELETE") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.headers
: The HTTP headers as anstd::unordered_map
ofstd::string
tostd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_PATH_PARAM
ValidationError::INVALID_QUERY_PARAM
ValidationError::INVALID_HEADER_PARAM
std::unordered_map<std::string, std::string> headers = {{"Authorization", "Bearer token"}};
std::string error_msg;
ValidationError result = oas_validator.ValidateRequest("GET", "/api/v1/resource", headers, error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.
Validates the entire HTTP request including method, route, JSON body, path/query params (if specified) and headers against the OpenAPI specification, and sets error_msg
with a descriptive error message if validation fails.
- HTTP method
- Route
- Body schema
- Path parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
- Query parameters (if specified in specs, in the sequence of provided in OpenAPI spec)
- Header parameters
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
);
method
: The HTTP method (e.g., "POST", "PUT") as astd::string
.http_path
: The HTTP path (e.g., "/api/v1/resource") as astd::string
.json_body
: The JSON body of the HTTP request as astd::string
.headers
: The HTTP headers as anstd::unordered_map
ofstd::string
tostd::string
.error_msg
: Reference to astd::string
where the error message will be stored in case of a validation error.
ValidationError
: An enum indicating the result of the validation. A return value ofValidationError::NONE
signifies successful validation, while other values indicate specific types of validation failures.- Possible values:
ValidationError::NONE
ValidationError::INVALID_METHOD
ValidationError::INVALID_ROUTE
ValidationError::INVALID_BODY
ValidationError::INVALID_PATH_PARAM
ValidationError::INVALID_QUERY_PARAM
ValidationError::INVALID_HEADER_PARAM
std::unordered_map<std::string, std::string> headers = {{"Authorization", "Bearer token"}};
std::string error_msg;
ValidationError result = oas_validator.ValidateRequest("POST", "/api/v1/resource", "{\"key\": \"value\"}", headers, error_msg);
if (result != ValidationError::NONE) {
std::cerr << "Validation failed: " << error_msg << std::endl;
}
- The
error_msg
argument will be populated with aJSON
string in case of a validation error.