Class: ParametersValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/parameters_validator.rb

Overview

Validates that attribute's value is Array which is the only valid type signature for serialized parameters.

Constant Summary collapse

TYPE_SIGNATURE_SENTENCE =

Sentence explaining the valid type signature for parameters.

'Valid parameters are an Array<Array(String, String)>.'

Instance Method Summary collapse

Instance Method Details

#error_at(options = {}) ⇒ String (private)

Generates error message for element at the given index. Prefix is prepened to #location_clause to make a sentence. TYPE_SIGNATURE_SENTENCE is appended to that sentence.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :element (Object)

    The element that has the error.

  • :index (Integer)

    The index of element in its parent Array.

  • :prefix (String)

    Specific error prefix to differentiate from other calls to #error_at.

Returns:

  • (String)

See Also:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/validators/parameters_validator.rb', line 89

def error_at(options={})
  options.assert_valid_keys(:element, :index, :prefix)
  prefix = options.fetch(:prefix)

  clause = location_clause(
      :element => options[:element],
      :index => options[:index]
  )
  sentence = "#{prefix} #{clause}."

  sentences = [
      sentence,
      TYPE_SIGNATURE_SENTENCE
  ]

  error = sentences.join("  ")

  error
end

#length_error_at(options = {}) ⇒ String (private)

Generates error message for too few or too many elements.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :element (Array)

    Array that has the wrong number of elements.

  • :extreme (:few, :many)

    whether :element has too :few or too :many child elements.

  • :index (Integer)

    index of :element in its parent Array.

Returns:

  • (String)

See Also:

  • ParametersValidator.{{#error_at}


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/validators/parameters_validator.rb', line 117

def length_error_at(options={})
  options.assert_valid_keys(:element, :extreme, :index)
  extreme = options.fetch(:extreme)

  prefix = "has too #{extreme} elements"
  error = error_at(
      :element => options[:element],
      :index => options[:index],
      :prefix => prefix
  )

  error
end

#location_clause(options = {}) ⇒ String (private)

Generates a clause with the location of element and its value.

Parameters:

  • options (Hash{Symbol => String,Integer}) (defaults to: {})

Options Hash (options):

  • :element (Object, #inspect)

    an element in a parent Array.

  • :index (Integer)

    index of :element in parent Array.

Returns:

  • (String)

    "at index ()"



137
138
139
140
141
142
143
144
145
146
# File 'app/validators/parameters_validator.rb', line 137

def location_clause(options={})
  options.assert_valid_keys(:element, :index)

  element = options.fetch(:element)
  index = options.fetch(:index)

  clause = "at index #{index} (#{element.inspect})"

  clause
end

#validate_each(record, attribute, value) ⇒ void

This method returns an undefined value.

Validates that attribute's value is Array which is the only valid type signature for serialized parameters. Errors are specific to the how different value is compared to correct format.

Parameters:

  • record (#errors, ActiveRecord::Base)

    ActiveModel or ActiveRecord

  • attribute (Symbol)

    serialized parameters attribute name.

  • value (Object, nil, Array, Array<Array>, Array<Array(String, String)>)

    serialized parameters.



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
# File 'app/validators/parameters_validator.rb', line 14

def validate_each(record, attribute, value)
  if value.is_a? Array
    value.each_with_index do |element, index|
      if element.is_a? Array
        if element.length != 2
          extreme = :few

          if element.length > 2
            extreme = :many
          end

          length_error = length_error_at(
              :extreme => extreme,
              :element => element,
              :index => index
          )

          record.errors[attribute] << length_error
        else
          parameter_name = element.first

          if parameter_name.is_a? String
            unless parameter_name.present?
              error = error_at(
                  :element => element,
                  :index => index,
                  :prefix => "has blank parameter name"
              )
              record.errors[attribute] << error
            end
          else
            error = error_at(
                :element => element,
                :index => index,
                :prefix => "has non-String parameter name (#{parameter_name.inspect})"
            )
            record.errors[attribute] << error
          end

          parameter_value = element.second

          unless parameter_value.is_a? String
            error = error_at(
                :element => element,
                :index => index,
                :prefix => "has non-String parameter value (#{parameter_value.inspect})"
            )
            record.errors[attribute] << error
          end
        end
      else
        error = error_at(
            :element => element,
            :index => index,
            :prefix => 'has non-Array'
        )
        record.errors[attribute] << error
      end
    end
  else
    record.errors[attribute] << "is not an Array.  #{TYPE_SIGNATURE_SENTENCE}"
  end
end