|
| 1 | +require_relative 'types/build_coercer' |
| 2 | +require_relative 'types/custom_type_coercer' |
| 3 | +require_relative 'types/json' |
| 4 | +require_relative 'types/file' |
| 5 | + |
| 6 | +# Patch for Virtus::Attribute::Collection |
| 7 | +# See the file for more details |
| 8 | +require_relative 'types/virtus_collection_patch' |
| 9 | + |
| 10 | +module Grape |
| 11 | + module Validations |
| 12 | + # Module for code related to grape's system for |
| 13 | + # coercion and type validation of incoming request |
| 14 | + # parameters. |
| 15 | + # |
| 16 | + # Grape uses a number of tests and assertions to |
| 17 | + # work out exactly how a parameter should be handled, |
| 18 | + # based on the +type+ and +coerce_with+ options that |
| 19 | + # may be supplied to {Grape::Dsl::Parameters#requires} |
| 20 | + # and {Grape::Dsl::Parameters#optional}. The main |
| 21 | + # entry point for this process is {Types.build_coercer}. |
| 22 | + module Types |
| 23 | + # Types representing a single value, which are coerced through Virtus |
| 24 | + # or special logic in Grape. |
| 25 | + PRIMITIVES = [ |
| 26 | + # Numerical |
| 27 | + Integer, |
| 28 | + Float, |
| 29 | + BigDecimal, |
| 30 | + Numeric, |
| 31 | + |
| 32 | + # Date/time |
| 33 | + Date, |
| 34 | + DateTime, |
| 35 | + Time, |
| 36 | + |
| 37 | + # Misc |
| 38 | + Virtus::Attribute::Boolean, |
| 39 | + String, |
| 40 | + Symbol, |
| 41 | + Rack::Multipart::UploadedFile |
| 42 | + ] |
| 43 | + |
| 44 | + # Types representing data structures. |
| 45 | + STRUCTURES = [ |
| 46 | + Hash, |
| 47 | + Array, |
| 48 | + Set |
| 49 | + ] |
| 50 | + |
| 51 | + # Types for which Grape provides special coercion |
| 52 | + # and type-checking logic. |
| 53 | + SPECIAL = { |
| 54 | + JSON => Json, |
| 55 | + Array[JSON] => JsonArray, |
| 56 | + ::File => File, |
| 57 | + Rack::Multipart::UploadedFile => File |
| 58 | + } |
| 59 | + |
| 60 | + # Is the given class a primitive type as recognized by Grape? |
| 61 | + # |
| 62 | + # @param type [Class] type to check |
| 63 | + # @return [Boolean] whether or not the type is known by Grape as a valid |
| 64 | + # type for a single value |
| 65 | + def self.primitive?(type) |
| 66 | + PRIMITIVES.include?(type) |
| 67 | + end |
| 68 | + |
| 69 | + # Is the given class a standard data structure (collection or map) |
| 70 | + # as recognized by Grape? |
| 71 | + # |
| 72 | + # @param type [Class] type to check |
| 73 | + # @return [Boolean] whether or not the type is known by Grape as a valid |
| 74 | + # data structure type |
| 75 | + # @note This method does not yet consider 'complex types', which inherit |
| 76 | + # Virtus.model. |
| 77 | + def self.structure?(type) |
| 78 | + STRUCTURES.include?(type) |
| 79 | + end |
| 80 | + |
| 81 | + # Does the given class implement a type system that Grape |
| 82 | + # (i.e. the underlying virtus attribute system) supports |
| 83 | + # out-of-the-box? Currently supported are +axiom-types+ |
| 84 | + # and +virtus+. |
| 85 | + # |
| 86 | + # The type will be passed to +Virtus::Attribute.build+, |
| 87 | + # and the resulting attribute object will be expected to |
| 88 | + # respond correctly to +coerce+ and +value_coerced?+. |
| 89 | + # |
| 90 | + # @param type [Class] type to check |
| 91 | + # @return [Boolean] +true+ where the type is recognized |
| 92 | + def self.recognized?(type) |
| 93 | + return false if type.is_a?(Array) || type.is_a?(Set) |
| 94 | + |
| 95 | + type.is_a?(Virtus::Attribute) || |
| 96 | + type.ancestors.include?(Axiom::Types::Type) || |
| 97 | + type.include?(Virtus::Model::Core) |
| 98 | + end |
| 99 | + |
| 100 | + # Does Grape provide special coercion and validation |
| 101 | + # routines for the given class? This does not include |
| 102 | + # automatic handling for primitives, structures and |
| 103 | + # otherwise recognized types. See {Types::SPECIAL}. |
| 104 | + # |
| 105 | + # @param type [Class] type to check |
| 106 | + # @return [Boolean] +true+ if special routines are available |
| 107 | + def self.special?(type) |
| 108 | + SPECIAL.key? type |
| 109 | + end |
| 110 | + |
| 111 | + # A valid custom type must implement a class-level `parse` method, taking |
| 112 | + # one String argument and returning the parsed value in its correct type. |
| 113 | + # @param type [Class] type to check |
| 114 | + # @return [Boolean] whether or not the type can be used as a custom type |
| 115 | + def self.custom?(type) |
| 116 | + !primitive?(type) && |
| 117 | + !structure?(type) && |
| 118 | + !recognized?(type) && |
| 119 | + !special?(type) && |
| 120 | + type.respond_to?(:parse) && |
| 121 | + type.method(:parse).arity == 1 |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments