class Grape::Validations::Types::PrimitiveCoercer
that it has the proper type.
initialization. When strict is true, it doesn’t coerce a value but check
Coerces the given value to a type defined via a type argument during
def call(val)
def call(val) return InvalidValue.new if reject?(val) return nil if val.nil? || treat_as_nil?(val) super end
def initialize(type, strict = false)
def initialize(type, strict = false) super @type = type @coercer = (strict ? STRICT_MAPPING : MAPPING).fetch(type) do scope.const_get(type.name, false) rescue NameError raise ArgumentError, "type #{type} should support coercion via `[]`" unless type.respond_to?(:[]) type end end
def reject?(val)
but Virtus wouldn't accept it. So, this method only exists to not introduce
dry-types is ok to convert an array or a hash to a string, it is supported,
This method maintains logic which was defined by Virtus. For example,
def reject?(val) (val.is_a?(Array) && type == String) || (val.is_a?(String) && type == Hash) || (val.is_a?(Hash) && type == String) end
def treat_as_nil?(val)
absence of a value and coerces it into nil. See a discussion there
Dry-Types treats an empty string as invalid. However, Grape considers an empty string as
def treat_as_nil?(val) val == '' && type != String end