Skip to content

Commit

Permalink
Added param key to ValidationError class
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgotterer committed Aug 13, 2012
1 parent 8d15724 commit 1fd96f7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/grape/exceptions/validation_error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require 'grape/exceptions/base'

class ValidationError < Grape::Exceptions::Base
attr_accessor :param

def initialize(args = {})
@param = args[:param].to_s if args.has_key? :param
super
end
end
2 changes: 1 addition & 1 deletion lib/grape/validations/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def validate_param!(attr_name, params)
if valid_type?(new_value)
params[attr_name] = new_value
else
raise ValidationError, :status => 400, :message => "invalid parameter: #{attr_name}"
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
end
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 @@ -3,7 +3,7 @@ module Validations
class PresenceValidator < Validator
def validate_param!(attr_name, params)
unless params.has_key?(attr_name)
raise ValidationError, :status => 400, :message => "missing parameter: #{attr_name}"
raise ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Validations
class RegexpValidator < SingleOptionValidator
def validate_param!(attr_name, params)
if params[attr_name] && !( params[attr_name].to_s =~ @option )
raise ValidationError, :status => 400, :message => "invalid parameter: #{attr_name}"
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
end
end
end
Expand Down

0 comments on commit 1fd96f7

Please sign in to comment.