-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: adopt lua-resty-etcd code style conventions #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d0b650
refactor: adopt lua-resty-etcd code style conventions
jarvis9443 31d0682
fix: address review feedback
jarvis9443 bf34ebd
fix: add lint to CI, rename readOnly/writeOnly to snake_case
jarvis9443 aa7be90
fix: update test description to match renamed skip.read_only key
jarvis9443 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| std = "ngx_lua" | ||
| ignore = { | ||
| "542", -- empty if branch | ||
| } | ||
| redefined = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,51 @@ | ||
| .PHONY: test test-unit test-conformance benchmark lint clean | ||
| INST_PREFIX ?= /usr/local/openresty | ||
| INST_LUADIR ?= $(INST_PREFIX)/lualib | ||
| INSTALL ?= install | ||
|
|
||
| RESTY := /usr/local/openresty/bin/resty --shdict "test 1m" | ||
|
|
||
| UNIT_TESTS := $(sort $(wildcard t/unit/test_*.lua)) | ||
| CONFORMANCE_TESTS := $(sort $(wildcard t/conformance/test_*.lua)) | ||
|
|
||
| .PHONY: test test-unit test-conformance benchmark lint dev install clean help | ||
|
|
||
| ### help: Show Makefile rules | ||
| help: | ||
| @echo Makefile rules: | ||
| @echo | ||
| @grep -E '^### [-A-Za-z0-9_]+:' Makefile | sed 's/###/ /' | ||
|
|
||
| ### dev: Create a development ENV | ||
| dev: | ||
| luarocks install rockspec/lua-resty-openapi-validator-master-0.1-0.rockspec --only-deps --local | ||
|
|
||
| ### install: Install the library to runtime | ||
| install: | ||
| $(INSTALL) -d $(INST_LUADIR)/resty/openapi_validator/ | ||
| $(INSTALL) lib/resty/openapi_validator/*.lua $(INST_LUADIR)/resty/openapi_validator/ | ||
|
|
||
| ### test: Run all tests | ||
| test: test-unit test-conformance | ||
| @echo "All tests passed." | ||
|
|
||
| ### test-unit: Run unit tests | ||
| test-unit: | ||
| @echo "=== Unit tests ===" | ||
| @for f in $(UNIT_TESTS); do $(RESTY) -e "dofile('$$f')" || exit 1; done | ||
|
|
||
| ### test-conformance: Run conformance tests | ||
| test-conformance: | ||
| @echo "=== Conformance tests ===" | ||
| @for f in $(CONFORMANCE_TESTS); do $(RESTY) -e "dofile('$$f')" || exit 1; done | ||
|
|
||
| ### benchmark: Run microbenchmark | ||
| benchmark: | ||
| @$(RESTY) -e 'dofile("benchmark/bench.lua")' | ||
|
|
||
| ### lint: Lint Lua source code | ||
| lint: | ||
| @luacheck lib/ --std ngx_lua | ||
| luacheck -q lib/ | ||
|
|
||
| ### clean: Remove build artifacts | ||
| clean: | ||
| @rm -rf *.rock benchmark/logs/ benchmark/nginx.conf | ||
| rm -rf *.rock benchmark/logs/ benchmark/nginx.conf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| API | ||
| === | ||
|
|
||
| Table of Contents | ||
| ================= | ||
|
|
||
| * [compile](#compile) | ||
| * [validate_request](#validate_request) | ||
|
|
||
| compile | ||
| ------- | ||
|
|
||
| `syntax: validator, err = ov.compile(spec_str, opts)` | ||
|
|
||
| Compiles an OpenAPI specification JSON string into a reusable validator object. | ||
| The spec is parsed, `$ref` pointers are resolved, and schemas are normalized to | ||
| JSON Schema Draft 7. The returned validator should be cached and reused across | ||
| requests. | ||
|
|
||
| - `spec_str`: string — raw JSON of an OpenAPI 3.0 or 3.1 specification. | ||
| - `opts`: table (optional) — compilation options: | ||
| - `strict`: boolean (default `true`) — if `true`, returns an error when | ||
| unsupported OpenAPI 3.1 keywords are encountered (`$dynamicRef`, | ||
| `unevaluatedProperties`, etc.); if `false`, these keywords are silently | ||
| dropped with a warning. | ||
|
|
||
| Returns a validator object on success, or `nil` and an error string on failure. | ||
|
|
||
| ```lua | ||
| local ov = require("resty.openapi_validator") | ||
|
|
||
| local validator, err = ov.compile(spec_json, { strict = true }) | ||
| if not validator then | ||
| ngx.log(ngx.ERR, "compile: ", err) | ||
| return | ||
| end | ||
| ``` | ||
|
|
||
| [Back to TOC](#table-of-contents) | ||
|
|
||
| validate_request | ||
| ---------------- | ||
|
|
||
| `syntax: ok, err = validator:validate_request(req, skip)` | ||
|
|
||
| Validates an incoming HTTP request against the compiled OpenAPI spec. Returns | ||
| `true` on success, or `false` and a formatted error string on failure. | ||
|
|
||
| - `req`: table — request data with the following fields: | ||
| - `method`: string (required) — HTTP method (e.g. `"GET"`, `"POST"`) | ||
| - `path`: string (required) — request URI path (e.g. `"/users/123"`) | ||
| - `query`: table (optional) — query parameters `{ name = value | {values} }` | ||
| - `headers`: table (optional) — request headers `{ name = value }` | ||
| - `body`: string (optional) — raw request body | ||
| - `content_type`: string (optional) — Content-Type header value | ||
|
|
||
| - `skip`: table (optional) — selectively skip validation steps: | ||
| - `path`: boolean — skip path parameter validation | ||
| - `query`: boolean — skip query parameter validation | ||
| - `header`: boolean — skip header validation | ||
| - `body`: boolean — skip request body validation | ||
| - `read_only`: boolean — skip readOnly property checks in request body | ||
| - `write_only`: boolean — skip writeOnly property checks | ||
|
|
||
| ```lua | ||
| local ok, 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, | ||
| }) | ||
|
|
||
| if not ok then | ||
| ngx.status = 400 | ||
| ngx.say(err) | ||
| return | ||
| end | ||
| ``` | ||
|
|
||
| Skip specific validation: | ||
|
|
||
| ```lua | ||
| local ok, err = validator:validate_request(req, { | ||
| body = true, -- skip body validation | ||
| read_only = true, -- skip readOnly checks | ||
| }) | ||
| ``` | ||
|
|
||
| [Back to TOC](#table-of-contents) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.