Skip to content

Commit 5712b5b

Browse files
committed
Merge pull request #1255 from namusyaka/allow-param-definition-in-route_param
Allow param type definition in route_param
2 parents d8818f1 + 0017824 commit 5712b5b

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#1238](https://github.com/ruby-grape/grape/pull/1238): Call `after` of middleware on error - [@namusyaka](https://github.com/namusyaka).
1010
* [#1243](https://github.com/ruby-grape/grape/pull/1243): Add `header` support for middleware - [@namusyaka](https://github.com/namusyaka).
1111
* [#1252](https://github.com/ruby-grape/grape/pull/1252): Allow default to be a subset or equal to allowed values without raising IncompatibleOptionValues - [@jeradphelps](https://github.com/jeradphelps).
12+
* [#1255](https://github.com/ruby-grape/grape/pull/1255): Allow param type definition in route_param - [@namusyaka](https://github.com/namusyaka)
1213
* Your contribution here.
1314

1415
#### Fixes

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,19 @@ namespace :statuses do
11481148
end
11491149
```
11501150

1151+
You can also define a route parameter type by passing to `route_param`'s options.
1152+
1153+
```ruby
1154+
namespace :arithmetic do
1155+
route_param :n, type: Integer do
1156+
desc 'Returns in power'
1157+
get 'power' do
1158+
params[:n] ** params[:n]
1159+
end
1160+
end
1161+
end
1162+
```
1163+
11511164
### Custom Validators
11521165

11531166
```ruby

lib/grape/dsl/routing.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,13 @@ def reset_routes!
185185
# @option options [Regexp] You may supply a regular expression that the declared parameter must meet.
186186
def route_param(param, options = {}, &block)
187187
options = options.dup
188-
options[:requirements] = { param.to_sym => options[:requirements] } if options[:requirements].is_a?(Regexp)
188+
options[:requirements] = {
189+
param.to_sym => options[:requirements]
190+
} if options[:requirements].is_a?(Regexp)
191+
192+
Grape::Validations::ParamsScope.new(api: self) do
193+
requires param, type: options[:type]
194+
end if options.key?(:type)
189195
namespace(":#{param}", options, &block)
190196
end
191197

spec/grape/api_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ def app
249249
get '/users/23'
250250
expect(last_response.status).to eq(200)
251251
end
252+
253+
context 'with param type definitions' do
254+
it 'is used by passing to options' do
255+
subject.namespace :route_param do
256+
route_param :foo, type: Integer do
257+
get { params.to_json }
258+
end
259+
end
260+
get '/route_param/1234'
261+
expect(last_response.body).to eq("{\"foo\":1234}")
262+
end
263+
end
252264
end
253265

254266
describe '.route' do

0 commit comments

Comments
 (0)