forest_admin_datasource_toolkit 1.0.0.pre.beta.33 → 1.0.0.pre.beta.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2c552cdfa3f4daaaf894b5dd8bec3ede99c92391fb2f81348cc7fd88f4abf03
4
- data.tar.gz: a67f14e7e72ea5f5540426f04180ae64506a25c38d512d8a99a8a988ad4b4497
3
+ metadata.gz: b62f0db9ae5e520370092144118bfd57891f74fbc27a9a7b213b12baa7d5b1da
4
+ data.tar.gz: 87741b3097fe5317d3e0e08135f95d976d8b683eae7ef2f5235f1823f9be4f2f
5
5
  SHA512:
6
- metadata.gz: 5bbf306c5f0c39d289f61bb0530244f2eae932f27a8c2aa10052f163871026ecf032547937e2a3a754c32059d15f8944f63f30764210bffe00659ef49a9a5557
7
- data.tar.gz: 6975afc24895674edc7e43c27e68c64855050468a84f54a3bb20bfffe99652988b5bc5d5a03b7c9932df485dd2b96d840b97a491a3fc661eb3bac6b4e626967a
6
+ metadata.gz: ce3f4cc50681b49eb8a71bb9d5f120807e80774f3dae950342d6b4ebf92b77cd69b27ecae93f844aa50272fcf8b0580e79f9c7a7e30c433557c8d47ef3d8305d
7
+ data.tar.gz: d340bd6fd4adfd07fd30508a6df78bc531df281b975c323dfbf98119210cb92425f46a20c001bf8c923497c69975edb9094ba5cd283b9ff64c1717bbcb9e489d
@@ -0,0 +1,95 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Validations
3
+ class ConditionTreeValidator
4
+ include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
5
+
6
+ def self.validate(condition_tree, collection)
7
+ if condition_tree.is_a?(Nodes::ConditionTreeBranch)
8
+ validate_branch(condition_tree, collection)
9
+ elsif condition_tree.is_a?(Nodes::ConditionTreeLeaf)
10
+ validate_leaf(condition_tree, collection)
11
+ else
12
+ raise Exceptions::ValidationError, 'Unexpected condition tree type'
13
+ end
14
+ end
15
+
16
+ def self.validate_branch(branch, collection)
17
+ unless %w[And Or].include?(branch.aggregator)
18
+ raise Exceptions::ValidationError,
19
+ "The given aggregator '#{branch.aggregator}' is not supported. The supported values are: ['Or', 'And']"
20
+ end
21
+
22
+ unless branch.conditions.is_a?(Array)
23
+ raise Exceptions::ValidationError,
24
+ "The given conditions '#{branch.conditions}' were expected to be an array"
25
+ end
26
+
27
+ branch.conditions.each { |condition| validate(condition, collection) }
28
+
29
+ nil
30
+ end
31
+
32
+ def self.validate_leaf(leaf, collection)
33
+ field_schema = Utils::Collection.get_field_schema(collection, leaf.field)
34
+
35
+ throw_if_operator_not_allowed_with_column(leaf, field_schema)
36
+ throw_if_value_not_allowed_with_operator(leaf, field_schema)
37
+ throw_if_operator_not_allowed_with_column_type(leaf, field_schema)
38
+ throw_if_value_not_allowed_with_column_type(leaf, field_schema)
39
+
40
+ nil
41
+ end
42
+
43
+ def self.throw_if_operator_not_allowed_with_column(leaf, column_schema)
44
+ operators = column_schema.filter_operators
45
+ return if operators.include?(leaf.operator)
46
+
47
+ raise Exceptions::ValidationError,
48
+ "The given operator '#{leaf.operator}' is not supported by the column: '#{leaf.field}'." \
49
+ "#{operators.empty? ? " The allowed types are: #{operators.join(",")}" : " The column is not filterable"}"
50
+ end
51
+
52
+ def self.throw_if_value_not_allowed_with_operator(leaf, column_schema)
53
+ allowed_types = Rules.get_allowed_types_for_operator(leaf.operator)
54
+ validate_values(leaf.field, column_schema, leaf.value, allowed_types)
55
+ end
56
+
57
+ def self.throw_if_operator_not_allowed_with_column_type(leaf, column_schema)
58
+ allowed_operators = Rules.get_allowed_operators_for_column_type(column_schema.column_type)
59
+
60
+ return if allowed_operators.include?(leaf.operator)
61
+
62
+ raise Exceptions::ValidationError,
63
+ "The given operator '#{leaf.operator}' is not allowed with the columnType schema: " \
64
+ "'#{column_schema.column_type}'. The allowed types are: [#{allowed_operators.join(",")}]"
65
+ end
66
+
67
+ def self.throw_if_value_not_allowed_with_column_type(leaf, column_schema)
68
+ # exclude some cases where the value is not related to the columnType of the field
69
+ excluded_cases = [
70
+ Operators::SHORTER_THAN,
71
+ Operators::LONGER_THAN,
72
+ Operators::AFTER_X_HOURS_AGO,
73
+ Operators::BEFORE_X_HOURS_AGO,
74
+ Operators::PREVIOUS_X_DAYS,
75
+ Operators::PREVIOUS_X_DAYS_TO_DATE
76
+ ]
77
+
78
+ return if excluded_cases.include?(leaf.operator)
79
+
80
+ types = Rules.get_allowed_types_for_column_type(column_schema.column_type)
81
+ validate_values(leaf.field, column_schema, leaf.value, types)
82
+ end
83
+
84
+ def self.validate_values(field, column_schema, value, allowed_types)
85
+ if value.is_a?(Array)
86
+ value.each do |item|
87
+ FieldValidator.validate_value(field, column_schema, item, allowed_types)
88
+ end
89
+ else
90
+ FieldValidator.validate_value(field, column_schema, value, allowed_types)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -62,15 +62,11 @@ module ForestAdminDatasourceToolkit
62
62
  Operators::LESS_THAN,
63
63
  Operators::GREATER_THAN
64
64
  ],
65
- PrimitiveType::JSON => [
66
- Operators::BLANK,
67
- Operators::MISSING,
68
- Operators::PRESENT
69
- ],
65
+ PrimitiveType::JSON => [*Rules::BASE_OPERATORS, *Rules::ARRAY_OPERATORS],
70
66
  PrimitiveType::DATEONLY => [*Rules::BASE_OPERATORS, *Rules::BASE_DATEONLY_OPERATORS],
71
67
  PrimitiveType::ENUM => [*Rules::BASE_OPERATORS, *Rules::ARRAY_OPERATORS],
72
68
  PrimitiveType::UUID => [*Rules::BASE_OPERATORS, *Rules::ARRAY_OPERATORS],
73
- PrimitiveType::BOOLEAN => Rules::BASE_OPERATORS,
69
+ PrimitiveType::BOOLEAN => [*Rules::BASE_OPERATORS, *Rules::ARRAY_OPERATORS],
74
70
  PrimitiveType::POINT => Rules::BASE_OPERATORS
75
71
  }
76
72
 
@@ -95,7 +91,7 @@ module ForestAdminDatasourceToolkit
95
91
  end
96
92
 
97
93
  def self.compute_allowed_types_for_operators
98
- get_allowed_operators_for_column_type.keys.reduce do |result, type|
94
+ get_allowed_operators_for_column_type.keys.each_with_object({}) do |type, result|
99
95
  allowed_operators = get_allowed_operators_for_column_type(type)
100
96
  allowed_operators.each do |operator|
101
97
  if result[operator]
@@ -104,8 +100,6 @@ module ForestAdminDatasourceToolkit
104
100
  result[operator] = [type]
105
101
  end
106
102
  end
107
-
108
- result
109
103
  end
110
104
  end
111
105
 
@@ -134,7 +128,9 @@ module ForestAdminDatasourceToolkit
134
128
  Operators::PREVIOUS_X_DAYS_TO_DATE => ['Number'],
135
129
  Operators::PREVIOUS_X_DAYS => ['Number'],
136
130
  Operators::BEFORE_X_HOURS_AGO => ['Number'],
137
- Operators::AFTER_X_HOURS_AGO => ['Number']
131
+ Operators::AFTER_X_HOURS_AGO => ['Number'],
132
+ Operators::LONGER_THAN => ['Number'],
133
+ Operators::SHORTER_THAN => ['Number']
138
134
  )
139
135
 
140
136
  operator ? merged[operator] : merged
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceToolkit
2
- VERSION = "1.0.0-beta.33"
2
+ VERSION = "1.0.0-beta.35"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_admin_datasource_toolkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.beta.33
4
+ version: 1.0.0.pre.beta.35
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-03-05 00:00:00.000000000 Z
12
+ date: 2024-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -103,6 +103,7 @@ files:
103
103
  - lib/forest_admin_datasource_toolkit/utils/record.rb
104
104
  - lib/forest_admin_datasource_toolkit/utils/schema.rb
105
105
  - lib/forest_admin_datasource_toolkit/validations/chart_validator.rb
106
+ - lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb
106
107
  - lib/forest_admin_datasource_toolkit/validations/field_validator.rb
107
108
  - lib/forest_admin_datasource_toolkit/validations/projection_validator.rb
108
109
  - lib/forest_admin_datasource_toolkit/validations/rules.rb