Skip to content

Commit

Permalink
use full attribute name for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak committed Feb 7, 2013
1 parent f6f585e commit 7c74435
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/grape/validations/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def validate_param!(attr_name, params)
if valid_type?(new_value)
params[attr_name] = new_value
else
raise Grape::Exceptions::Validation, :status => 400, :param => attr_name, :message_key => :coerce
raise Grape::Exceptions::Validation, :status => 400,
:param => @scope.full_name(attr_name), :message_key => :coerce
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/grape/validations/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Validations
class PresenceValidator < Validator
def validate_param!(attr_name, params)
unless params.has_key?(attr_name)
raise Grape::Exceptions::Validation, :status => 400, :param => attr_name, :message_key => :presence
raise Grape::Exceptions::Validation, :status => 400,
:param => @scope.full_name(attr_name), :message_key => :presence
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/validations/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Validations
class RegexpValidator < SingleOptionValidator
def validate_param!(attr_name, params)
if params[attr_name] && !( params[attr_name].to_s =~ @option )
raise Grape::Exceptions::Validation, :status => 400, :param => attr_name, :message_key => :regexp
raise Grape::Exceptions::Validation, :status => 400,
:param => @scope.full_name(attr_name), :message_key => :regexp
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/grape/validations/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ def app
it 'validates nested parameters' do
get('/nested')
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: first_name"}'
last_response.body.should == '{"error":"missing parameter: user[first_name]"}'

get('/nested', :user => {:first_name => "Billy"})
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: last_name"}'
last_response.body.should == '{"error":"missing parameter: user[last_name]"}'

get('/nested', :user => {:first_name => "Billy", :last_name => "Bob"})
last_response.status.should == 200
Expand All @@ -114,27 +114,27 @@ def app
it 'validates triple nested parameters' do
get('/nested_triple')
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: admin_name"}'
last_response.body.should == '{"error":"missing parameter: admin[admin_name]"}'

get('/nested_triple', :user => {:first_name => "Billy"})
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: admin_name"}'
last_response.body.should == '{"error":"missing parameter: admin[admin_name]"}'

get('/nested_triple', :admin => {:super => {:first_name => "Billy"}})
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: admin_name"}'
last_response.body.should == '{"error":"missing parameter: admin[admin_name]"}'

get('/nested_triple', :super => {:user => {:first_name => "Billy", :last_name => "Bob"}})
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: admin_name"}'
last_response.body.should == '{"error":"missing parameter: admin[admin_name]"}'

get('/nested_triple', :admin => {:super => {:user => {:first_name => "Billy"}}})
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: admin_name"}'
last_response.body.should == '{"error":"missing parameter: admin[admin_name]"}'

get('/nested_triple', :admin => { :admin_name => 'admin', :super => {:user => {:first_name => "Billy"}}})
last_response.status.should == 400
last_response.body.should == '{"error":"missing parameter: last_name"}'
last_response.body.should == '{"error":"missing parameter: admin[super][user][last_name]"}'

get('/nested_triple', :admin => { :admin_name => 'admin', :super => {:user => {:first_name => "Billy", :last_name => "Bob"}}})
last_response.status.should == 200
Expand Down

0 comments on commit 7c74435

Please sign in to comment.