Skip to content

Fix alias on dependent param bug #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
* [#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).
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
* [#1810](https://github.com/ruby-grape/grape/pull/1810): Fix alias on dependent param bug - [@darren987469](https://github.com/darren987469).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe say something more explicit? Eg. "Fix support in given for aliased params"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is more clear.


### 1.1.0 (8/4/2018)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,18 @@ params do
end
```

You can set alias for parameter:

```ruby
params do
optional :category, as: :type
given type: ->(val) { val == 'foo' } do
requires :description
end
end
```

Note: param in `given` should be the aliased one. In the example, it should be `type`, not `category`.

### Group Options

Expand Down
1 change: 1 addition & 0 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def push_declared_params(attrs, **opts)
if opts && opts[:as]
@api.route_setting(:aliased_params, @api.route_setting(:aliased_params) || [])
@api.route_setting(:aliased_params) << { attrs.first => opts[:as] }
attrs = [opts[:as]]
end

@declared_params.concat attrs
Expand Down
17 changes: 17 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,23 @@ def initialize(value)
expect(body.keys).to_not include('b')
end

it 'allows aliasing of dependent on parameter' do
subject.params do
optional :a, as: :b
given b: ->(val) { val == 'x' } do
requires :c
end
end
subject.get('/') { declared(params) }

get '/', a: 'x'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'c is missing'

get '/', a: 'y'
expect(last_response.status).to eq 200
end

it 'does not validate nested requires when given is false' do
subject.params do
requires :a, type: String, allow_blank: false, values: %w[x y z]
Expand Down