Class: ParametersValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ParametersValidator
- Defined in:
- app/validators/parameters_validator.rb
Overview
Validates that attribute's value is Array
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
-
#error_at(options = {}) ⇒ String
private
Generates error message for element at the given index.
-
#length_error_at(options = {}) ⇒ String
private
Generates error message for too few or too many elements.
-
#location_clause(options = {}) ⇒ String
private
Generates a clause with the location of element and its value.
-
#validate_each(record, attribute, value) ⇒ void
Validates that
attribute
'svalue
onrecord
isArray<Array(String, String)>
which is the only valid type signature for serialized parameters.
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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/validators/parameters_validator.rb', line 97 def error_at(={}) .assert_valid_keys(:element, :index, :prefix) prefix = .fetch(:prefix) clause = location_clause( :element => [:element], :index => [: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.
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/validators/parameters_validator.rb', line 125 def length_error_at(={}) .assert_valid_keys(:element, :extreme, :index) extreme = .fetch(:extreme) prefix = "has too #{extreme} elements" error = error_at( :element => [:element], :index => [:index], :prefix => prefix ) error end |
#location_clause(options = {}) ⇒ String (private)
Generates a clause with the location of element and its value.
145 146 147 148 149 150 151 152 153 154 |
# File 'app/validators/parameters_validator.rb', line 145 def location_clause(={}) .assert_valid_keys(:element, :index) element = .fetch(:element) index = .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
on record
is Array<Array(String, String)>
which is the only valid type
signature for serialized parameters.
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 |
# File 'app/validators/parameters_validator.rb', line 22 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.add 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.add attribute, error end else error = error_at( :element => element, :index => index, :prefix => "has non-String parameter name (#{parameter_name.inspect})" ) record.errors.add 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.add attribute, error end end else error = error_at( :element => element, :index => index, :prefix => 'has non-Array' ) record.errors.add attribute, error end end else record.errors.add attribute, "is not an Array. #{TYPE_SIGNATURE_SENTENCE}" end end |