Sha256: 3fa8c7232d42bdc4a000e1b4bb7022def7a104fb659aa6eaa4a67fdfb003daf6

Contents?: true

Size: 822 Bytes

Versions: 3

Compression:

Stored size: 822 Bytes

Contents

module Volt
  class LengthValidator
    def self.validate(model, field_name, args)
      errors = {}
      value  = model.read_attribute(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]
        fail 'length or minimum must be specified' unless min.is_a?(Fixnum)

        message = args[:message]
      else
        fail '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

      errors
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.8.21 lib/volt/models/validators/length_validator.rb
volt-0.8.20 lib/volt/models/validators/length_validator.rb
volt-0.8.19 lib/volt/models/validators/length_validator.rb