Skip to content

Commit 00e745e

Browse files
ShPakveldblock
authored andcommitted
Fixed using values with a default proc.
1 parent 834d06a commit 00e745e

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
* [#774](https://github.com/intridea/grape/pull/774): Extended `mutually_exclusive`, `exactly_one_of`, `at_least_one_of` to work inside any kind of group: `requires` or `optional`, `Hash` or `Array` - [@ShPakvel](https://github.com/ShPakvel).
55
* [#743](https://github.com/intridea/grape/pull/743): Added `allow_blank` parameter validator to validate non-empty strings - [@elado](https://github.com/elado).
6-
* Your contribution here.
76
* [#745](https://github.com/intridea/grape/pull/745): Removed `atom+xml`, `rss+xml`, and `jsonapi` content-types - [@akabraham](https://github.com/akabraham).
87
* [#745](https://github.com/intridea/grape/pull/745): Added `:binary, application/octet-stream` content-type - [@akabraham](https://github.com/akabraham).
98
* [#757](https://github.com/intridea/grape/pull/757): Changed `desc` can now be used with a block syntax - [@dspaeth-faber](https://github.com/dspaeth-faber).
9+
* [#779](https://github.com/intridea/grape/pull/779): Fixed using `values` with a `default` proc - [@ShPakvel](https://github.com/ShPakvel).
10+
* Your contribution here.
1011

1112
0.9.0 (8/27/2014)
1213
=================

lib/grape/validations/params_scope.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ def validates(attrs, validations)
104104
default = validations[:default]
105105
doc_attrs[:default] = default if default
106106

107+
default = default.call if default.is_a?(Proc)
108+
107109
values = validations[:values]
108110
doc_attrs[:values] = values if values
109111

110-
values = (values.is_a?(Proc) ? values.call : values)
112+
values = values.call if values.is_a?(Proc)
111113

112114
# default value should be present in values array, if both exist
113115
if default && values && !values.include?(default)

spec/grape/validations/validators/values_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ class API < Grape::API
4343
{ type: params[:type] }
4444
end
4545

46+
params do
47+
optional :type, values: ValuesModel.values, default: -> { ValuesModel.values.sample }
48+
end
49+
get '/default_lambda' do
50+
{ type: params[:type] }
51+
end
52+
53+
params do
54+
optional :type, values: -> { ValuesModel.values }, default: -> { ValuesModel.values.sample }
55+
end
56+
get '/default_and_values_lambda' do
57+
{ type: params[:type] }
58+
end
59+
4660
params do
4761
requires :type, type: Integer, desc: "An integer", values: [10, 11], default: 10
4862
end
@@ -123,6 +137,30 @@ def app
123137
expect(last_response.body).to eq({ error: "type does not have a valid value" }.to_json)
124138
end
125139

140+
it 'validates default value from proc' do
141+
get("/default_lambda")
142+
expect(last_response.status).to eq 200
143+
end
144+
145+
it 'validates default value from proc against values in a proc' do
146+
get("/default_and_values_lambda")
147+
expect(last_response.status).to eq 200
148+
end
149+
150+
it 'raises IncompatibleOptionValues on an invalid default value from proc' do
151+
subject = Class.new(Grape::API)
152+
expect {
153+
subject.params { optional :type, values: ['valid-type1', 'valid-type2', 'valid-type3'], default: -> { ValuesModel.values.sample + "_invalid" } }
154+
}.to raise_error Grape::Exceptions::IncompatibleOptionValues
155+
end
156+
157+
it 'raises IncompatibleOptionValues on an invalid default value from proc validating against values in a proc' do
158+
subject = Class.new(Grape::API)
159+
expect {
160+
subject.params { optional :type, values: -> { ValuesModel.values }, default: -> { ValuesModel.values.sample + "_invalid" } }
161+
}.to raise_error Grape::Exceptions::IncompatibleOptionValues
162+
end
163+
126164
it 'raises IncompatibleOptionValues on an invalid default value' do
127165
subject = Class.new(Grape::API)
128166
expect {

0 commit comments

Comments
 (0)