Giter Site home page Giter Site logo

nawaz1991 / lua-oasvalidator Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 27 KB

A FAST Lua module to validate the HTTP requests against the OpenAPI specifications of the REST server.

License: MIT License

CMake 31.08% C++ 32.06% Lua 36.86%
api json-schema-validator json-validation json-validator kong lua oas openapi openapi-validation openapi-validator

lua-oasvalidator's Introduction

lua-oasvalidator: REST Request Validator

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

  • Lua Integration: Seamless integration with Lua, allowing you to use the capabilities of cpp-oasvalidator within API gateways like Kong/NGINX.
  • Efficient, Sequential Validation: Validates requests in a logical order, starting from the HTTP method down to the header parameters. This means if you validate a later stage, preceding steps are validated as well.
  • In-Depth Error Reports: Returns an insightful error enumeration coupled with an extensive error message in JSON format to pinpoint inaccuracies.
  • Optimized Performance: Utilizes lazy deserialization, only processing content when all prior checks pass.
  • Broad Parameter Support: Deserializes parameters across a spectrum of styles and data types, ensuring a wide range of OpenAPI configurations are supported.

๐Ÿ“œ 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:

  • lua development environment
    • lua-devel (linux)
    • or luavm(windows)
    • or brew install lua luarocks
    • or any equivalent on your system
  • cmake >= 3.1.0, cmake 2.8 may work but not well tested.
  • A C++11 compatible compiler.

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.

ยฉ 2023 Muhammad Nawaz. All Rights Reserved.

lua-oasvalidator's People

Contributors

nawaz1991 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

lua-oasvalidator's Issues

[Feature] Add github actions to build

Is your feature request related to a problem? Please describe.
No

Describe the solution you'd like
N/A

Describe alternatives you've considered
N/A

Additional context
Github action should be added to build.

How to package lua-oasvalidator with kong gateway

Hi @nawaz1991 We are trying to use Kong open source gateway and trying to create oas validator custom plugin in kong and came across your oas validator written in c++.

Can you please let me know the process of packaging c++ and lua code in the kong image together.

Thank in Advance!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.