Module Grape::Validations::Types
In: lib/grape/validations/types.rb
lib/grape/validations/types/file.rb
lib/grape/validations/types/build_coercer.rb
lib/grape/validations/types/custom_type_collection_coercer.rb
lib/grape/validations/types/json.rb
lib/grape/validations/types/custom_type_coercer.rb
lib/grape/validations/types/multiple_type_coercer.rb
lib/grape/validations/types/variant_collection_coercer.rb

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 {Grape::Dsl::Parameters#requires} and {Grape::Dsl::Parameters#optional}. The main entry point for this process is {Types.build_coercer}.

Methods

Classes and Modules

Class Grape::Validations::Types::CustomTypeCoercer
Class Grape::Validations::Types::CustomTypeCollectionCoercer
Class Grape::Validations::Types::File
Class Grape::Validations::Types::InvalidValue
Class Grape::Validations::Types::Json
Class Grape::Validations::Types::JsonArray
Class Grape::Validations::Types::MultipleTypeCoercer
Class Grape::Validations::Types::VariantCollectionCoercer

Constants

PRIMITIVES = [ # Numerical Integer, Float, BigDecimal, Numeric, # Date/time Date, DateTime, Time, # Misc Virtus::Attribute::Boolean, String, Symbol, Rack::Multipart::UploadedFile   Types representing a single value, which are coerced through Virtus or special logic in Grape.
STRUCTURES = [ Hash, Array, Set   Types representing data structures.
SPECIAL = { JSON => Json, Array[JSON] => JsonArray, ::File => File, Rack::Multipart::UploadedFile => File   Types for which Grape provides special coercion and type-checking logic.
GROUPS = [ Array, Hash, JSON, Array[JSON]

Public Class methods

Work out the +Virtus::Attribute+ object to use for coercing strings to the given type. Coercion method will be inferred if none is supplied.

If a +Virtus::Attribute+ object already built with +Virtus::Attribute.build+ is supplied as the type it will be returned and method will be ignored.

See {CustomTypeCoercer} for further details about coercion and type-checking inference.

@param type [Class] the type to which input strings

  should be coerced

@param method [Class,call] the coercion method to use @return [Virtus::Attribute] object to be used

  for coercion and type validation

Is the declared type an Array or Set of a {custom?} type?

@param type [Array<Class>,Class] type to check @return [Boolean] true if type is a collection of a type that implements

  its own +#parse+ method.

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

@param type [Class] type to check @return [Boolean] whether or not the type can be used as a custom type

Is the declared type a supported group type? Currently supported group types are Array, Hash, JSON, and Array[JSON]

@param type [Array<Class>,Class] type to check @return [Boolean] true if the type is a supported group type

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.

@param type [Array<Class>,Set<Class>] type (or type list!) to check @return [Boolean] true if the given value will be treated as

  a list of types.

Is the given class a primitive type as recognized by Grape?

@param type [Class] type to check @return [Boolean] whether or not the type is known by Grape as a valid

  type for a single value

Does the given class implement a type system that Grape (i.e. the underlying virtus attribute system) supports out-of-the-box? Currently supported are +axiom-types+ and virtus.

The type will be passed to +Virtus::Attribute.build+, and the resulting attribute object will be expected to respond correctly to coerce and +value_coerced?+.

@param type [Class] type to check @return [Boolean] true where the type is recognized

Does Grape provide special coercion and validation routines for the given class? This does not include automatic handling for primitives, structures and otherwise recognized types. See {Types::SPECIAL}.

@param type [Class] type to check @return [Boolean] true if special routines are available

Is the given class a standard data structure (collection or map) as recognized by Grape?

@param type [Class] type to check @return [Boolean] whether or not the type is known by Grape as a valid

  data structure type

@note This method does not yet consider ‘complex types’, which inherit

  Virtus.model.

[Validate]