Skip to content

Commit 813925e

Browse files
dgasperdblock
authored andcommitted
Make validation consistent for multiple params per requires
Add spec for multiple params per requires fix validation inconsistency update changelog update UPGRADING.md
1 parent eb1711a commit 813925e

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Next Release
55

66
* [#1503](https://github.com/ruby-grape/grape/pull/1503): Allow to use regexp validator with arrays - [@akoltun](https://github.com/akoltun).
77
* [#1507](https://github.com/ruby-grape/grape/pull/1507): Add group attributes for parameter definitions - [@304](https://github.com/304).
8-
* [#1512](https://github.com/ruby-grape/grape/pull/1512): Fix for deeply nested params TypeError situation - [@krbs](https://github.com/krbs).
8+
* [#1512](https://github.com/ruby-grape/grape/pull/1512): Fix: deeply nested parameters are included within `#declared(params)` - [@krbs](https://github.com/krbs).
9+
* [#1510](https://github.com/ruby-grape/grape/pull/1510): Fix: inconsistent validation for multiple parameters - [@dgasper](https://github.com/dgasper).
910
* Your contribution here.
1011

1112
#### Fixes

UPGRADING.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ When a request is made to the built-in `OPTIONS` handler, only the `before` and
99
callbacks associated with the resource will be run. The `before_validation` and
1010
`after_validation` callbacks and parameter validations will be skipped.
1111

12+
See [#1505](https://github.com/ruby-grape/grape/pull/1505) for more information.
13+
14+
#### Changed endpoint params validation
15+
16+
Grape now correctly returns validation errors for all params when multiple params are passed to a requires.
17+
The following code will return `one is missing, two is missing` when calling the endpoint without parameters.
18+
19+
```ruby
20+
params do
21+
requires :one, :two
22+
end
23+
```
24+
25+
Prior to this version the response would be `one is missing`.
26+
27+
See [#1510](https://github.com/ruby-grape/grape/pull/1510) for more information.
28+
1229
### Upgrading to >= 0.17.0
1330

1431
#### Removed official support for Ruby < 2.2.2
@@ -22,7 +39,7 @@ See [#1441](https://github.com/ruby-grape/grape/pull/1441) for nmore information
2239
The `rescue_from` clauses declared inside a namespace would take a priority over ones declared in the root scope.
2340
This could possibly affect those users who use different `rescue_from` clauses in root scope and in namespaces.
2441

25-
See [#1405](https://github.com/ruby-grape/grape/pull/1405) for more inforomation.
42+
See [#1405](https://github.com/ruby-grape/grape/pull/1405) for more information.
2643

2744
#### Helper methods injected inside `rescue_from` in middleware
2845

lib/grape/validations/validators/base.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ def validate(request)
3939
def validate!(params)
4040
attributes = AttributesIterator.new(self, @scope, params)
4141
array_errors = []
42-
attributes.each do |resource_params, attr_name, inside_array|
42+
attributes.each do |resource_params, attr_name|
4343
next unless @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name))
4444

4545
begin
4646
validate_param!(attr_name, resource_params)
4747
rescue Grape::Exceptions::Validation => e
48-
raise e unless inside_array
4948
# we collect errors inside array because
5049
# there may be more than one error per field
5150
array_errors << e

spec/grape/validations/validators/presence_spec.rb

+29-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,34 @@ def app
105105
end
106106
end
107107

108+
context 'with multiple parameters per requires' do
109+
before do
110+
subject.params do
111+
requires :one, :two
112+
end
113+
subject.get '/single-requires' do
114+
'Hello'
115+
end
116+
117+
subject.params do
118+
requires :one
119+
requires :two
120+
end
121+
subject.get '/multiple-requires' do
122+
'Hello'
123+
end
124+
end
125+
it 'validates for all defined params' do
126+
get '/single-requires'
127+
expect(last_response.status).to eq(400)
128+
single_requires_error = last_response.body
129+
130+
get '/multiple-requires'
131+
expect(last_response.status).to eq(400)
132+
expect(last_response.body).to eq(single_requires_error)
133+
end
134+
end
135+
108136
context 'with required parameters and no type' do
109137
before do
110138
subject.params do
@@ -117,7 +145,7 @@ def app
117145
it 'validates name, company' do
118146
get '/'
119147
expect(last_response.status).to eq(400)
120-
expect(last_response.body).to eq('{"error":"name is missing"}')
148+
expect(last_response.body).to eq('{"error":"name is missing, company is missing"}')
121149

122150
get '/', name: 'Bob'
123151
expect(last_response.status).to eq(400)

0 commit comments

Comments
 (0)