Module: JSONAPI::Exceptions::QueryParamsExceptions

Defined in:
lib/easy/jsonapi/exceptions.rb,
lib/easy/jsonapi/exceptions/query_params_exceptions.rb

Overview

Validates that the Query Parameters comply with the JSONAPI specification

Defined Under Namespace

Classes: InvalidQueryParameter

Constant Summary collapse

SPECIAL_QUERY_PARAMS =

The jsonapi specific query parameters.

%i[include fields page sort filter].freeze

Class Method Summary collapse

Class Method Details

.check_compliance(rack_req_params, config_manager = nil, opts = {}) ⇒ Object

Checks to see if the query paramaters conform to the JSONAPI spec and raises InvalidQueryParameter

if any parts are found to be non compliant

Parameters:

  • rack_req_params (Hash)

    The hash of the query parameters given by Rack::Request



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/easy/jsonapi/exceptions/query_params_exceptions.rb', line 29

def self.check_compliance(rack_req_params, config_manager = nil, opts = {})
  impl_spec_names = rack_req_params.keys - %w[include fields page sort filter]
  impl_spec_names.each do |name|
    check_param_name(name)
  end
  
  err_msg = JSONAPI::Exceptions::UserDefinedExceptions.check_user_query_param_requirements(rack_req_params, config_manager, opts)
  raise err_msg unless err_msg.nil?

  nil
end

.check_param_name(name) ⇒ Object

Checks an implementation specific param name to see if it complies to the spec.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/easy/jsonapi/exceptions/query_params_exceptions.rb', line 42

def self.check_param_name(name)
  should_return =  
    NamingExceptions.check_member_constraints(name).nil? && \
    NamingExceptions.check_additional_constraints(name).nil? && \
    !name.include?('-')
  return if should_return
  
  raise_error(
    'Implementation specific query parameters MUST adhere to the same constraints ' \
    'as member names. Allowed characters are: a-z, A-Z, 0-9 for beginning, middle, or end characters, ' \
    "and '_' is allowed for middle characters. (While the JSON:API spec also allows '-', it is not " \
    'recommended, and thus is prohibited in this implementation). ' \
    'Implementation specific query members MUST contain at least one non a-z character as well. ' \
    "Param name given: \"#{name}\""
  )
end