Class: Grape::Validations::CoerceValidator

Inherits:
SingleOptionValidator show all
Defined in:
lib/grape/validations/coerce.rb

Defined Under Namespace

Classes: InvalidValue

Instance Attribute Summary

Attributes inherited from Validator

#attrs

Instance Method Summary (collapse)

Methods inherited from SingleOptionValidator

#initialize

Methods inherited from Validator

inherited, #initialize, #validate!

Constructor Details

This class inherits a constructor from Grape::Validations::SingleOptionValidator

Instance Method Details

- (Object) validate_param!(attr_name, params)



8
9
10
11
12
13
14
15
16
# File 'lib/grape/validations/coerce.rb', line 8

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