Skip to content

Commit

Permalink
allow custom param coercion failure messages
Browse files Browse the repository at this point in the history
Custom param types can now report why coercion failed in their .parse
method by returning an InvalidValue initialized with an error message.
For example:

  Grape::Validations::Types::InvalidValue.new "is too short"
  • Loading branch information
Matt Zukowski committed Nov 14, 2015
1 parent a9a29a4 commit bfe9704
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/grape/validations/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ module Validations
module Types
# Instances of this class may be used as tokens to denote that
# a parameter value could not be coerced.
class InvalidValue; end
class InvalidValue
attr_reader :message
def initialize(message = nil)
@message = message
end
end

# Types representing a single value, which are coerced through Virtus
# or special logic in Grape.
Expand Down
7 changes: 6 additions & 1 deletion lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def validate_param!(attr_name, params)
if valid_type?(new_value)
params[attr_name] = new_value
else
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :coerce
bad_value = new_value
if bad_value.is_a?(Types::InvalidValue) && !bad_value.message.nil?
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: bad_value.message.to_s
else
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :coerce
end
end
end

Expand Down
48 changes: 48 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,54 @@ def initialize(value)
expect(last_response.status).to eq(400)
expect(last_response.body).to match(/foo is invalid/)
end

context 'when the parse method returns an InvalidValue' do
module ParamsScopeSpec
class AnotherCustomType
attr_reader :value
def self.parse(value)
case value
when 'invalid with message'
Grape::Validations::Types::InvalidValue.new "is not correct"
when 'invalid without message'
Grape::Validations::Types::InvalidValue.new
else
new(value)
end
end

def initialize(value)
@value = value
end
end
end

context 'with a message' do
it 'fails with the InvalidValue\'s error message' do
subject.params do
requires :foo, type: ParamsScopeSpec::AnotherCustomType
end
subject.get('/types') { params[:foo].value }

get '/types', foo: 'invalid with message'
expect(last_response.status).to eq(400)
expect(last_response.body).to match(/foo is not correct/)
end
end

context 'without a message' do
it 'fails with the default coercion failure message' do
subject.params do
requires :foo, type: ParamsScopeSpec::AnotherCustomType
end
subject.get('/types') { params[:foo].value }

get '/types', foo: 'invalid without message'
expect(last_response.status).to eq(400)
expect(last_response.body).to match(/foo is invalid/)
end
end
end
end

context 'array without coerce type explicitly given' do
Expand Down

0 comments on commit bfe9704

Please sign in to comment.