Skip to content

Commit de457c6

Browse files
committed
Merge pull request #671 from dm1try/fix_required_params_inside_optional_group
allow required param with predefined set of values to be nil inside optional group
2 parents 022bec2 + f60eaa6 commit de457c6

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Next Release
1212

1313
#### Fixes
1414

15+
* [#671](https://github.com/intridea/grape/pull/671): Allow required param with predefined set of values to be nil inside optional group - [@dm1try](https://github.com/dm1try).
1516
* [#651](https://github.com/intridea/grape/pull/651): The `rescue_from` keyword now properly defaults to rescuing subclasses of exceptions - [@xevix](https://github.com/xevix).
1617
* [#614](https://github.com/intridea/grape/pull/614): Params with `nil` value are now refused by `RegexpValidator` - [@dm1try](https://github.com/dm1try).
1718
* [#494](https://github.com/intridea/grape/issues/494): Fixed performance issue with requests carrying a large payload - [@dblock](https://github.com/dblock).

lib/grape/validations.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ def full_name(name)
174174
name.to_s
175175
end
176176

177+
def root?
178+
!@parent
179+
end
180+
177181
protected
178182

179183
def push_declared_params(attrs)

lib/grape/validations/values.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ def initialize(attrs, options, required, scope)
88
end
99

1010
def validate_param!(attr_name, params)
11-
if (params[attr_name] || @required) && !(@values.is_a?(Proc) ? @values.call : @values).include?(params[attr_name])
11+
if (params[attr_name] || required_for_root_scope?) && !(@values.is_a?(Proc) ? @values.call : @values).include?(params[attr_name])
1212
raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message_key: :values
1313
end
1414
end
15+
16+
private
17+
18+
def required_for_root_scope?
19+
@required && @scope.root?
20+
end
1521
end
1622
end
1723
end

spec/grape/validations/values_spec.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class API < Grape::API
4949
get '/values/coercion' do
5050
{ type: params[:type] }
5151
end
52+
53+
params do
54+
optional :optional do
55+
requires :type, values: ["a", "b"]
56+
end
57+
end
58+
get '/optional_with_required_values'
5259
end
5360
end
5461
end
@@ -69,10 +76,17 @@ def app
6976
expect(last_response.body).to eq({ error: "type does not have a valid value" }.to_json)
7077
end
7178

72-
it 'does not allow nil value for a parameter' do
73-
get("/", type: nil)
74-
expect(last_response.status).to eq 400
75-
expect(last_response.body).to eq({ error: "type does not have a valid value" }.to_json)
79+
context 'nil value for a parameter' do
80+
it 'does not allow for root params scope' do
81+
get("/", type: nil)
82+
expect(last_response.status).to eq 400
83+
expect(last_response.body).to eq({ error: "type does not have a valid value" }.to_json)
84+
end
85+
86+
it 'allows for a required param in child scope' do
87+
get('/optional_with_required_values')
88+
expect(last_response.status).to eq 200
89+
end
7690
end
7791

7892
it 'allows a valid default value' do

0 commit comments

Comments
 (0)