lua-oasvalidator
is a Lua binding for the cpp-oasvalidator library, designed for the validation of HTTP requests against OpenAPI 3.x specifications. By integrating this module with your Lua-based REST servers or API gateways, you can ensure that only compliant requests interact with your backend services.
With support for OpenAPI 3.x, this module streamlines the process of validating various components of an HTTP request, from methods and routes to detailed parameters and JSON
body.
cpp-oasvalidator
within API gateways like Kong/NGINX.lua-oasvalidator
validates HTTP requests in the following order:
For a comprehensive understanding, refer to API Reference.
cpp-oasvalidator
can deserialize and parse parameters of all data types serialized in various styles provided by Swagger/OpenAPI. Following tables provide the details.
Style | Explode | Primitive | String | Array of primitives | Array of strings | Object |
---|---|---|---|---|---|---|
simple* | false* | β | β | β | β | β |
simple | true | β | β | β | β | β |
label | false | β | β | β | β | β |
label | true | β | β | β | β | β |
matrix | false | β | β | β | β | β |
matrix | true | β | β | β | β | β |
* Default serialization method
Style | Explode | Primitive | String | Array of primitives | Array of strings | Object |
---|---|---|---|---|---|---|
form* | true* | β | β | β | β | β |
form | false | β | β | β | β | β |
spaceDelimited | true | N/A | N/A | β | β | N/A |
spaceDelimited | false | N/A | N/A | β | β | N/A |
pipeDelimited | true | N/A | N/A | β | β | N/A |
pipeDelimited | false | N/A | N/A | β | β | N/A |
deepObject | false | N/A | N/A | N/A | N/A | β |
* Default serialization method
Style | Explode | Primitive | String | Array of primitives | Array of strings | Object |
---|---|---|---|---|---|---|
simple* | false* | β | β | β | β | β |
simple | true | β | β | β | β | β |
* Default serialization method
lua-oasvalidator
returns a specific ERROR_CODE
value, indicating the error type:
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
An accompanying detailed error message, structured in JSON, elucidates the error:
{
"errorCode": "INVALID_BODY",
"detail": {
"specRef": "#/paths/%2Fpet/put/requestBody/content/application%2Fjson/schema",
"code": "type",
"description": "Property has a type 'string' that is not in the following list: 'array'.",
"instance": "#/photoUrls",
"schema": "#/properties/photoUrls"
}
}
Prerequisites:
lua-devel
(linux)brew install lua luarocks
cmake
>= 3.1.0
, cmake 2.8 may work but not well tested.Building and Installing:
To build and install lua-oasvalidator
, follow the steps below:
lua-oasvalidator
luarocks make
to build and install
git clone --recurse-submodules https://github.com/nawaz1991/lua-oasvalidator.git
cd lua-oasvalidator
sudo luarocks make
To utilize lua-oasvalidator
, include the module and initialize the validator with your OpenAPI specification:
local oasvalidator = require("oasvalidator")
print(oasvalidator._VERSION)
-- Initialize and get validators
local validators = oasvalidator.GetValidators("/path/to/your/spec.json")
err_code, err_msg = validators:ValidateRoute("PUT", "/pet")
if err_code ~= 0 then
print(err_msg)
end
For a detailed breakdown of each API, refer to the API Reference.
lua-oasvalidator
bridges the power of cpp-oasvalidator
to the Lua based requirements like to write the plugins for Kong and NGINX etc. With its organized validation order, expansive parameter style support, and meticulous error reporting, it ensures your Lua-based services stay compliant with your OpenAPI specs.
This project is licensed under the MIT License. See the LICENSE file for the full license text.
Β© 2024 Muhammad Nawaz. All Rights Reserved.