Skip to content

Commit

Permalink
fix(required): broken with nested data
Browse files Browse the repository at this point in the history
  • Loading branch information
ProGM committed Dec 17, 2023
1 parent 492e91e commit 88cf2fc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
18 changes: 11 additions & 7 deletions lib/paramoid/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def to_required_params
# @param [Array<Symbol>] path
# @raise [ActionController::ParameterMissing] if one of the required params is missing
def ensure_required_params!(params, path: [])
if params.is_a?(Array)
params.flatten.each do |param|
ensure_required_params!(param, path: path)
end
return params
end

current_path = [*path, @name]

_raise_on_missing_parameter!(params, output_key, current_path) if @required
Expand All @@ -76,13 +83,9 @@ def ensure_required_params!(params, path: [])
end

def ensure_required_nested_params!(params, current_path)
if params.is_a?(Array)
params.flatten.each do |param|
@nested.ensure_required_params!(param[output_key], path: current_path)
end
else
@nested.ensure_required_params!(params ? params[output_key] : nil, path: current_path)
end
return params if current_path.size > 1 && params.nil?

@nested.ensure_required_params!(params ? params[output_key] : nil, path: current_path)

params
end
Expand Down Expand Up @@ -156,6 +159,7 @@ def nested?
private

def _raise_on_missing_parameter!(params, key, current_path)
return if params.nil?
return if _param_exist?(params, key)

raise ActionController::ParameterMissing, current_path.join('.')
Expand Down
29 changes: 28 additions & 1 deletion spec/paramoid/complex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@
}
)
end

context 'and the required parameter is missing' do
let(:params_hash) do
{

unwanted: 1,
name: 'some_name',
buyer: {
payment_method: {
id: 1
}
},
person: {},
items: [{}, { sub_items: [{ price: 5 }] }]
}
end

it 'raises an error' do
expect { sanitized }.to raise_error(
ActionController::ParameterMissing,
'param is missing or the value is empty: items.sub_items.id'
)
end
end
end
end

Expand Down Expand Up @@ -126,7 +150,10 @@
let(:user) { double(admin?: false) }
let(:params_hash) do
{
name: 'some_name'
name: 'some_name',
buyer: {
payment_method: {}
}
}
end

Expand Down
18 changes: 11 additions & 7 deletions spec/paramoid/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@
context 'when it\'s nested' do
let(:nested) { described_class.new(:nested, nil, required: true) }

it 'raises an error' do
expect do
subject.ensure_required_params!(params)
end.to raise_error(ActionController::ParameterMissing,
'param is missing or the value is empty: some_param.nested')
context 'and some_param is present, but nested is required' do
let(:params_hash) { { 'not_this_param' => 'some_value', 'some_param' => {} } }

it 'raises an error' do
expect do
subject.ensure_required_params!(params)
end.to raise_error(ActionController::ParameterMissing,
'param is missing or the value is empty: some_param.nested')
end
end

context 'and some_param is required' do
Expand All @@ -129,8 +133,8 @@
it 'skips the check' do
expect do
subject.ensure_required_params!(params)
end.to raise_error(ActionController::ParameterMissing,
'param is missing or the value is empty: some_param.nested')
end.not_to raise_error(ActionController::ParameterMissing,
'param is missing or the value is empty: some_param.nested')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize(user = nil)
param :discount, transformer: ->(data) { data&.to_f / 100 } if user.admin?

array :sub_items do
param :id
param :id, required: true
default :price, 0
end
end
Expand Down

0 comments on commit 88cf2fc

Please sign in to comment.