Class: Sift::TypeValidator

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

Overview

TypeValidator validates that the incoming param is of the specified type

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param, type) ⇒ TypeValidator

Returns a new instance of TypeValidator.



20
21
22
23
# File 'lib/sift/type_validator.rb', line 20

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

Instance Attribute Details

#paramObject (readonly)

Returns the value of attribute param.



25
26
27
# File 'lib/sift/type_validator.rb', line 25

def param
  @param
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/sift/type_validator.rb', line 25

def type
  @type
end

Instance Method Details

#valid_type?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/sift/type_validator.rb', line 42

def valid_type?
  WHITELIST_TYPES.include?(type)
end

#validateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sift/type_validator.rb', line 27

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