Class: TypedParams::Validations::Length

Inherits:
Validation
  • Object
show all
Defined in:
lib/typed_params/validations/length.rb

Instance Method Summary collapse

Methods inherited from Validation

#initialize, wrap

Constructor Details

This class inherits a constructor from TypedParams::Validations::Validation

Instance Method Details

#call(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/typed_params/validations/length.rb', line 8

def call(value)
  case options
  in minimum: Numeric => min, maximum: Numeric => max
    raise ValidationError, "length must be between #{min} and #{max} (inclusive)" unless
      value.length >= min && value.length <= max
  in minimum: Numeric => n
    raise ValidationError, "length must be greater than or equal to #{n}" unless
      value.length >= n
  in maximum: Numeric => n
    raise ValidationError, "length must be less than or equal to #{n}" unless
      value.length <= n
  in within: Range | Array => e
    raise ValidationError, "length must be between #{e.first} and #{e.last}" unless
      e.include?(value.length)
  in in: Range | Array => e
    raise ValidationError, "length must be between #{e.first} and #{e.last}" unless
      e.include?(value.length)
  in is: Numeric => n
    raise ValidationError, "length must be equal to #{n}" unless
      value.length == n
  end
end