Sha256: 6dded88024c4aff1c4f57885edf384a8a5e9eb88a220d782228fd7e4caeb7e18

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'json-schema'
require 'esplanade/error'

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

      def valid!
        raise ContentTypeIsNotJson.new(**mini_message) unless @doc.content_type == 'application/json'

        @error ||= if @doc.json_schemas.size == 1
                     one_json_schema
                   else
                     more_than_one_json_schema
                   end

        raise Invalid.new(**message) unless @error.empty?
      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 mini_message
        {
          method: @doc.method,
          path: @doc.path,
          raw_path: @raw.raw_path,
          content_type: @doc.content_type
        }
      end

      def message
        {
          method: @raw.method,
          path: @raw.path,
          raw_path: @raw.raw_path,
          content_type: @raw.content_type,
          body: @raw.body.to_hash,
          error: @error
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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