lib/grape/validations/validators/coerce.rb in grape-0.12.0 vs lib/grape/validations/validators/coerce.rb in grape-0.13.0

- old
+ new

@@ -39,11 +39,13 @@ val.is_a?(klass) end end def valid_type?(val) - if @option.is_a?(Array) || @option.is_a?(Set) + if val.instance_of?(InvalidValue) + false + elsif @option.is_a?(Array) || @option.is_a?(Set) _valid_array_type?(@option.first, val) else _valid_single_type?(@option, val) end end @@ -52,10 +54,17 @@ # Don't coerce things other than nil to Arrays or Hashes return val || [] if type == Array return val || Set.new if type == Set return val || {} if type == Hash - converter = Virtus::Attribute.build(type) + # To support custom types that Virtus can't easily coerce, pass in an + # explicit coercer. Custom types must implement a `parse` class method. + converter_options = {} + if ParameterTypes.custom_type?(type) + converter_options[:coercer] = type.method(:parse) + end + + converter = Virtus::Attribute.build(type, converter_options) converter.coerce(val) # not the prettiest but some invalid coercion can currently trigger # errors in Virtus (see coerce_spec.rb:75) rescue