Class: ValidationProfiler::Rules::ConditionValidationRule

Inherits:
ValidationRule
  • Object
show all
Defined in:
lib/validation_profiler/rules/condition_validation_rule.rb

Overview

Defines the ConditionValidationRule class

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Instance Method Details

#confirm_expression?(value_a, expression, value_b) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/validation_profiler/rules/condition_validation_rule.rb', line 66

def confirm_expression?(value_a, expression, value_b)
  a = value_a
  b = value_b

  a.public_send(expression.to_s, b)
end

#error_message(field, attributes = {}, parent = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/validation_profiler/rules/condition_validation_rule.rb', line 5

def error_message(field, attributes = {}, parent = nil)
  field_name = field.to_s
  field_name = "#{parent}.#{field}" unless parent.nil?

  # check if a custom error message has been specified in the attributes
  if attributes[:message].nil?
    # no custom error message has been specified so create the default message.
    "#{field_name} is not valid"
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes, _parent = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
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
62
63
64
# File 'lib/validation_profiler/rules/condition_validation_rule.rb', line 18

def validate(obj, field, attributes, _parent = nil)
  # attempt to get the field value from the object
  value = get_field_value(obj, field)

  condition_field = attributes[:condition_field]
  if condition_field.nil?
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
      .new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  condition_value = attributes[:condition_value]
  if condition_value.nil?
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
      .new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  condition_expression = attributes[:condition_expression]
  if condition_expression.nil?
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
      .new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  field_expression = attributes[:field_expression]
  if field_expression.nil?
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
      .new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  field_value = attributes[:field_value]
  if field_value.nil?
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
      .new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  return true unless is_required?(value, attributes)

  condition_field_value = get_field_value(obj, condition_field)

  # check if the condition is valid
  if confirm_expression?(condition_field_value, condition_expression, condition_value)
    # check if the field value is correct for the condition
    return false unless confirm_expression?(value, field_expression, field_value)
    return true
  end

  true
end