Class: ThemeSettingsValidator

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

Overview

Service class that holds helper methods that can be used to validate theme settings.

Class Method Summary collapse

Class Method Details

.is_valid_value_type?(value, type) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/theme_settings_validator.rb', line 10

def is_valid_value_type?(value, type)
  case type
  when self.types[:integer]
    value.is_a?(Integer)
  when self.types[:float]
    value.is_a?(Integer) || value.is_a?(Float)
  when self.types[:bool]
    value.is_a?(TrueClass) || value.is_a?(FalseClass)
  when self.types[:list]
    value.is_a?(String)
  when self.types[:objects]
    value.is_a?(Array) && value.all? { |v| v.is_a?(Hash) }
  else
    true
  end
end

.is_value_present?(value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/theme_settings_validator.rb', line 6

def is_value_present?(value)
  !value.nil?
end

.validate_value(value, type, opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/theme_settings_validator.rb', line 27

def validate_value(value, type, opts)
  errors = []

  case type
  when types[:enum]
    if opts[:choices].exclude?(value) && opts[:choices].map(&:to_s).exclude?(value)
      errors << I18n.t(
        "themes.settings_errors.enum_value_not_valid",
        choices: opts[:choices].join(", "),
      )
    end
  when types[:integer], types[:float]
    validate_value_in_range!(
      value,
      min: opts[:min],
      max: opts[:max],
      errors:,
      translation_prefix: "number",
    )
  when types[:string]
    validate_value_in_range!(
      value.to_s.length,
      min: opts[:min],
      max: opts[:max],
      errors:,
      translation_prefix: "string",
    )
  when types[:objects]
    errors.concat(
      ThemeSettingsObjectValidator.validate_objects(schema: opts[:schema], objects: value),
    )
  end

  errors
end