Module: Echochamber::Validatable

Instance Method Summary collapse

Instance Method Details

#require_exactly_one(field_group, params) ⇒ Object

Raises:


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/echochamber/validatable.rb', line 14

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.to_s} should be present" if set_fields != 1
end

#require_keys(required_fields, params) ⇒ Object


8
9
10
11
12
# File 'lib/echochamber/validatable.rb', line 8

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

#validate_field(field, params) ⇒ Object

TODO (kayagoban) A validator accepting a block for conditional execution might be useful.

Maybe require_keys should accept a block. Figure out later.


32
33
34
35
36
37
38
39
# File 'lib/echochamber/validatable.rb', line 32

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