Module: DevSuite::Utils::Construct::Component::Validator::ValidationRule

Included in:
Base
Defined in:
lib/dev_suite/utils/construct/component/validator/validation_rule.rb

Instance Method Summary collapse

Instance Method Details

#validate_array_of_type!(array, klass, field_name: "Array") ⇒ Object



36
37
38
39
40
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 36

def validate_array_of_type!(array, klass, field_name: "Array")
  unless array.is_a?(Array) && array.all? { |item| item.is_a?(klass) }
    raise_validation_error(field_name, "must be an Array of #{klass.name} instances.")
  end
end

#validate_hash!(value, field_name: "Value") ⇒ Object



30
31
32
33
34
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 30

def validate_hash!(value, field_name: "Value")
  unless value.is_a?(Hash)
    raise_validation_error(field_name, "must be a Hash. Provided value is #{value.class.name}.")
  end
end

#validate_inclusion!(value, allowed_values, field_name: "Value") ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 9

def validate_inclusion!(value, allowed_values, field_name: "Value")
  unless allowed_values.include?(value)
    raise_validation_error(
      field_name,
      "#{value} is not valid. Allowed values: #{allowed_values.join(", ")}",
    )
  end
end

#validate_non_empty_string!(value, field_name: "Value") ⇒ Object



18
19
20
21
22
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 18

def validate_non_empty_string!(value, field_name: "Value")
  unless value.is_a?(String) && !value.strip.empty?
    raise_validation_error(field_name, "must be a non-empty string.")
  end
end

#validate_presence!(value, field_name: "Value") ⇒ Object



48
49
50
51
52
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 48

def validate_presence!(value, field_name: "Value")
  if value.nil? || (value.respond_to?(:empty?) && value.empty?)
    raise_validation_error(field_name, "is required and cannot be empty.")
  end
end

#validate_range!(value, range, field_name: "Value") ⇒ Object



42
43
44
45
46
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 42

def validate_range!(value, range, field_name: "Value")
  unless range.include?(value)
    raise_validation_error(field_name, "#{value} is not within the allowed range: #{range}.")
  end
end

#validate_type!(value, allowed_types, field_name: "Value") ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 54

def validate_type!(value, allowed_types, field_name: "Value")
  return if allowed_types.any? { |type| value.is_a?(type) }

  allowed_names = allowed_types.map(&:name).join(", ")
  raise_validation_error(
    field_name,
    "must be one of the following types: #{allowed_names}. Got #{value.class.name} instead.",
  )
end

#validate_url!(url, field_name: "URL") ⇒ Object



24
25
26
27
28
# File 'lib/dev_suite/utils/construct/component/validator/validation_rule.rb', line 24

def validate_url!(url, field_name: "URL")
  unless url =~ /\A#{URI::DEFAULT_PARSER.make_regexp(["http", "https"])}\z/
    raise_validation_error(field_name, "#{url} is not a valid URL.")
  end
end