4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/jsonapionify/api/resource/definitions/params.rb', line 4
def self.extended(klass)
klass.class_eval do
extend JSONAPIonify::InheritedAttributes
inherited_hash_attribute :param_definitions
context(:params, readonly: true, persisted: true) do |context|
should_error = false
params = self.class.param_definitions.select do |_, v|
v.actions.blank? || v.actions.include?(action_name)
end
context.request.params.replace(
[*params.values.select(&:has_default?).map(&:default), context.request.params].reduce(:deep_merge)
)
required_params = params.select do |_, v|
v.required
end
context.request.params.each do |k, v|
keypath = ParamOptions.hash_to_keypaths(k => v)[0]
reserved = ParamOptions.reserved?(k)
allowed = params.keys.include? keypath
valid = ParamOptions.valid?(k) || v.is_a?(Hash)
unless reserved || (allowed && valid) || !context.root_request?
should_error = true
error :parameter_invalid, ParamOptions.keypath_to_string(*keypath)
end
end unless context.request.options?
missing_params =
ParamOptions.missing_parameters(
context.request.params,
required_params.values.map(&:keypath)
)
if context.root_request? && missing_params.present?
error :parameters_missing, missing_params
end
halt if should_error
context.request.params
end
end
end
|