Sha256: 2fa1a12e5dfa3cd8387c37b3c293c838cc0549edb6c8525b6fc40dc825c24607

Contents?: true

Size: 771 Bytes

Versions: 1

Compression:

Stored size: 771 Bytes

Contents

require "sinatra/swagger/param_validator"

module Sinatra
  module Swagger
    module SpecEnforcer
      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}"
            raise e
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-swagger-0.2.0 lib/sinatra/swagger/spec_enforcer.rb