Class: Brita::TypeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/brita/type_validator.rb

Overview

TypeValidator validates that the incoming param is of the specified type

Constant Summary collapse

RANGE_PATTERN =
{ format: { with: /\A.+(?:[^.]\.\.\.[^.]).+\z/, message: "must be a range" } }.freeze
DECIMAL_PATTERN =
{ numericality: true, allow_nil: true }.freeze
BOOLEAN_PATTERN =
{ inclusion: { in: [true, false] }, allow_nil: true }.freeze
WHITELIST_TYPES =
[:int,
:decimal,
:boolean,
:string,
:text,
:date,
:time,
:datetime,
:scope].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param, type) ⇒ TypeValidator

Returns a new instance of TypeValidator.



18
19
20
21
# File 'lib/brita/type_validator.rb', line 18

def initialize(param, type)
  @param = param
  @type = type
end

Instance Attribute Details

#paramObject (readonly)

Returns the value of attribute param.



23
24
25
# File 'lib/brita/type_validator.rb', line 23

def param
  @param
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/brita/type_validator.rb', line 23

def type
  @type
end

Instance Method Details

#valid_type?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/brita/type_validator.rb', line 38

def valid_type?
  WHITELIST_TYPES.include?(type)
end

#validateObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brita/type_validator.rb', line 25

def validate
  case type
  when :datetime, :date, :time
    RANGE_PATTERN
  when :int
    valid_int?
  when :decimal
    DECIMAL_PATTERN
  when :boolean
    BOOLEAN_PATTERN
  end
end