Class: Committee::Middleware::RequestValidation

Inherits:
Base
  • Object
show all
Defined in:
lib/committee/middleware/request_validation.rb

Instance Method Summary collapse

Methods inherited from Base

#call, get_schema

Constructor Details

#initialize(app, options = {}) ⇒ RequestValidation

Returns a new instance of RequestValidation.



6
7
8
9
10
# File 'lib/committee/middleware/request_validation.rb', line 6

def initialize(app, options={})
  super

  @strict = options[:strict]
end

Instance Method Details

#handle(request) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/committee/middleware/request_validation.rb', line 12

def handle(request)
  begin
    schema_validator = build_schema_validator(request)
    schema_validator.request_validate(request)

    raise Committee::NotFound, "That request method and path combination isn't defined." if !schema_validator.link_exist? && @strict
  rescue Committee::BadRequest, Committee::InvalidRequest
    handle_exception($!, request.env)
    raise if @raise
    return @error_class.new(400, :bad_request, $!.message, request).render unless @ignore_error
  rescue Committee::NotFound => e
    handle_exception($!, request.env)
    raise if @raise
    return @error_class.new(404, :not_found, e.message, request).render unless @ignore_error
  rescue JSON::ParserError
    handle_exception($!, request.env)
    raise Committee::InvalidRequest if @raise
    return @error_class.new(400, :bad_request, "Request body wasn't valid JSON.", request).render unless @ignore_error
  end

  @app.call(request.env)
end