-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added request validations feature * README.md update * Rubocop fixes * Missing newline * Updated version to 1.0.0 * README and introspection bug fix * README cleaned up
- Loading branch information
1 parent
1418265
commit 221d657
Showing
14 changed files
with
194 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -47,3 +47,4 @@ Gemfile.lock | |
|
||
/demo | ||
vendor/ruby | ||
/vendor |
This file contains 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 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 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 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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Apicraft | ||
module Concerns | ||
# Helper class with shared methods | ||
module MiddlewareUtil | ||
def config | ||
@config ||= Apicraft.config | ||
end | ||
|
||
def convertor(format) | ||
return if format.blank? | ||
|
||
Apicraft::Constants::MIME_TYPE_CONVERTORS[format] | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module Apicraft | ||
module Middlewares | ||
# Middleware to handle Request Validation. | ||
class RequestValidator | ||
include Concerns::MiddlewareUtil | ||
|
||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
return @app.call(env) unless validate? | ||
|
||
request = ActionDispatch::Request.new(env) | ||
content_type = request.content_type | ||
|
||
operation = Apicraft::Openapi::Contract.find_by_operation( | ||
request.method, request.path_info | ||
)&.operation( | ||
request.method, request.path_info | ||
) | ||
return @app.call(env) if operation.blank? | ||
|
||
operation.validate_request_body( | ||
content_type, | ||
request.params | ||
) | ||
@app.call(env) | ||
rescue OpenAPIParser::OpenAPIError => e | ||
[ | ||
config.request_validation_http_code, | ||
{ 'Content-Type': content_type }, | ||
[ | ||
response_body(e)&.send(convertor(content_type)) | ||
].compact | ||
] | ||
end | ||
|
||
private | ||
|
||
def validate? | ||
config.request_validation_enabled? | ||
end | ||
|
||
def response_body(ex) | ||
config.request_validation_response_body.call(ex) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Oops, something went wrong.