Sha256: 6d1e2dad9d6460995f039727375231cfba9cfa640964254472bc1753bf8ab4d5

Contents?: true

Size: 809 Bytes

Versions: 15

Compression:

Stored size: 809 Bytes

Contents

module Volt
  class LengthValidator
    def self.validate(model, field_name, args)
      errors = {}
      value  = model.get(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 #{min} characters"]
      elsif max && value.size > max
        errors[field_name] = [message || "must be less than #{max} characters"]
      end

      errors
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
volt-0.9.7.pre8 lib/volt/models/validators/length_validator.rb
volt-0.9.7.pre7 lib/volt/models/validators/length_validator.rb
volt-0.9.7.pre6 lib/volt/models/validators/length_validator.rb
volt-0.9.7.pre5 lib/volt/models/validators/length_validator.rb
volt-0.9.7.pre3 lib/volt/models/validators/length_validator.rb
volt-0.9.7.pre2 lib/volt/models/validators/length_validator.rb
volt-0.9.6 lib/volt/models/validators/length_validator.rb
volt-0.9.6.pre3 lib/volt/models/validators/length_validator.rb
volt-0.9.6.pre2 lib/volt/models/validators/length_validator.rb
volt-0.9.6.pre1 lib/volt/models/validators/length_validator.rb
volt-0.9.5 lib/volt/models/validators/length_validator.rb
volt-0.9.5.pre12 lib/volt/models/validators/length_validator.rb
volt-0.9.5.pre11 lib/volt/models/validators/length_validator.rb
volt-0.9.5.pre9 lib/volt/models/validators/length_validator.rb
volt-0.9.5.pre8 lib/volt/models/validators/length_validator.rb