Sha256: 812e236887bb009701a0c7974a71d7aa03b83d8759a7b5951a249c8c25ab9733

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 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.new(**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,
            raw_path: @request.raw.raw_path
          },
          status: @raw.status,
          body: @raw.body.to_string,
          error: @error
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
esplanade-1.7.1 lib/esplanade/response/validation.rb
esplanade-1.7.0 lib/esplanade/response/validation.rb
esplanade-1.6.0 lib/esplanade/response/validation.rb