Skip to content

Commit

Permalink
Style fixes for ruby-grape#543 / ruby-grape#545
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalex committed Jan 1, 2014
1 parent 52c9fbd commit 2f0e5f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ def push_declared_params(attrs)
private

def new_scope(attrs, optional = false, &block)
opts = attrs[1] || { :type => Array }
raise ArgumentError unless opts.keys.to_set.subset? [ :type ].to_set
opts = attrs[1] || { type: Array }
raise ArgumentError unless opts.keys.to_set.subset? [:type].to_set
ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def validate!(params)
end

def validate_param!(attr_name, params)
unless params.respond_to? :has_key? and params.has_key?(attr_name)
unless params.respond_to?(:has_key?) && params.has_key?(attr_name)
raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message_key: :presence
end
end
Expand Down
30 changes: 15 additions & 15 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ def app
last_response.status.should == 400
last_response.body.should == 'items is invalid, items[key] is missing'

get '/required', items: { :key => 'foo' }
get '/required', items: { key: 'foo' }
last_response.status.should == 400
last_response.body.should == 'items is invalid'
end

it "doesn't throw a missing param when param is present" do
get '/required', items: [{key: 'hello'}, {key: 'world'}]
get '/required', items: [{ key: 'hello' }, { key: 'world' }]
last_response.status.should == 200
last_response.body.should == 'required works'
end
Expand Down Expand Up @@ -155,13 +155,13 @@ def app
last_response.status.should == 400
last_response.body.should == 'items is invalid, items[key] is missing'

get '/required', items: [{ :key => 'foo' }]
get '/required', items: [{ key: 'foo' }]
last_response.status.should == 400
last_response.body.should == 'items is invalid'
end

it "doesn't throw a missing param when param is present" do
get '/required', items: {key: 'hello'}
get '/required', items: { key: 'hello' }
last_response.status.should == 200
last_response.body.should == 'required works'
end
Expand Down Expand Up @@ -237,7 +237,7 @@ def app

it 'can handle new scopes within child elements' do
get '/within_array', children: [
{ name: 'John', parents: [{ name: 'Jane' }, { name: 'Bob'}] },
{ name: 'John', parents: [{ name: 'Jane' }, { name: 'Bob' }] },
{ name: 'Joe', parents: [{ name: 'Josie' }] }
]
last_response.status.should == 200
Expand Down Expand Up @@ -276,7 +276,7 @@ def app
last_response.status.should == 400
last_response.body.should == 'children is invalid, children[parents] is missing'

get '/within_array', children: [name: 'Jay', parents: { name: 'Fred'}]
get '/within_array', children: [name: 'Jay', parents: { name: 'Fred' }]
last_response.status.should == 400
last_response.body.should == 'children[parents] is invalid'
end
Expand Down Expand Up @@ -331,7 +331,7 @@ def app
last_response.status.should == 400
last_response.body.should == 'planets is invalid'

get '/req', planets: [{:name => 'Venus'}, {:name => 'Mars'}]
get '/req', planets: [{ :name => 'Venus' }, { :name => 'Mars' }]
last_response.status.should == 200

put_with_json '/req', planets: []
Expand All @@ -347,7 +347,7 @@ def app
last_response.status.should == 400
last_response.body.should == 'moons is invalid'

get '/opt', name: "Jupiter", moons: [{:name => 'Io'}, {:name => 'Callisto'}]
get '/opt', name: "Jupiter", moons: [{ :name => 'Io' }, { :name => 'Callisto' }]
last_response.status.should == 200

put_with_json '/opt', name: "Venus"
Expand All @@ -357,7 +357,7 @@ def app
last_response.status.should == 200
end

it 'group defaults to Array type' do
it 'group defaults to Array type' do
get '/grp', stars: "Sun"
last_response.status.should == 400
last_response.body.should == 'stars is invalid, stars[name] is missing'
Expand All @@ -366,7 +366,7 @@ def app
last_response.status.should == 400
last_response.body.should == 'stars is invalid'

get '/grp', stars: [{:name => 'Sun'}]
get '/grp', stars: [{ :name => 'Sun' }]
last_response.status.should == 200

put_with_json '/grp', stars: []
Expand All @@ -391,7 +391,7 @@ def app

it 'can handle new scopes within child elements' do
put_with_json '/within_array', children: [
{ name: 'John', parents: [{ name: 'Jane' }, { name: 'Bob'}] },
{ name: 'John', parents: [{ name: 'Jane' }, { name: 'Bob' }] },
{ name: 'Joe', parents: [{ name: 'Josie' }] }
]
last_response.status.should == 200
Expand Down Expand Up @@ -451,7 +451,7 @@ def app
last_response.status.should == 400
last_response.body.should == 'items is invalid, items[key] is missing'

get '/optional_group', items: { :key => 'foo' }
get '/optional_group', items: { key: 'foo' }
last_response.status.should == 400
last_response.body.should == 'items is invalid'
end
Expand Down Expand Up @@ -505,15 +505,15 @@ def app
end

it 'handles validation within arrays' do
get '/nested_optional_group', items: [{key: 'foo'}]
get '/nested_optional_group', items: [{ key: 'foo' }]
last_response.status.should == 400
last_response.body.should == 'items[required_subitems] is missing'

get '/nested_optional_group', items: [{key: 'foo', required_subitems: [{ value: 'bar' }]}]
get '/nested_optional_group', items: [{ key: 'foo', required_subitems: [{ value: 'bar' }]}]
last_response.status.should == 200
last_response.body.should == 'nested optional group works'

get '/nested_optional_group', items: [{key: 'foo', required_subitems: [{ value: 'bar' }], optional_subitems: [{ not_value: 'baz' }]}]
get '/nested_optional_group', items: [{ key: 'foo', required_subitems: [{ value: 'bar' }], optional_subitems: [{ not_value: 'baz' }]}]
last_response.status.should == 400
last_response.body.should == 'items[optional_subitems][value] is missing'
end
Expand Down

0 comments on commit 2f0e5f7

Please sign in to comment.