Skip to content

Repository files navigation

Name

lua-resty-openapi-validator - Pure Lua OpenAPI request validator for OpenResty / LuaJIT.

CILicense

Table of Contents

Description

Validates HTTP requests against OpenAPI 3.0 and 3.1 specifications using lua-resty-radixtree for path matching and api7/jsonschema for schema validation. Pure Lua, no external processes required.

Install

Dependencies

install by luarocks

luarocks install lua-resty-openapi-validator

install by source

git clone https://github.com/api7/lua-resty-openapi-validator.git
cd lua-resty-openapi-validator
make dev
sudo make install

Back to TOC

Quick Start

localov=require("resty.openapi_validator")
-- compile once (cache the result)localvalidator, err=ov.compile(spec_json_string, {
strict=true, -- error on unsupported 3.1 keywords (default: true)
})
ifnotvalidatorthenngx.log(ngx.ERR, "spec compile error: ", err)
returnend-- validate per-requestlocalok, err=validator:validate_request({
method=ngx.req.get_method(),
path=ngx.var.uri,
query=ngx.req.get_uri_args(),
headers=ngx.req.get_headers(0, true),
body=ngx.req.get_body_data(),
content_type=ngx.var.content_type,
})
ifnotokthenngx.status=400ngx.say(err)
returnend

See API documentation for details on all methods and options.

Back to TOC

Validation Scope

FeatureStatus
Path parameter matching & validation
Query parameter validation (with type coercion)
Header validation
Request body validation (JSON)
Request body validation (form-urlencoded)
style / explode parameter serialization
$ref resolution (document-internal)
Circular $ref support
allOf / oneOf / anyOf composition
additionalProperties
OpenAPI 3.0 nullable
OpenAPI 3.1 type arrays (["string", "null"])
readOnly / writeOnly validation
Response validation❌ (not planned for v1)
Security scheme validation
External $ref (URLs, files)
multipart/form-data body⚠️ basic support

Back to TOC

OpenAPI 3.1 Support

OpenAPI 3.1 uses JSON Schema Draft 2020-12. Since the underlying jsonschema library supports up to Draft 7, schemas are normalized at compile time:

3.1 / 2020-12 FeatureNormalization
prefixItemsitems (tuple form)
$defsdefinitions
dependentRequired / dependentSchemasdependencies
type: ["string", "null"]Passed through (Draft 7 compatible)
$ref with sibling keywordsallOf: [resolved, {siblings}]
$dynamicRef, unevaluatedPropertiesError (strict) / Warning (lenient)

Back to TOC

Testing

make test

Runs unit tests and conformance tests covering OpenAPI 3.0 / 3.1 features (parameter serialization, request body validation, discriminator, $ref, readOnly/writeOnly, deepObject/form/multipart, etc.).

Back to TOC

License

Apache 2.0

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages