Sha256: d9c5f2395dd9921979f0ccd626f0a82252ba8e25ac30e68fcbdd1bd878205158

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 KB

Contents

require 'json-schema'

module Esplanade
  class Response
    class Validation
      def initialize(request, doc, raw)
        @request = request
        @doc = doc
        @raw = raw
      end

      def valid!
        @error ||= if @doc.json_schemas.size == 1
                     one_json_schema
                   else
                     more_than_one_json_schema
                   end
        raise Invalid, message if @error != []
      end

      private

      def one_json_schema
        JSON::Validator.fully_validate(@doc.json_schemas.first, @raw.body.to_hash)
      end

      def more_than_one_json_schema
        main_res = @doc.json_schemas.each do |json_schema|
          res = JSON::Validator.fully_validate(json_schema, @raw.body.to_hash)
          break res if res == []
        end
        if main_res != []
          ['invalid']
        else
          []
        end
      end

      def message
        {
          request: {
            method: @request.raw.method,
            path: @request.raw.path
          },
          status: @raw.status,
          body: @raw.body.to_string,
          error: @error
        }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
esplanade-1.4.0 lib/esplanade/response/validation.rb
esplanade-1.3.0 lib/esplanade/response/validation.rb
esplanade-1.2.1 lib/esplanade/response/validation.rb
esplanade-1.2.0 lib/esplanade/response/validation.rb
esplanade-1.1.2 lib/esplanade/response/validation.rb
esplanade-1.1.1 lib/esplanade/response/validation.rb
esplanade-1.1.0 lib/esplanade/response/validation.rb
esplanade-1.0.1 lib/esplanade/response/validation.rb
esplanade-1.0.0 lib/esplanade/response/validation.rb