Overview

Module for code related to grape's system for
coercion and type validation of incoming request
parameters.

Grape uses a number of tests and assertions to
work out exactly how a parameter should be handled,
based on the +type+ and +coerce_with+ options that
may be supplied to Dsl::Parameters#requires
and Dsl::Parameters#optional. The main
entry point for this process is Types.build_coercer.

Is the declared type in fact an array of multiple allowed types? For example the declaration +types: [Integer,String]+ will attempt first to coerce given values to integer, but will also accept any other string.

# File 'lib/grape/validations/types/build_coercer.rb', line 22defself.build_coercer(type,method=nil)# Accept pre-rolled virtus attributes without interferencereturntypeiftype.is_a?Virtus::Attributeconverter_options={nullify_blank:true}conversion_type=ifmethod==JSONObject# because we want just parsed JSON content:# if type is Array and data is `"{}"`# result will be [] because Virtus converts hashes# to arrayselsetypeend# Use a special coercer for multiply-typed parameters.ifTypes.multiple?(type)converter_options[:coercer]=Types::MultipleTypeCoercer.new(type,method)conversion_type=Object# Use a special coercer for custom types and coercion methods.elsifmethod||Types.custom?(type)converter_options[:coercer]=Types::CustomTypeCoercer.new(type,method)# Grape swaps in its own Virtus::Attribute implementations# for certain special types that merit first-class support# (but not if a custom coercion method has been supplied).elsifTypes.special?(type)conversion_type=Types::SPECIAL[type]end# Virtus will infer coercion and validation rules# for many common ruby types.Virtus::Attribute.build(conversion_type,converter_options)end

.custom?(type) ⇒ Boolean

A valid custom type must implement a class-level parse method, taking
one String argument and returning the parsed value in its correct type.

.multiple?(type) ⇒ Boolean

Is the declared type in fact an array of multiple allowed types?
For example the declaration +types: [Integer,String]+ will attempt
first to coerce given values to integer, but will also accept any
other string.