Module: Grape::Validations::Types
- Defined in:
- lib/grape/validations/types.rb,
lib/grape/validations/types/file.rb,
lib/grape/validations/types/json.rb,
lib/grape/validations/types/set_coercer.rb,
lib/grape/validations/types/array_coercer.rb,
lib/grape/validations/types/invalid_value.rb,
lib/grape/validations/types/dry_type_coercer.rb,
lib/grape/validations/types/primitive_coercer.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,
lib/grape/validations/types/custom_type_collection_coercer.rb
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.
Defined Under Namespace
Classes: ArrayCoercer, CoercerCache, CustomTypeCoercer, CustomTypeCollectionCoercer, DryTypeCoercer, File, InvalidValue, Json, JsonArray, MultipleTypeCoercer, PrimitiveCoercer, SetCoercer, VariantCollectionCoercer
Constant Summary collapse
- PRIMITIVES =
[ # Numerical Integer, Float, BigDecimal, Numeric, # Date/time Date, DateTime, Time, # Misc Grape::API::Boolean, String, Symbol, TrueClass, FalseClass ].freeze
- STRUCTURES =
Types representing data structures.
[Hash, Array, Set].freeze
- SPECIAL =
{ ::JSON => Json, Array[JSON] => JsonArray, ::File => File, Rack::Multipart::UploadedFile => File }.freeze
- GROUPS =
[Array, Hash, JSON, Array[JSON]].freeze
Class Method Summary collapse
- .array_or_set?(type) ⇒ Boolean
-
.build_coercer(type, method: nil, strict: false) ⇒ Object
Chooses the best coercer for the given type.
-
.collection_of_custom?(type) ⇒ Boolean
Is the declared type an
ArrayorSetof a #custom? type?. - .create_coercer_instance(type, method, strict) ⇒ Object
-
.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.
-
.group?(type) ⇒ Boolean
Is the declared type a supported group type? Currently supported group types are Array, Hash, JSON, and Array.
- .map_special(type) ⇒ Object
-
.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.
-
.primitive?(type) ⇒ Boolean
Is the given class a primitive type as recognized by Grape?.
-
.special?(type) ⇒ Boolean
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.
-
.structure?(type) ⇒ Boolean
Is the given class a standard data structure (collection or map) as recognized by Grape?.
Class Method Details
.array_or_set?(type) ⇒ Boolean
101 102 103 |
# File 'lib/grape/validations/types.rb', line 101 def array_or_set?(type) type.is_a?(Array) || type.is_a?(Set) end |
.build_coercer(type, method: nil, strict: false) ⇒ Object
Chooses the best coercer for the given type. For example, if the type is Integer, it will return a coercer which will be able to coerce a value to the integer.
There are a few very special coercers which might be returned.
Grape::Types::MultipleTypeCoercer is a coercer which is returned when the given type implies values in an array with different types. For example, [Integer, String] allows integer and string values in an array.
Grape::Types::CustomTypeCoercer is a coercer which is returned when a method is specified by a user with coerce_with option or the user specifies a custom type which implements requirments of Grape::Types::CustomTypeCoercer.
Grape::Types::CustomTypeCollectionCoercer is a very similar to the previous one, but it expects an array or set of values having a custom type implemented by the user.
There is also a group of custom types implemented by Grape, check Grape::Validations::Types::SPECIAL to get the full list.
156 157 158 159 160 161 |
# File 'lib/grape/validations/types.rb', line 156 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 |
.collection_of_custom?(type) ⇒ Boolean
Is the declared type an Array or Set of a #custom? type?
120 121 122 |
# File 'lib/grape/validations/types.rb', line 120 def collection_of_custom?(type) array_or_set?(type) && type.length == 1 && (custom?(type.first) || special?(type.first)) end |
.create_coercer_instance(type, method, strict) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/grape/validations/types.rb', line 163 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 |
.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.
110 111 112 113 |
# File 'lib/grape/validations/types.rb', line 110 def custom?(type) !(primitive?(type) || structure?(type) || multiple?(type)) && type.respond_to?(:parse) && type.method(:parse).arity == 1 end |
.group?(type) ⇒ Boolean
Is the declared type a supported group type? Currently supported group types are Array, Hash, JSON, and Array
97 98 99 |
# File 'lib/grape/validations/types.rb', line 97 def group?(type) GROUPS.include? type end |
.map_special(type) ⇒ Object
124 125 126 |
# File 'lib/grape/validations/types.rb', line 124 def map_special(type) SPECIAL.fetch(type, type) end |
.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.
77 78 79 |
# File 'lib/grape/validations/types.rb', line 77 def multiple?(type) array_or_set?(type) && type.size > 1 end |
.primitive?(type) ⇒ Boolean
Is the given class a primitive type as recognized by Grape?
55 56 57 |
# File 'lib/grape/validations/types.rb', line 55 def primitive?(type) PRIMITIVES.include?(type) end |
.special?(type) ⇒ Boolean
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 SPECIAL.
88 89 90 |
# File 'lib/grape/validations/types.rb', line 88 def special?(type) SPECIAL.key? type end |
.structure?(type) ⇒ Boolean
Is the given class a standard data structure (collection or map) as recognized by Grape?
65 66 67 |
# File 'lib/grape/validations/types.rb', line 65 def structure?(type) STRUCTURES.include?(type) end |