class AFCSalesforce::Tools::Validation::Rule::Length

Length rule

Public Class Methods

new(params) click to toggle source

params can be any of the following:

- :minimum - at least this many chars
- :maximum - at most this many chars
- :exact - exactly this many chars

Example:

{minimum: 3, maximum: 10}
{exact: 10}
# File lib/afc_salesforce/tools/validation/rule/length.rb, line 17
def initialize(params)
  @params = params
end

Public Instance Methods

error(value) click to toggle source
# File lib/afc_salesforce/tools/validation/rule/length.rb, line 27
def error(value)
  results = {}
  results[:expected] = @params
  results[:got] = value.length
  results
end
error_key() click to toggle source
# File lib/afc_salesforce/tools/validation/rule/length.rb, line 45
def error_key
  :length
end
params() click to toggle source

returns the params given in the constructor

# File lib/afc_salesforce/tools/validation/rule/length.rb, line 22
def params
  @params
end
valid_value?(value) click to toggle source

determines if value is valid according to the constructor params

# File lib/afc_salesforce/tools/validation/rule/length.rb, line 35
def valid_value?(value)
  @params.each_pair do |key, param|
    return false if key == :minimum && (value.nil? || value.length < param)
    return false if key == :maximum && !value.nil? && value.length > param
    return false if key == :exact && (value.nil? || value.length != param)
  end

  true
end