Class: Grape::Validations::CoerceValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/grape/validations/validators/coerce.rb

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

convert_to_short_name, #fail_fast?, inherited, #message, #options_key?, #validate, #validate!

Constructor Details

#initialize(*_args) ⇒ CoerceValidator

Returns a new instance of CoerceValidator


8
9
10
11
# File 'lib/grape/validations/validators/coerce.rb', line 8

def initialize(*_args)
  super
  @converter = Types.build_coercer(type, @option[:method])
end

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object


13
14
15
16
17
18
# File 'lib/grape/validations/validators/coerce.rb', line 13

def validate_param!(attr_name, params)
  raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:coerce) unless params.is_a? Hash
  new_value = coerce_value(params[attr_name])
  raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:coerce) unless valid_type?(new_value)
  params[attr_name] = new_value
end