Sha256: e54900a47fb6ae1f82ff4adaf5931ceec741e29cc52b54c023db3e32f197eb9e

Contents?: true

Size: 867 Bytes

Versions: 3

Compression:

Stored size: 867 Bytes

Contents

# frozen_string_literal: true

require 'request_handler/schema_parser'
require 'request_handler/error'
module RequestHandler
  class QueryParser < SchemaParser
    RESERVED_KEYS = %w[fields filter include page sort].freeze

    def initialize(params:, schema:, schema_options: {})
      super(schema: schema, schema_options: schema_options)
      @query = params.dup
      RESERVED_KEYS.each { |key| query.delete(key) }
    end

    def run
      validate_schema(query)
    rescue SchemaValidationError => e
      raise ExternalArgumentError, (e.errors.map do |schema_error|
        param = schema_error[:source][:pointer]
        { status: '400',
          code: "#{query[param] ? 'INVALID' : 'MISSING'}_QUERY_PARAMETER",
          detail: schema_error[:detail],
          source: { param: param } }
      end)
    end

    private

    attr_reader :query
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
request_handler-2.1.0 lib/request_handler/query_parser.rb
request_handler-2.0.0 lib/request_handler/query_parser.rb
request_handler-1.3.0 lib/request_handler/query_parser.rb