Skip to content

Commit c6e3f23

Browse files
author
Darren Chang
committed
Fix alias on dependent param bug
1 parent 3bb835a commit c6e3f23

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
1515
* [#1787](https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](https://github.com/michaellennox).
1616
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
17+
* [#1810](https://github.com/ruby-grape/grape/pull/1810): Fix alias on dependent param bug - [@darren987469](https://github.com/darren987469).
1718

1819
### 1.1.0 (8/4/2018)
1920

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,18 @@ params do
11751175
end
11761176
```
11771177

1178+
You can set alias for parameter:
1179+
1180+
```ruby
1181+
params do
1182+
optional :category, as: :type
1183+
given type: ->(val) { val == 'foo' } do
1184+
requires :description
1185+
end
1186+
end
1187+
```
1188+
1189+
Note: param in `given` should be the aliased one. In the example, it should be `type`, not `category`.
11781190

11791191
### Group Options
11801192

lib/grape/validations/params_scope.rb

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def push_declared_params(attrs, **opts)
121121
if opts && opts[:as]
122122
@api.route_setting(:aliased_params, @api.route_setting(:aliased_params) || [])
123123
@api.route_setting(:aliased_params) << { attrs.first => opts[:as] }
124+
attrs = [opts[:as]]
124125
end
125126

126127
@declared_params.concat attrs

spec/grape/validations/params_scope_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ def initialize(value)
497497
expect(body.keys).to_not include('b')
498498
end
499499

500+
it 'allows aliasing of dependent on parameter' do
501+
subject.params do
502+
optional :a, as: :b
503+
given b: ->(val) { val == 'x' } do
504+
requires :c
505+
end
506+
end
507+
subject.get('/') { declared(params) }
508+
509+
get '/', a: 'x'
510+
expect(last_response.status).to eq 400
511+
expect(last_response.body).to eq 'c is missing'
512+
513+
get '/', a: 'y'
514+
expect(last_response.status).to eq 200
515+
end
516+
500517
it 'does not validate nested requires when given is false' do
501518
subject.params do
502519
requires :a, type: String, allow_blank: false, values: %w[x y z]

0 commit comments

Comments
 (0)