module Grape::Validations::Types
def array_or_set?(type)
def array_or_set?(type) type.is_a?(Array) || type.is_a?(Set) end
def build_coercer(type, method: nil, strict: false)
-
(Object)- object to be used
Parameters:
-
method(Class, #call) -- the coercion method to use -
type(Class) -- the type to which input strings
def build_coercer(type, method: nil, strict: false) # no cache since unique return create_coercer_instance(type, method, strict) if method.respond_to?(:call) CoercerCache[[type, method, strict]] end
def collection_of_custom?(type)
-
(Boolean)- true if +type+ is a collection of a type that implements
Parameters:
-
type(Array) -- type to check, Class
def collection_of_custom?(type) array_or_set?(type) && type.length == 1 && (custom?(type.first) || special?(type.first)) end
def create_coercer_instance(type, method, strict)
def create_coercer_instance(type, method, strict) # map_special doesn't recurse into collections — applied only to the top-level type here. type = map_special(type) # Multiply-typed parameters, e.g. types: [Integer, String]. return MultipleTypeCoercer.new(type, method) if multiple?(type) # User-supplied coercion method, or a custom type with its own #parse. return CustomTypeCoercer.new(type, method) if method || custom?(type) # Array/Set of a custom type — CustomTypeCoercer already handles single # custom types when an explicit coercion method is supplied. return CustomTypeCollectionCoercer.new(map_special(type.first), set: type.is_a?(Set)) if collection_of_custom?(type) # Fallback: let dry-types handle primitives, structures, and known specials. DryTypeCoercer.coercer_instance_for(type, strict:) end
def custom?(type)
-
(Boolean)- whether or not the type can be used as a custom type
Parameters:
-
type(Class) -- type to check
def custom?(type) !(primitive?(type) || structure?(type) || multiple?(type)) && type.respond_to?(:parse) && type.method(:parse).arity == 1 end
def group?(type)
-
(Boolean)- +true+ if the type is a supported group type
Parameters:
-
type(Array) -- type to check, Class
def group?(type) GROUPS.include? type end
def map_special(type)
def map_special(type) SPECIAL.fetch(type, type) end
def multiple?(type)
-
(Boolean)- +true+ if the given value will be treated as
Parameters:
-
type(Array) -- type (or type list!) to check, Set
def multiple?(type) array_or_set?(type) && type.size > 1 end
def primitive?(type)
-
(Boolean)- whether or not the type is known by Grape as a valid
Parameters:
-
type(Class) -- type to check
def primitive?(type) PRIMITIVES.include?(type) end
def special?(type)
-
(Boolean)- +true+ if special routines are available
Parameters:
-
type(Class) -- type to check
def special?(type) SPECIAL.key? type end
def structure?(type)
-
(Boolean)- whether or not the type is known by Grape as a valid
Parameters:
-
type(Class) -- type to check
def structure?(type) STRUCTURES.include?(type) end