Skip to content

Commit

Permalink
Make validation consistent for multiple params per requires
Browse files Browse the repository at this point in the history
Add spec for multiple params per requires

fix validation inconsistency

update changelog

update UPGRADING.md
  • Loading branch information
dgasper committed Oct 14, 2016
1 parent 3a34ca2 commit afff083
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Next Release

* [#1503](https://github.com/ruby-grape/grape/pull/1503): Allow to use regexp validator with arrays - [@akoltun](https://github.com/akoltun).
* [#1507](https://github.com/ruby-grape/grape/pull/1507): Add group attributes for parameter definitions - [@304](https://github.com/304).
* [#1510](https://github.com/ruby-grape/grape/pull/1510): Fix inconsistent validation - [@dgasper](https://github.com/dgasper).
* Your contribution here.

0.18.0 (10/7/2016)
Expand Down
15 changes: 15 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Upgrading Grape
===============

### Upgrading to >= 0.18.1

#### Changed endpoint params validation

Grape now returns validation errors for all params when multiple params are passed to a requires.
The following code will return `one is missing, two is missing` when calling the endpoint without parameters.

```ruby
params do
requires :one, :two
end
```

Prior to this version the response would be `one is missing`.

### Upgrading to >= 0.17.0

#### Removed official support for Ruby < 2.2.2
Expand Down
3 changes: 1 addition & 2 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ def validate(request)
def validate!(params)
attributes = AttributesIterator.new(self, @scope, params)
array_errors = []
attributes.each do |resource_params, attr_name, inside_array|
attributes.each do |resource_params, attr_name|
next unless @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name))

begin
validate_param!(attr_name, resource_params)
rescue Grape::Exceptions::Validation => e
raise e unless inside_array
# we collect errors inside array because
# there may be more than one error per field
array_errors << e
Expand Down
30 changes: 29 additions & 1 deletion spec/grape/validations/validators/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,34 @@ def app
end
end

context 'with multiple parameters per requires' do
before do
subject.params do
requires :one, :two
end
subject.get '/single-requires' do
'Hello'
end

subject.params do
requires :one
requires :two
end
subject.get '/multiple-requires' do
'Hello'
end
end
it 'validates for all defined params' do
get '/single-requires'
expect(last_response.status).to eq(400)
single_requires_error = last_response.body

get '/multiple-requires'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq(single_requires_error)
end
end

context 'with required parameters and no type' do
before do
subject.params do
Expand All @@ -117,7 +145,7 @@ def app
it 'validates name, company' do
get '/'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"name is missing"}')
expect(last_response.body).to eq('{"error":"name is missing, company is missing"}')

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

0 comments on commit afff083

Please sign in to comment.