Class: Interpol::ResponseSchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/interpol/response_schema_validator.rb

Overview

Rack middleware that validates response data against the schema definition for the endpoint. Can be configured to raise an error or warn in this condition. Intended for development and test use.

Defined Under Namespace

Classes: Handler, HandlerWithWarnings

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ ResponseSchemaValidator

Returns a new instance of ResponseSchemaValidator.



10
11
12
13
14
# File 'lib/interpol/response_schema_validator.rb', line 10

def initialize(app, &block)
  @config = Configuration.default.customized_duplicate(&block)
  @app = app
  @handler_class = @config.validation_mode == :warn ? HandlerWithWarnings : Handler
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/interpol/response_schema_validator.rb', line 16

def call(env)
  status, headers, body = @app.call(env)
  unless @config.validate_response?(env, status, headers, body)
    return status, headers, body
  end

  handler = @handler_class.new(status, headers, body, env, @config)
  handler.validate!

  return status, headers, handler.extracted_body
end