-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvalidators.rb
More file actions
361 lines (291 loc) · 10.4 KB
/
Copy pathvalidators.rb
File metadata and controls
361 lines (291 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# frozen_string_literal: true
module SplitIoClient
class Validators
Flagset_regex = /^[a-z0-9][_a-z0-9]{0,49}$/
Fallback_treatment_regex = /^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$/
Fallback_treatment_size = 100
def initialize(config)
@config = config
end
def valid_get_treatment_parameters(method, key, split_name, matching_key, bucketing_key, attributes)
valid_key?(method, key) &&
valid_split_name?(method, split_name) &&
valid_matching_key?(method, matching_key) &&
valid_bucketing_key?(method, key, bucketing_key) &&
valid_attributes?(method, attributes)
end
def valid_get_treatments_parameters(method, key, split_names, matching_key, bucketing_key, attributes)
valid_key?(method, key) &&
valid_split_names?(method, split_names)
valid_matching_key?(method, matching_key) &&
valid_bucketing_key?(method, key, bucketing_key) &&
valid_attributes?(method, attributes)
end
def valid_track_parameters(key, traffic_type_name, event_type, value, properties)
valid_track_key?(key) &&
valid_traffic_type_name?(traffic_type_name) &&
valid_event_type?(event_type) &&
valid_value?(value) &&
valid_properties?(properties)
end
def valid_split_parameters(split_name)
valid_split_name?(:split, split_name)
end
def valid_matcher_arguments(args)
return false if !args.key?(:attributes) && !args.key?(:value)
return false if args.key?(:value) && args[:value].nil?
return false if args.key?(:attributes) && args[:attributes].nil?
true
end
def valid_flag_sets(method, flag_sets)
if flag_sets.nil? || !flag_sets.is_a?(Array)
@config.logger.error("#{method}: FlagSets must be a non-empty list.")
return []
end
if flag_sets.empty?
@config.logger.error("#{method}: FlagSets must be a non-empty list.")
return []
end
without_nil = Array.new
flag_sets.each { |flag_set|
if !flag_set.nil?
without_nil.push(flag_set)
next
end
log_nil("flag set", method)
}
if without_nil.length() == 0
log_invalid_flag_set_type(method)
return []
end
valid_flag_sets = Set[]
without_nil.compact.uniq.select do |flag_set|
if flag_set.nil? || !flag_set.is_a?(String)
log_invalid_flag_set_type(method)
elsif flag_set.is_a?(String) && flag_set.empty?
log_invalid_flag_set_type(method)
elsif !flag_set.empty? && string_match?(flag_set.strip.downcase, method, Flagset_regex, :log_invalid_match)
valid_flag_sets.add(flag_set.strip.downcase)
else
log_invalid_flag_set_type(method)
end
end
!valid_flag_sets.empty? ? valid_flag_sets.to_a.sort : []
end
def validate_fallback_treatment(method, fallback_treatment)
if !fallback_treatment.is_a? Engine::Models::FallbackTreatment
@config.logger.warn("#{method}: Fallback treatment instance should be FallbackTreatment, input is discarded")
return false
end
if !fallback_treatment.treatment.is_a? String
@config.logger.warn("#{method}: Fallback treatment value should be str type, input is discarded")
return false
end
return false unless string_match?(fallback_treatment.treatment, method, Fallback_treatment_regex, :log_invalid_fallback_treatment)
if fallback_treatment.treatment.size > Fallback_treatment_size
@config.logger.warn("#{method}: Fallback treatment size should not exceed #{Fallback_treatment_size} characters")
return false
end
true
end
def valid_split_name?(method, split_name)
if split_name.nil?
log_nil(:split_name, method)
return false
end
unless string?(split_name)
log_invalid_type(:split_name, method)
return false
end
if empty_string?(split_name)
log_empty_string(:split_name, method)
return false
end
true
end
private
def string?(value)
value.is_a?(String) || value.is_a?(Symbol)
end
def empty_string?(value)
value.is_a?(String) && value.empty?
end
def number_or_string?(value)
(value.is_a?(Numeric) && !value.to_f.nan?) || string?(value)
end
def string_match?(value, method, regex_exp, log_if_invalid)
if regex_exp.match(value) == nil
method(log_if_invalid).call(value, method)
false
else
true
end
end
def log_invalid_match(key, method)
@config.logger.error("#{method}: you passed #{key}, flag set must adhere to the regular expression #{Flagset_regex}. " +
"This means flag set must be alphanumeric, cannot be more than 50 characters long, and can only include a dash, underscore, " +
"period, or colon as separators of alphanumeric characters.")
end
def log_nil(key, method)
msg_text = String.new("#{method}: you passed a nil #{key}, #{key} must be a non-empty String")
msg_text << " or a Symbol" if !key.equal?("flag set")
@config.logger.error(msg_text)
end
def log_empty_string(key, method)
@config.logger.error("#{method}: you passed an empty #{key}, #{key} must be a non-empty String or a Symbol")
end
def log_invalid_type(key, method)
@config.logger.error("#{method}: you passed an invalid #{key} type, #{key} must be a non-empty String or a Symbol")
end
def log_invalid_flag_set_type(method)
@config.logger.warn("#{method}: you passed an invalid flag set type, flag set must be a non-empty String")
end
def log_convert_numeric(key, method, value)
@config.logger.warn("#{method}: #{key} \"#{value}\" is not of type String, converting")
end
def log_key_too_long(key, method)
@config.logger.error("#{method}: #{key} is too long - must be #{@config.max_key_size} characters or less")
end
def valid_key?(method, key)
if key.nil?
log_nil(:key, method)
return false
end
true
end
def valid_matching_key?(method, matching_key)
if matching_key.nil?
log_nil(:matching_key, method)
return false
end
unless number_or_string?(matching_key)
log_invalid_type(:matching_key, method)
return false
end
if empty_string?(matching_key)
log_empty_string(:matching_key, method)
return false
end
log_convert_numeric(:matching_key, method, matching_key) if matching_key.is_a? Numeric
if matching_key.size > @config.max_key_size
log_key_too_long(:matching_key, method)
return false
end
true
end
def valid_bucketing_key?(method, key, bucketing_key)
if key.is_a? Hash
if bucketing_key.nil?
log_nil(:bucketing_key, method)
return false
end
unless number_or_string?(bucketing_key)
log_invalid_type(:bucketing_key, method)
return false
end
if empty_string?(bucketing_key)
log_empty_string(:bucketing_key, method)
return false
end
log_convert_numeric(:bucketing_key, method, bucketing_key) if bucketing_key.is_a? Numeric
if bucketing_key.size > @config.max_key_size
log_key_too_long(:bucketing_key, method)
return false
end
end
true
end
def valid_split_names?(method, split_names)
unless !split_names.nil? && split_names.is_a?(Array)
@config.logger.error("#{method}: feature_flag_names must be a non-empty Array")
return false
end
true
end
def valid_attributes?(method, attributes)
unless attributes.nil? || attributes.is_a?(Hash)
@config.logger.error("#{method}: attributes must be of type Hash")
return false
end
true
end
def valid_track_key?(key)
if key.nil?
log_nil(:key, :track)
return false
end
unless number_or_string?(key)
log_invalid_type(:key, :track)
return false
end
if empty_string?(key)
log_empty_string(:key, :track)
return false
end
log_convert_numeric(:key, :track, key) if key.is_a? Numeric
if key.size > @config.max_key_size
log_key_too_long(:key, :track)
return false
end
true
end
def valid_event_type?(event_type)
if event_type.nil?
log_nil(:event_type, :track)
return false
end
unless string?(event_type)
log_invalid_type(:event_type, :track)
return false
end
if event_type.empty?
log_empty_string(:event_type, :track)
return false
end
if (event_type.to_s =~ /^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$/).nil?
@config.logger.error("track: you passed '#{event_type}', " \
'event_type must adhere to the regular expression ^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$. ' \
'This means an event name must be alphanumeric, cannot be more than 80 characters long, ' \
'and can only include a dash, underscore, period, or colon as separators of alphanumeric characters')
return false
end
true
end
def valid_traffic_type_name?(traffic_type_name)
if traffic_type_name.nil?
log_nil(:traffic_type_name, :track)
return false
end
unless string?(traffic_type_name)
log_invalid_type(:traffic_type_name, :track)
return false
end
if traffic_type_name.empty?
log_empty_string(:traffic_type_name, :track)
return false
end
unless traffic_type_name == traffic_type_name.downcase
@config.logger.warn('track: traffic_type_name should be all lowercase - ' \
'converting string to lowercase')
end
true
end
def valid_value?(value)
unless (value.is_a?(Numeric) && !value.to_f.nan?) || value.nil?
@config.logger.error('track: value must be Numeric')
return false
end
true
end
def valid_properties?(properties)
unless properties.is_a?(Hash) || properties.nil?
@config.logger.error('track: properties must be a Hash')
return false
end
true
end
def log_invalid_fallback_treatment(key, method)
@config.logger.warn("#{method}: Invalid treatment #{key}, Fallback treatment should match regex #{Fallback_treatment_regex}")
end
end
end