Class: ForestAdminDatasourceToolkit::Validations::ConditionTreeValidator

Inherits:
Object
  • Object
show all
Includes:
Components::Query::ConditionTree
Defined in:
lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb

Class Method Summary collapse

Class Method Details

.throw_if_operator_not_allowed_with_column(leaf, column_schema) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 43

def self.throw_if_operator_not_allowed_with_column(leaf, column_schema)
  operators = column_schema.filter_operators
  return if operators.include?(leaf.operator)

  raise Exceptions::ValidationError,
        "The given operator '#{leaf.operator}' is not supported by the column: '#{leaf.field}'." \
        "#{operators.empty? ? " The allowed types are: #{operators.join(",")}" : " The column is not filterable"}"
end

.throw_if_operator_not_allowed_with_column_type(leaf, column_schema) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 57

def self.throw_if_operator_not_allowed_with_column_type(leaf, column_schema)
  allowed_operators = Rules.get_allowed_operators_for_column_type(column_schema.column_type)

  return if allowed_operators.include?(leaf.operator)

  raise Exceptions::ValidationError,
        "The given operator '#{leaf.operator}' is not allowed with the columnType schema: " \
        "'#{column_schema.column_type}'. The allowed types are: [#{allowed_operators.join(",")}]"
end

.throw_if_value_not_allowed_with_column_type(leaf, column_schema) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 67

def self.throw_if_value_not_allowed_with_column_type(leaf, column_schema)
  # exclude some cases where the value is not related to the columnType of the field
  # or where the operator accepts multiple column types (making the value type flexible)
  excluded_cases = [
    Operators::SHORTER_THAN,
    Operators::LONGER_THAN,
    Operators::AFTER_X_HOURS_AGO,
    Operators::BEFORE_X_HOURS_AGO,
    Operators::PREVIOUS_X_DAYS,
    Operators::PREVIOUS_X_DAYS_TO_DATE,
    # Comparison operators that work on multiple column types (String, Number, Date, etc.)
    # The value type validation is already handled by throw_if_value_not_allowed_with_operator
    Operators::GREATER_THAN,
    Operators::LESS_THAN,
    Operators::GREATER_THAN_OR_EQUAL,
    Operators::LESS_THAN_OR_EQUAL
  ]

  return if excluded_cases.include?(leaf.operator)

  types = Rules.get_allowed_types_for_column_type(column_schema.column_type)
  validate_values(leaf.field, column_schema, leaf.value, types)
end

.throw_if_value_not_allowed_with_operator(leaf, column_schema) ⇒ Object



52
53
54
55
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 52

def self.throw_if_value_not_allowed_with_operator(leaf, column_schema)
  allowed_types = Rules.get_allowed_types_for_operator(leaf.operator)
  validate_values(leaf.field, column_schema, leaf.value, allowed_types)
end

.validate(condition_tree, collection) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 6

def self.validate(condition_tree, collection)
  if condition_tree.is_a?(Nodes::ConditionTreeBranch)
    validate_branch(condition_tree, collection)
  elsif condition_tree.is_a?(Nodes::ConditionTreeLeaf)
    validate_leaf(condition_tree, collection)
  else
    raise Exceptions::ValidationError, 'Unexpected condition tree type'
  end
end

.validate_branch(branch, collection) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 16

def self.validate_branch(branch, collection)
  unless %w[And Or].include?(branch.aggregator)
    raise Exceptions::ValidationError,
          "The given aggregator '#{branch.aggregator}' is not supported. The supported values are: ['Or', 'And']"
  end

  unless branch.conditions.is_a?(Array)
    raise Exceptions::ValidationError,
          "The given conditions '#{branch.conditions}' were expected to be an array"
  end

  branch.conditions.each { |condition| validate(condition, collection) }

  nil
end

.validate_leaf(leaf, collection) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 32

def self.validate_leaf(leaf, collection)
  field_schema = Utils::Collection.get_field_schema(collection, leaf.field)

  throw_if_operator_not_allowed_with_column(leaf, field_schema)
  throw_if_value_not_allowed_with_operator(leaf, field_schema)
  throw_if_operator_not_allowed_with_column_type(leaf, field_schema)
  throw_if_value_not_allowed_with_column_type(leaf, field_schema)

  nil
end

.validate_values(field, column_schema, value, allowed_types) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb', line 91

def self.validate_values(field, column_schema, value, allowed_types)
  if value.is_a?(Array)
    value.each do |item|
      FieldValidator.validate_value(field, column_schema, item, allowed_types)
    end
  else
    FieldValidator.validate_value(field, column_schema, value, allowed_types)
  end
end