Sha256: d9cba3f3ec0817b1d849c4151bdde14b646e6949af098c14642cd9faca6110d4

Contents?: true

Size: 630 Bytes

Versions: 5

Compression:

Stored size: 630 Bytes

Contents

class LengthValidation < Validation
  def self.validate(params, field, name, value, record, errors)
    length = params['length']
    min, max = length
  
    if min == 0
      valid = (value.size <= max)
    elsif max == 0
      valid = (value.size >= min)
    else
      valid = (value.size >= min) && (value.size <= max)
    end
  
    errors[field.name] << new(length) unless valid
  end

  def describe
    min, max = params

    if min == 0
      "is too long (maximum length is #{max})"
    elsif max == 0
      "is too short (minimum length is #{min})"
    else
      "must be between #{min} and #{max}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yodel-0.0.7 lib/yodel/models/core/validations/length_validation.rb
yodel-0.0.4 lib/yodel/models/core/validations/length_validation.rb
yodel-0.0.3 lib/yodel/models/core/validations/length_validation.rb
yodel-0.0.2 lib/yodel/models/core/validations/length_validation.rb
yodel-0.0.1 lib/yodel/models/core/validations/length_validation.rb