Sha256: b43da35fe10a1bd9b10d71d3827e4cd4959b83e246e4f85e8a35faad9d0a1a6e
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
# frozen_string_literal: true require_relative 'dry_type_coercer' module Grape module Validations module Types # Coerces the given value to a type defined via a +type+ argument during # initialization. class PrimitiveCoercer < DryTypeCoercer MAPPING = { Grape::API::Boolean => DryTypes::Params::Bool, # unfortunatelly, a +Params+ scope doesn't contain String String => DryTypes::Coercible::String }.freeze STRICT_MAPPING = { Grape::API::Boolean => DryTypes::Strict::Bool }.freeze def initialize(type, strict = false) super @type = type @coercer = if strict STRICT_MAPPING.fetch(type) { scope.const_get(type.name) } else MAPPING.fetch(type) { scope.const_get(type.name) } end end def call(val) return InvalidValue.new if reject?(val) return nil if val.nil? return '' if val == '' super end protected attr_reader :type # This method maintaine logic which was defined by Virtus. For example, # dry-types is ok to convert an array or a hash to a string, it is supported, # but Virtus wouldn't accept it. So, this method only exists to not introduce # breaking changes. def reject?(val) (val.is_a?(Array) && type == String) || (val.is_a?(String) && type == Hash) || (val.is_a?(Hash) && type == String) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
grape-1.3.0 | lib/grape/validations/types/primitive_coercer.rb |