Class: Grape::Validations::CoerceValidator
- Defined in:
- lib/grape/validations/validators/coerce.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(*_args) ⇒ CoerceValidator
constructor
A new instance of CoerceValidator.
- #validate_param!(attr_name, params) ⇒ Object
Methods inherited from Base
convert_to_short_name, 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 19 20 21 |
# File 'lib/grape/validations/validators/coerce.rb', line 13 def validate_param!(attr_name, params) fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: (:coerce) unless params.is_a? Hash new_value = coerce_value(params[attr_name]) if valid_type?(new_value) params[attr_name] = new_value else fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: (:coerce) end end |