nawaz1991 / lua-oasvalidator

A FAST Lua module to validate the HTTP requests against the OpenAPI specifications of the REST server.
MIT License
6 stars 0 forks source link
api json-schema-validator json-validation json-validator kong lua oas openapi openapi-validation openapi-validator openresty parameter-validation rest rest-validators restful swagger validation validator

lua-oasvalidator: REST Request Validator

Language Lua Github releases Mac build Linux build License API Docs

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.

🌟 Key Features

πŸ“œ Validation Sequence

lua-oasvalidator validates HTTP requests in the following order:

  1. HTTP Method Validation: Ensures that the HTTP method (GET, POST, PUT, etc.) aligns with the OpenAPI spec.
  2. Route Validation: Checks if the provided route matches the specification.
  3. Body Validation: Validates the JSON body structure and data against the OpenAPI spec.
  4. Path Parameter Validation: Validates path parameters.
  5. Query Parameter Validation: Ensures query parameters are consistent with the OpenAPI spec.
  6. Header Parameter Validation: Confirms headers are in line with the OpenAPI specification.
  7. Request Validation: Validates the whole HTTP request starting from method, route, body (if provided), path/query params (if specified in specs) and/or headers. To address all variations, four overloaded methods are provided.

For a comprehensive understanding, refer to API Reference.

πŸ›  Parameter Styles, data types & Deserialization

cpp-oasvalidator can deserialize and parse parameters of all data types serialized in various styles provided by Swagger/OpenAPI. Following tables provide the details.

Path Parameters

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

Query Parameters

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

Header Parameters

Style Explode Primitive String Array of primitives Array of strings Object
simple* false* βœ… βœ… βœ… βœ… βœ…
simple true βœ… βœ… βœ… βœ… βœ…

* Default serialization method

🚫 Error Handling

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"
  }
}

πŸš€ Getting Started

πŸ”§ Installation

Prerequisites:

Building and Installing:

To build and install lua-oasvalidator, follow the steps below:

  1. Clone and initialize the git submodules
  2. Navigate to the root directory of the lua-oasvalidator
  3. Run luarocks make to build and install
    git clone --recurse-submodules https://github.com/nawaz1991/lua-oasvalidator.git
    cd lua-oasvalidator
    sudo luarocks make

🎬 Initialization

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.

πŸ“œ Conclusion

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.

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for the full license text.

Β© 2024 Muhammad Nawaz. All Rights Reserved.