Module: Fend::Plugins::ValidationHelpers::ParamMethods

Defined in:
lib/fend/plugins/validation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_error(*args) ⇒ Object

:nodoc:



227
228
229
230
231
232
233
# File 'lib/fend/plugins/validation_helpers.rb', line 227

def add_error(*args)
  if args.size == 1 && args.first.is_a?(String)
    super(*args)
  else
    @errors << error_message(*args)
  end
end

#validate_absence(opts = {}) ⇒ Object

Validates that param value is blank. To see what values are considered as blank, check ValueHelpers::ParamMethods#blank?.

id.validate_absence


80
81
82
# File 'lib/fend/plugins/validation_helpers.rb', line 80

def validate_absence(opts = {})
  add_error(:absence, opts[:message]) if present?
end

#validate_acceptance(opts = {}) ⇒ Object

Validates acceptance. Potential use case would be checking if Terms of Service has been accepted.

By default, validation will pass if value is one of: ‘[1, “1”, :true, true, “true”, “TRUE”, :yes, “YES”, “yes”]`

You can pass the ‘:as` option with custom list of acceptable values:

tos.validate_acceptance(as: ["Agreed", "OK"])


93
94
95
96
97
# File 'lib/fend/plugins/validation_helpers.rb', line 93

def validate_acceptance(opts = {})
  as = Array(opts.fetch(:as, ACCEPTABLE))

  add_error(:acceptance, opts[:message]) unless as.include?(value)
end

#validate_equality(rhs, opts = {}) ⇒ Object

Validates that param value is equal to the specified value.

color.validate_equality("black")


102
103
104
# File 'lib/fend/plugins/validation_helpers.rb', line 102

def validate_equality(rhs, opts = {})
  add_error(:equality, opts[:message], rhs) unless value.eql?(rhs)
end

#validate_exact_length(exact_length, opts = {}) ⇒ Object

Validates that param value length is equal to the specified value. Works with any object that responds to ‘#length` method.

code.validate_exact_length(10)


110
111
112
113
114
115
116
# File 'lib/fend/plugins/validation_helpers.rb', line 110

def validate_exact_length(exact_length, opts = {})
  value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE

  return if !value_length.eql?(UNSUPPORTED_TYPE) && value_length.eql?(exact_length)

  add_error(:exact_length, opts[:message], exact_length)
end

#validate_exclusion(exclude_from, opts = {}) ⇒ Object

Validates that param value is not one of the specified values.

.validate_exclusion(["admin", "editor"])


121
122
123
# File 'lib/fend/plugins/validation_helpers.rb', line 121

def validate_exclusion(exclude_from, opts = {})
  add_error(:exclusion, opts[:message], exclude_from) if exclude_from.include?(value)
end

#validate_format(format, opts = {}) ⇒ Object

Validates that param value is a match for specified regex.

name.validate_format(/\A[a-z]\z/i)


128
129
130
# File 'lib/fend/plugins/validation_helpers.rb', line 128

def validate_format(format, opts = {})
  add_error(:format, opts[:message]) if format.match(value.to_s).nil?
end

#validate_greater_than(rhs, opts = {}) ⇒ Object

Validates that param value is greater than specified value

age.validate_greater_than(18)


135
136
137
# File 'lib/fend/plugins/validation_helpers.rb', line 135

def validate_greater_than(rhs, opts = {})
  add_error(:greater_than, opts[:message], rhs) unless value.is_a?(Numeric) && value > rhs
end

#validate_greater_than_or_equal_to(rhs, opts = {}) ⇒ Object Also known as: validate_gteq

Validates that param value is greater than or equal to specified value

age.validate_greater_than_or_equal_to(18)

Aliased as ‘validate_gteq`

age.validate_gteq(10)


146
147
148
# File 'lib/fend/plugins/validation_helpers.rb', line 146

def validate_greater_than_or_equal_to(rhs, opts = {})
  add_error(:greater_than_or_equal_to, opts[:message], rhs) unless value.is_a?(Numeric) && value >= rhs
end

#validate_inclusion(include_in, opts = {}) ⇒ Object

Validates that param value is one of the specified values.

.validate_inclusion(["admin", "editor"])


154
155
156
# File 'lib/fend/plugins/validation_helpers.rb', line 154

def validate_inclusion(include_in, opts = {})
  add_error(:inclusion, opts[:message], include_in) unless include_in.include?(value)
end

#validate_length_range(range, opts = {}) ⇒ Object

Validates that param value length is within specified range

code.validate_length_range(10..15)


161
162
163
164
165
166
167
# File 'lib/fend/plugins/validation_helpers.rb', line 161

def validate_length_range(range, opts = {})
  value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE

  return if !value_length.eql?(UNSUPPORTED_TYPE) && range.include?(value_length)

  add_error(:length_range, opts[:message], range)
end

#validate_less_than(rhs, opts = {}) ⇒ Object

Validates that param value is less than specified value

funds.validate_less_than(100)


172
173
174
# File 'lib/fend/plugins/validation_helpers.rb', line 172

def validate_less_than(rhs, opts = {})
  add_error(:less_than, opts[:message], rhs) unless value.is_a?(Numeric) && value < rhs
end

#validate_less_than_or_equal_to(rhs, opts = {}) ⇒ Object Also known as: validate_lteq

Validates that param value is less than or equal to specified value

funds.validate_less_than_or_equal_to(100)

Aliased as ‘validate_lteq`

funds.validate_lteq(100)


183
184
185
# File 'lib/fend/plugins/validation_helpers.rb', line 183

def validate_less_than_or_equal_to(rhs, opts = {})
  add_error(:less_than_or_equal_to, opts[:message], rhs) unless value.is_a?(Numeric) && value <= rhs
end

#validate_max_length(length, opts = {}) ⇒ Object

Validates that param value length is not greater than specified value

password.validate_max_length(15)


191
192
193
194
195
196
197
# File 'lib/fend/plugins/validation_helpers.rb', line 191

def validate_max_length(length, opts = {})
  value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE

  return if !value_length.eql?(UNSUPPORTED_TYPE) && value_length <= length

  add_error(:max_length, opts[:message], length)
end

#validate_min_length(length, opts = {}) ⇒ Object

Validates that param value length is not less than specified value

password.validate_min_length(5)


202
203
204
205
206
207
208
# File 'lib/fend/plugins/validation_helpers.rb', line 202

def validate_min_length(length, opts = {})
  value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE

  return if !value_length.eql?(UNSUPPORTED_TYPE) && value_length >= length

  add_error(:min_length, opts[:message], length)
end

#validate_presence(opts = {}) ⇒ Object

Validates that param value is present. To see what values are considered as present, check ValueHelpers::ParamMethods#present?

name.validate_presence


214
215
216
# File 'lib/fend/plugins/validation_helpers.rb', line 214

def validate_presence(opts = {})
  add_error(:presence, opts[:message]) if blank?
end

#validate_type(type, opts = {}) ⇒ Object

Uses ValueHelpers::ParamMethods#type_of? method to validate that param value is of specified type.

tags.validate_type(Array)


222
223
224
# File 'lib/fend/plugins/validation_helpers.rb', line 222

def validate_type(type, opts = {})
  add_error(:type, opts[:message], type) unless type_of?(type)
end