File: coerce.rb

package info (click to toggle)
ruby-grape 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,032 kB
  • sloc: ruby: 24,329; makefile: 6
file content (88 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

module Grape
  class API
    class Boolean
      def self.build(val)
        return nil if val != true && val != false

        new
      end
    end

    class Instance
      Boolean = Grape::API::Boolean
    end
  end

  module Validations
    class CoerceValidator < Base
      def initialize(*_args)
        super

        @converter = if type.is_a?(Grape::Validations::Types::VariantCollectionCoercer)
                       type
                     else
                       Types.build_coercer(type, method: @option[:method])
                     end
      end

      def validate(request)
        super
      end

      def validate_param!(attr_name, params)
        raise validation_exception(attr_name) unless params.is_a? Hash

        new_value = coerce_value(params[attr_name])

        raise validation_exception(attr_name) unless valid_type?(new_value)

        # Don't assign a value if it is identical. It fixes a problem with Hashie::Mash
        # which looses wrappers for hashes and arrays after reassigning values
        #
        #     h = Hashie::Mash.new(list: [1, 2, 3, 4])
        #     => #<Hashie::Mash list=#<Hashie::Array [1, 2, 3, 4]>>
        #     list = h.list
        #     h[:list] = list
        #     h
        #     => #<Hashie::Mash list=[1, 2, 3, 4]>
        return if params[attr_name].class == new_value.class && params[attr_name] == new_value

        params[attr_name] = new_value
      end

      private

      # @!attribute [r] converter
      # Object that will be used for parameter coercion and type checking.
      #
      # See {Types.build_coercer}
      #
      # @return [Object]
      attr_reader :converter

      def valid_type?(val)
        !val.is_a?(Types::InvalidValue)
      end

      def coerce_value(val)
        converter.call(val)
      # Some custom types might fail, so it should be treated as an invalid value
      rescue StandardError
        Types::InvalidValue.new
      end

      # Type to which the parameter will be coerced.
      #
      # @return [Class]
      def type
        @option[:type].is_a?(Hash) ? @option[:type][:value] : @option[:type]
      end

      def validation_exception(attr_name)
        Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:coerce))
      end
    end
  end
end