Module: Echosign::Validatable

Instance Method Summary collapse

Instance Method Details

#require_exactly_one(field_group, params) ⇒ Object

Raises:


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/echosign/validatable.rb', line 12

def require_exactly_one(field_group, params)
  set_fields = 0
  field_group.each do |field|
    begin
      validate_field(field, params)
    rescue RequiredParameterError
      next
    else
      set_fields += 1
    end
  end
  raise ParameterError, "Exactly one of #{field_group} should be present" if set_fields != 1
end

#require_keys(required_fields, params) ⇒ Object


6
7
8
9
10
# File 'lib/echosign/validatable.rb', line 6

def require_keys(required_fields, params)
  required_fields.each do |field|
    validate_field(field, params)
  end
end

#validate_field(field, params) ⇒ Object

TODO (bernardworthy) A validator accepting a block for conditional execution might be useful. Maybe require_keys should accept a block. Figure out later.


30
31
32
33
34
35
36
37
# File 'lib/echosign/validatable.rb', line 30

def validate_field(field, params)
  begin
    value = params.fetch(field)
    required_error(field) if value.nil? || (value.is_a?(String) && value.empty?)
  rescue KeyError
    required_error(field)
  end
end