forest_admin_datasource_toolkit 1.0.0.pre.beta.34 → 1.0.0.pre.beta.36
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/forest_admin_datasource_toolkit/components/query/page.rb +5 -0
- data/lib/forest_admin_datasource_toolkit/validations/condition_tree_validator.rb +95 -0
- data/lib/forest_admin_datasource_toolkit/validations/rules.rb +6 -10
- data/lib/forest_admin_datasource_toolkit/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3d8c5b1c1274bd495e86cfae6e4f2c0b8ee1a4cc4655f8e7472f61b3fc7fc48
|
4
|
+
data.tar.gz: afd39e3f506770138dc50cf4e361b4164b4b9014d678e3ba22c5a57e90ba86d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 387ac362e95d5bd809fe2790e20c01c4bbec05d41266579bdb7a84863d825d7d9b25116ae4a44dde377f2779034623561a684f14679c74fa96d9523aaf32ed18
|
7
|
+
data.tar.gz: dba0eab2b216fcd3113e991c08a82dbfe18b0dcb3476a4f61f23ba3ebad2955482ed2fbbb604da5cf3744b6cadce6e550245c2b8e982f7871bf708401046d264
|
@@ -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.
|
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
|
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.
|
4
|
+
version: 1.0.0.pre.beta.36
|
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-
|
12
|
+
date: 2024-03-18 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
|