Skip to content
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

Add a filter for request validation #249

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Non-boolean options:
|prefix| String | supported | supported | Mounts the middleware to respond at a configured prefix. (e.g. prefix is '/v1' and request path is '/v1/test' use '/test' definition). |
|schema_path| String | supported | supported | Defines the location of the schema file to use for validation. |
|error_handler| Proc Object | supported | supported | A proc which will be called when error occurs. Take an Error instance as first argument, and request.env as second argument. (e.g. `-> (ex, env) { Raven.capture_exception(ex, extra: { rack_env: env }) }`) |
|accept_request_filter | Proc Object | supported | supported | A proc that accepts a Request and returns a boolean. It indicates whether to validate the current request, or not. (e.g. `-> (request) { request.path.start_with?('/something') }`) |

Note that Hyper-Schema and OpenAPI 2 get the same defaults for options.

Expand Down
3 changes: 2 additions & 1 deletion lib/committee/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ def initialize(app, options={})
@schema = self.class.get_schema(options)

@router = @schema.build_router(options)
@accept_request_filter = options[:accept_request_filter] || -> (_) { true }
end

def call(env)
request = Rack::Request.new(env)

if @router.includes_request?(request)
if @router.includes_request?(request) && @accept_request_filter.call(request)
handle(request)
else
@app.call(request.env)
Expand Down
16 changes: 16 additions & 0 deletions test/middleware/request_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,22 @@ def app
end
end

describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, accept_request_filter:, expected:|
it description do
@app = new_rack_app(prefix: '/v1', schema: open_api_3_schema, accept_request_filter: accept_request_filter)

post 'v1/characters', JSON.generate(string_post_1: 1)

assert_equal expected[:status], last_response.status
end
end
end

private

def new_rack_app(options = {})
Expand Down
16 changes: 16 additions & 0 deletions test/middleware/request_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ def app
assert_equal 200, last_response.status
end

describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, accept_request_filter:, expected:|
it description do
@app = new_rack_app(prefix: '/v1', schema: hyper_schema, accept_request_filter: accept_request_filter)

post '/v1/apps', '{x:y}'

assert_equal expected[:status], last_response.status
end
end
end

private

def new_rack_app(options = {})
Expand Down
16 changes: 16 additions & 0 deletions test/middleware/response_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ def app
end
end

describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 500 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 500 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, accept_request_filter:, expected:|
it description do
@app = new_response_rack('not_json', {}, schema: open_api_3_schema, prefix: '/v1', accept_request_filter: accept_request_filter)

get 'v1/characters'

assert_equal expected[:status], last_response.status
end
end
end

private

def new_response_rack(response, headers = {}, options = {}, rack_options = {})
Expand Down
16 changes: 16 additions & 0 deletions test/middleware/response_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ def app
assert_match(/valid JSON/i, last_response.body)
end

describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 500 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 500 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, accept_request_filter:, expected:|
it description do
@app = new_rack_app('not_json', {}, schema: hyper_schema, prefix: '/v1', accept_request_filter: accept_request_filter)

get '/v1/apps'

assert_equal expected[:status], last_response.status
end
end
end

private

def new_rack_app(response, headers = {}, options = {})
Expand Down