Sha256: 1d87a3f2e7eacbc61c7406a3c89d5d36321ce4377fcf98a4bf4ec5372cb64dcd

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

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 ||= JSON::Validator.fully_validate(@doc.json_schema, @raw.body.to_hash)

        raise Invalid.new(**message) unless @error.empty?
      end

      private

      def mini_message
        {
          method: @doc.method,
          path: @doc.path,
          content_type: @doc.content_type
        }
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esplanade-1.5.0 lib/esplanade/request/validation.rb