class Grape::Validations::CoerceValidator
Attributes
converter[R]
@!attribute [r] converter Object that will be used for parameter coercion and type checking.
See {Types.build_coercer}
@return [Virtus::Attribute]
Public Class Methods
new(*_args)
click to toggle source
Calls superclass method
Grape::Validations::Base.new
# File lib/grape/validations/validators/coerce.rb, line 8 def initialize(*_args) super @converter = Types.build_coercer(type, @option[:method]) end
Public Instance Methods
validate(request)
click to toggle source
Calls superclass method
Grape::Validations::Base#validate
# File lib/grape/validations/validators/coerce.rb, line 13 def validate(request) super end
validate_param!(attr_name, params)
click to toggle source
# File lib/grape/validations/validators/coerce.rb, line 17 def validate_param!(attr_name, params) raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:coerce) unless params.is_a? Hash return unless requires_coercion?(params[attr_name]) 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
Private Instance Methods
coerce_value(val)
click to toggle source
# File lib/grape/validations/validators/coerce.rb, line 45 def coerce_value(val) # Don't coerce things other than nil to Arrays or Hashes unless (@option[:method] && !val.nil?) || type.is_a?(Virtus::Attribute) return val || [] if type == Array return val || Set.new if type == Set return val || {} if type == Hash end converter.coerce(val) # not the prettiest but some invalid coercion can currently trigger # errors in Virtus (see coerce_spec.rb:75) rescue Types::InvalidValue.new end
requires_coercion?(value)
click to toggle source
# File lib/grape/validations/validators/coerce.rb, line 68 def requires_coercion?(value) # JSON types do not require coercion if value is valid !valid_type?(value) || converter.coercer.respond_to?(:method) && !converter.is_a?(Grape::Validations::Types::Json) end
type()
click to toggle source
Type to which the parameter will be coerced.
@return [Class]
# File lib/grape/validations/validators/coerce.rb, line 64 def type @option[:type].is_a?(Hash) ? @option[:type][:value] : @option[:type] end
valid_type?(val)
click to toggle source
# File lib/grape/validations/validators/coerce.rb, line 35 def valid_type?(val) # Special value to denote coercion failure return false if val.instance_of?(Types::InvalidValue) # Allow nil, to ignore when a parameter is absent return true if val.nil? converter.value_coerced? val end