Class: AFCSalesforce::Tools::Validation::Rule::Length

Inherits:
Object
  • Object
show all
Defined in:
lib/afc_salesforce/tools/validation/rule/length.rb

Overview

Length rule

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Length

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}


17
18
19
# File 'lib/afc_salesforce/tools/validation/rule/length.rb', line 17

def initialize(params)
  @params = params
end

Instance Method Details

#error(value) ⇒ Object



27
28
29
30
31
32
# 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_keyObject



45
46
47
# File 'lib/afc_salesforce/tools/validation/rule/length.rb', line 45

def error_key
  :length
end

#paramsObject

returns the params given in the constructor



22
23
24
# File 'lib/afc_salesforce/tools/validation/rule/length.rb', line 22

def params
  @params
end

#valid_value?(value) ⇒ Boolean

determines if value is valid according to the constructor params

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# 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