class Grape::Validations::Types::ArrayCoercer
maintains Virtus behavior in coercing.
behavior of Virtus which was used earlier, a ‘Grape::Validations::Types::PrimitiveCoercer`
provided by dry-types. Unfortunately, it doesn’t work for Grape because of
method (dry-rb.org/gems/dry-types/1.2/array-with-member/)
It could’ve been possible to use an of
an array of arrays of integers.
Coerces elements in an array. It might be an array of strings or integers or
def call(_val)
def call(_val) collection = super return collection if collection.is_a?(InvalidValue) coerce_elements collection end
def coerce_elements(collection)
def coerce_elements(collection) return if collection.nil? collection.each_with_index do |elem, index| return InvalidValue.new if reject?(elem) coerced_elem = elem_coercer.call(elem) return coerced_elem if coerced_elem.is_a?(InvalidValue) collection[index] = coerced_elem end collection end
def elem_coercer
def elem_coercer @elem_coercer ||= DryTypeCoercer.coercer_instance_for(subtype, strict) end
def initialize(type, strict = false)
def initialize(type, strict = false) super @coercer = scope::Array @subtype = type.first end
def reject?(val)
This method maintains logic which was defined by Virtus for arrays.
def reject?(val) val.nil? end