Class: Sequent::Core::Helpers::ValueValidators

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/core/helpers/value_validators.rb

Constant Summary collapse

INVALID_STRING_CHARS =
[
  "\u0000",
].freeze
VALIDATORS =
{
  ::Symbol => ->(_) { true },
  ::String => ->(value) { valid_string?(value) },
  ::Integer => ->(value) { valid_integer?(value) },
  ::Boolean => ->(value) { valid_bool?(value) },
  ::Date => ->(value) { valid_date?(value) },
  ::Time => ->(value) { valid_time?(value) },
  ::DateTime => ->(value) { valid_date_time?(value) },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ ValueValidators

Returns a new instance of ValueValidators.



79
80
81
# File 'lib/sequent/core/helpers/value_validators.rb', line 79

def initialize(klass)
  @klass = klass
end

Class Method Details

.for(klass) ⇒ Object



75
76
77
# File 'lib/sequent/core/helpers/value_validators.rb', line 75

def self.for(klass)
  new(klass)
end

.valid_bool?(value) ⇒ Boolean

Returns:



29
30
31
32
33
# File 'lib/sequent/core/helpers/value_validators.rb', line 29

def self.valid_bool?(value)
  return true if value.blank?

  value.is_a?(TrueClass) || value.is_a?(FalseClass) || value == 'true' || value == 'false'
end

.valid_date?(value) ⇒ Boolean

Returns:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sequent/core/helpers/value_validators.rb', line 35

def self.valid_date?(value)
  return true if value.blank?
  return true if value.is_a?(Date)
  return false unless value =~ /\d{4}-\d{2}-\d{2}/

  begin
    !!Date.iso8601(value)
  rescue StandardError
    false
  end
end

.valid_date_time?(value) ⇒ Boolean

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/sequent/core/helpers/value_validators.rb', line 47

def self.valid_date_time?(value)
  return true if value.blank?

  begin
    value.is_a?(DateTime) || !!DateTime.iso8601(value.dup)
  rescue StandardError
    false
  end
end

.valid_integer?(value) ⇒ Boolean

Returns:



23
24
25
26
27
# File 'lib/sequent/core/helpers/value_validators.rb', line 23

def self.valid_integer?(value)
  value.blank? || Integer(value)
rescue StandardError
  false
end

.valid_string?(value) ⇒ Boolean

Returns:



67
68
69
70
71
72
73
# File 'lib/sequent/core/helpers/value_validators.rb', line 67

def self.valid_string?(value)
  return true if value.nil?

  value.to_s && INVALID_STRING_CHARS.none? { |invalid_char| value.to_s.include?(invalid_char) }
rescue StandardError
  false
end

.valid_time?(value) ⇒ Boolean

Returns:



57
58
59
60
61
62
63
64
65
# File 'lib/sequent/core/helpers/value_validators.rb', line 57

def self.valid_time?(value)
  return true if value.blank?

  begin
    value.is_a?(Time) || !!Time.iso8601(value.dup)
  rescue StandardError
    false
  end
end

Instance Method Details

#valid_value?(value) ⇒ Boolean

Returns:



83
84
85
# File 'lib/sequent/core/helpers/value_validators.rb', line 83

def valid_value?(value)
  VALIDATORS[@klass].call(value)
end