Class: Committee::Middleware::RequestValidation
- Defined in:
- lib/committee/middleware/request_validation.rb
Instance Method Summary collapse
- #handle(request) ⇒ Object
-
#initialize(app, options = {}) ⇒ RequestValidation
constructor
A new instance of RequestValidation.
Methods inherited from Base
Constructor Details
#initialize(app, options = {}) ⇒ RequestValidation
Returns a new instance of RequestValidation.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/committee/middleware/request_validation.rb', line 3 def initialize(app, ={}) super @allow_form_params = .fetch(:allow_form_params, true) @allow_query_params = .fetch(:allow_query_params, true) @check_content_type = .fetch(:check_content_type, true) @check_header = .fetch(:check_header, true) @optimistic_json = .fetch(:optimistic_json, false) @strict = [:strict] @coerce_date_times = .fetch(:coerce_date_times, false) @coerce_recursive = .fetch(:coerce_recursive, true) @coerce_form_params = .fetch(:coerce_form_params, @schema.driver.default_coerce_form_params) @coerce_path_params = .fetch(:coerce_path_params, @schema.driver.default_path_params) @coerce_query_params = .fetch(:coerce_query_params, @schema.driver.default_query_params) # deprecated @allow_extra = [:allow_extra] end |
Instance Method Details
#handle(request) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/committee/middleware/request_validation.rb', line 26 def handle(request) link, param_matches = @router.find_request_link(request) if link # Attempts to coerce parameters that appear in a link's URL to Ruby # types that can be validated with a schema. if @coerce_path_params Committee::StringParamsCoercer.new(param_matches, link.schema, coerce_recursive: @coerce_recursive).call! end # Attempts to coerce parameters that appear in a query string to Ruby # types that can be validated with a schema. if @coerce_query_params && !request.GET.nil? && !link.schema.nil? Committee::StringParamsCoercer.new(request.GET, link.schema, coerce_recursive: @coerce_recursive).call! end end request.env[@params_key], request.env[@headers_key] = Committee::RequestUnpacker.new( request, allow_form_params: @allow_form_params, allow_query_params: @allow_query_params, coerce_form_params: @coerce_form_params, optimistic_json: @optimistic_json, schema: link ? link.schema : nil ).call request.env[@params_key].merge!(param_matches) if param_matches if link validator = Committee::RequestValidator.new(link, check_content_type: @check_content_type, check_header: @check_header) validator.call(request, request.env[@params_key], request.env[@headers_key]) parameter_coerce!(request, link, @params_key) parameter_coerce!(request, link, "rack.request.query_hash") if !request.GET.nil? && !link.schema.nil? @app.call(request.env) elsif @strict raise Committee::NotFound, "That request method and path combination isn't defined." else @app.call(request.env) end rescue Committee::BadRequest, Committee::InvalidRequest raise if @raise @error_class.new(400, :bad_request, $!.).render rescue Committee::NotFound => e raise if @raise @error_class.new( 404, :not_found, e. ).render rescue JSON::ParserError raise Committee::InvalidRequest if @raise @error_class.new(400, :bad_request, "Request body wasn't valid JSON.").render end |