Class: LengthValidation

Inherits:
Validation show all
Defined in:
lib/yodel/models/core/validations/length_validation.rb

Instance Attribute Summary

Attributes inherited from Validation

#field, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Validation

#initialize, #to_json

Constructor Details

This class inherits a constructor from Validation

Class Method Details

.validate(params, field, name, value, record, errors) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/yodel/models/core/validations/length_validation.rb', line 2

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

Instance Method Details

#describeObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yodel/models/core/validations/length_validation.rb', line 17

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