Class: LengthValidator

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

Class Method Summary collapse

Class Method Details

.validate(model, field_name, args) ⇒ Object



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

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

  if args.is_a?(Fixnum)
    min = args
    max = nil
    message = nil
  elsif args.is_a?(Hash)
    min = args[:length] || args[:minimum]
    max = args[:maximum]
    raise "length or minimum must be specified" unless min.is_a?(Fixnum)

    message = args[:message]
  else
    raise "The arguments to length must be a number or a hash"
  end

  if !value || value.size < min
    errors[field_name] = [message || "must be at least #{args} characters"]
  elsif max && value.size > max
    errors[field_name] = [message || "must be less than #{args} characters"]
  end

  return errors
end