Module: Sinatra::Swagger::SpecEnforcer

Defined in:
lib/sinatra/swagger/spec_enforcer.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sinatra/swagger/spec_enforcer.rb', line 6

def self.registered(app)
  app.register Swagger::SwaggerLinked

  app.after do
    next unless response.content_type =~ %r{^application/(?:.+\+)?json$}
    next unless body = JSON.parse(response.body.first) rescue nil
    next if swagger_spec.nil?
    schema = schema_from_spec_at("responses/#{response.status}/schema")
    next if schema.nil?
    begin
      JSON::Validator.validate!(schema, body)
    rescue JSON::Schema::ValidationError => e
      e.message = "Response JSON did not match the Swagger schema: #{e.message}\n#{body}"
      raise e
    end
  end
end