Class: Grape::Validations::Types::ArrayCoercer

Inherits:
DryTypeCoercer show all
Defined in:
lib/grape/validations/types/array_coercer.rb

Overview

Coerces elements in an array. It might be an array of strings or integers or an array of arrays of integers.

It could've been possible to use an +of+ method (https://dry-rb.org/gems/dry-types/1.2/array-with-member/) provided by dry-types. Unfortunately, it doesn't work for Grape because of behavior of Virtus which was used earlier, a Grape::Validations::Types::PrimitiveCoercer maintains Virtus behavior in coercing.

Direct Known Subclasses

SetCoercer

Instance Method Summary collapse

Methods inherited from DryTypeCoercer

coercer_instance_for, collection_coercer_for

Constructor Details

#initialize(type, strict = false) ⇒ ArrayCoercer

Returns a new instance of ArrayCoercer.



17
18
19
20
21
22
# File 'lib/grape/validations/types/array_coercer.rb', line 17

def initialize(type, strict = false)
  super

  @coercer = scope::Array
  @subtype = type.first
end

Instance Method Details

#call(_val) ⇒ Object



24
25
26
27
28
29
# File 'lib/grape/validations/types/array_coercer.rb', line 24

def call(_val)
  collection = super
  return collection if collection.is_a?(InvalidValue)

  coerce_elements collection
end