Sha256: c4dfcc14c2cfbb1495b8b04f928859d2ec05973ef5773f476cf14236db8e990c

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 == []
          []
        else
          ['invalid']
        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.8.2 lib/esplanade/response/validation.rb
esplanade-1.8.1 lib/esplanade/response/validation.rb
esplanade-1.8.0 lib/esplanade/response/validation.rb