Class: Volt::InclusionValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/volt/models/validators/inclusion_validator.rb

Class Method Summary collapse

Class Method Details

.validate(model, field_name, args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/volt/models/validators/inclusion_validator.rb', line 3

def self.validate(model, field_name, args)
  errors = {}
  value  = model.get(field_name)

  if args.is_a?(Array)
    list = args
    message = nil
  elsif args.is_a?(Hash)
    list = args[:in]
    message = args[:message]
    fail 'A list to match against must be specified' unless list.is_a?(Array)
  else
    fail 'The arguments to inclusion validator must be an array or a hash'
  end

  unless list.include?(value)
    errors[field_name] = [message || "must be one of #{list.join(', ')}"]
  end

  errors
end