Sha256: 04869c9af1ec11b52a0e549db6673ef2574d410d8664f8e0d8e6b01b3c880113

Contents?: true

Size: 986 Bytes

Versions: 4

Compression:

Stored size: 986 Bytes

Contents

module SimpleJsonapi
  module Rails
    module ActionController
      class RequestValidator
        attr_reader :request, :params

        delegate :body, :content_type, :accept, :path, to: :request, prefix: true

        def initialize(request, params)
          @request = request
          @params = params
        end

        def valid_content_type_header?
          !request_has_body? || request_content_type == SimpleJsonapi::MIME_TYPE
        end

        def valid_accept_header?
          request_accept.blank? || request_accept == SimpleJsonapi::MIME_TYPE
        end

        def valid_request_body?
          return true unless request_has_body?

          params["data"].present? && valid_relationship_body?
        end

        def valid_relationship_body?
          request_path.exclude?("relationships") || params["data"]&.is_a?(Array)
        end

        private

        def request_has_body?
          request_body.size > 0
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple_jsonapi_rails-1.2.0 lib/simple_jsonapi/rails/action_controller/request_validator.rb
simple_jsonapi_rails-1.1.1 lib/simple_jsonapi/rails/action_controller/request_validator.rb
simple_jsonapi_rails-1.1.0 lib/simple_jsonapi/rails/action_controller/request_validator.rb
simple_jsonapi_rails-1.0.0 lib/simple_jsonapi/rails/action_controller/request_validator.rb