Skip to content

Commit bbb6848

Browse files
committed
fixed validator and added tests
1 parent 16746c2 commit bbb6848

File tree

3 files changed

+76
-47
lines changed

3 files changed

+76
-47
lines changed

lib/splitclient-rb/split_config.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,15 +712,16 @@ def self.sanitize_fallback_config(fallback_config, validator, logger)
712712
end
713713

714714
sanitized_global_fallback_treatment = fallback_config.global_fallback_treatment
715-
if !fallback_config.global_fallback_treatment.nil? && !validator.validate_fallback_treatment(fallback_config.global_fallback_treatment)
715+
if !fallback_config.global_fallback_treatment.nil? && !validator.validate_fallback_treatment('Config', fallback_config.global_fallback_treatment)
716716
logger.warn('Config: global fallbacktreatment parameter is discarded.')
717-
sanitized_global_fallback_treatment = None
717+
sanitized_global_fallback_treatment = nil
718718
end
719719

720-
sanitized_flag_fallback_treatments = Hash.new
720+
sanitized_flag_fallback_treatments = nil
721721
if !fallback_config.by_flag_fallback_treatment.nil? && fallback_config.by_flag_fallback_treatment.is_a?(Hash)
722+
sanitized_flag_fallback_treatments = Hash.new
722723
for feature_name in fallback_config.by_flag_fallback_treatment.keys()
723-
if !validator.valid_split_name?('Config', feature_name) || !validator.validate_fallback_treatment(fallback_config.by_flag_fallback_treatment[feature_name])
724+
if !validator.valid_split_name?('Config', feature_name) || !validator.validate_fallback_treatment('Config', fallback_config.by_flag_fallback_treatment[feature_name])
724725
logger.warn("Config: fallback treatment parameter for feature flag #{feature_name} is discarded.")
725726
next
726727
end

lib/splitclient-rb/validators.rb

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def valid_flag_sets(method, flag_sets)
7070
log_invalid_flag_set_type(method)
7171
elsif flag_set.is_a?(String) && flag_set.empty?
7272
log_invalid_flag_set_type(method)
73-
elsif !flag_set.empty? && string_match?(flag_set.strip.downcase, method, Flagset_regex, log_invalid_match)
73+
elsif !flag_set.empty? && string_match?(flag_set.strip.downcase, method, Flagset_regex, :log_invalid_match)
7474
valid_flag_sets.add(flag_set.strip.downcase)
7575
else
7676
log_invalid_flag_set_type(method)
@@ -79,6 +79,46 @@ def valid_flag_sets(method, flag_sets)
7979
!valid_flag_sets.empty? ? valid_flag_sets.to_a.sort : []
8080
end
8181

82+
def validate_fallback_treatment(method, fallback_treatment)
83+
if !fallback_treatment.is_a? Engine::Models::FallbackTreatment
84+
@config.logger.warn("#{method}: Fallback treatment instance should be FallbackTreatment, input is discarded")
85+
return false
86+
end
87+
88+
if !fallback_treatment.treatment.is_a? String
89+
@config.logger.warn("#{method}: Fallback treatment value should be str type, input is discarded")
90+
return false
91+
end
92+
93+
return false unless string_match?(fallback_treatment.treatment, method, Fallback_treatment_regex, :log_invalid_fallback_treatment)
94+
95+
if fallback_treatment.treatment.size > Fallback_treatment_size
96+
@config.logger.warn("#{method}: Fallback treatment size should not exceed #{Fallback_treatment_size} characters")
97+
return false
98+
end
99+
100+
true
101+
end
102+
103+
def valid_split_name?(method, split_name)
104+
if split_name.nil?
105+
log_nil(:split_name, method)
106+
return false
107+
end
108+
109+
unless string?(split_name)
110+
log_invalid_type(:split_name, method)
111+
return false
112+
end
113+
114+
if empty_string?(split_name)
115+
log_empty_string(:split_name, method)
116+
return false
117+
end
118+
119+
true
120+
end
121+
82122
private
83123

84124
def string?(value)
@@ -95,7 +135,7 @@ def number_or_string?(value)
95135

96136
def string_match?(value, method, regex_exp, log_if_invalid)
97137
if regex_exp.match(value) == nil
98-
log_if_invalid(value, method)
138+
method(log_if_invalid).call(value, method)
99139
false
100140
else
101141
true
@@ -134,25 +174,6 @@ def log_key_too_long(key, method)
134174
@config.logger.error("#{method}: #{key} is too long - must be #{@config.max_key_size} characters or less")
135175
end
136176

137-
def valid_split_name?(method, split_name)
138-
if split_name.nil?
139-
log_nil(:split_name, method)
140-
return false
141-
end
142-
143-
unless string?(split_name)
144-
log_invalid_type(:split_name, method)
145-
return false
146-
end
147-
148-
if empty_string?(split_name)
149-
log_empty_string(:split_name, method)
150-
return false
151-
end
152-
153-
true
154-
end
155-
156177
def valid_key?(method, key)
157178
if key.nil?
158179
log_nil(:key, method)
@@ -329,29 +350,8 @@ def valid_properties?(properties)
329350
true
330351
end
331352

332-
def validate_fallback_treatment(method, fallback_treatment)
333-
if !fallback_treatment.is_a? Engine::Models::FallbackTreatment
334-
@config.logger.warn("#{method}: Fallback treatment instance should be FallbackTreatment, input is discarded")
335-
return false
336-
end
337-
338-
if !fallback_treatment.treatment.is_a? String
339-
@config.logger.warn("#{method}: Fallback treatment value should be str type, input is discarded")
340-
return false
341-
end
342-
343-
return false unless string_match?(fallback_treatment.treatment, method, Fallback_treatment_regex)
344-
345-
if fallback_treatment.treatment.size > Fallback_treatment_size
346-
@config.logger.warn("#{method}: Fallback treatment size should not exceed %s characters", Fallback_treatment_size)
347-
return false
348-
end
349-
350-
true
351-
end
352-
353353
def log_invalid_fallback_treatment(key, method)
354-
@config.logger.warn("#{method}: Invalid treatment #{key}, Fallback treatment should match regex %s", Fallback_treatment_regex)
354+
@config.logger.warn("#{method}: Invalid treatment #{key}, Fallback treatment should match regex #{Fallback_treatment_regex}")
355355
end
356356
end
357357
end

spec/splitclient/split_config_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,33 @@
180180
configs = SplitIoClient::SplitConfig.new(flag_sets_filter: ['1set', 12])
181181
expect(configs.flag_sets_filter).to eq ['1set']
182182
end
183+
184+
it 'test fallback treatment validations' do
185+
configs = SplitIoClient::SplitConfig.new(fallback_treatments: 0)
186+
expect(configs.fallback_treatments_configuration.is_a?(SplitIoClient::Engine::Models::FallbackTreatmentsConfiguration)).to eq true
187+
expect(configs.fallback_treatments_configuration.global_fallback_treatment).to eq nil
188+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment).to eq nil
189+
190+
configs = SplitIoClient::SplitConfig.new(fallback_treatments: SplitIoClient::Engine::Models::FallbackTreatmentsConfiguration.new)
191+
expect(configs.fallback_treatments_configuration.is_a?(SplitIoClient::Engine::Models::FallbackTreatmentsConfiguration)).to eq true
192+
expect(configs.fallback_treatments_configuration.global_fallback_treatment).to eq nil
193+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment).to eq nil
194+
195+
configs = SplitIoClient::SplitConfig.new(fallback_treatments: SplitIoClient::Engine::Models::FallbackTreatmentsConfiguration.new('on-global', {'feature' => 'on_45-c'}))
196+
expect(configs.fallback_treatments_configuration.global_fallback_treatment.is_a?(SplitIoClient::Engine::Models::FallbackTreatment)).to eq true
197+
expect(configs.fallback_treatments_configuration.global_fallback_treatment.treatment).to eq 'on-global'
198+
expect(configs.fallback_treatments_configuration.global_fallback_treatment.config).to eq nil
199+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment['feature'].is_a?(SplitIoClient::Engine::Models::FallbackTreatment)).to eq true
200+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment['feature'].treatment).to eq 'on_45-c'
201+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment['feature'].config).to eq nil
202+
203+
configs = SplitIoClient::SplitConfig.new(fallback_treatments: SplitIoClient::Engine::Models::FallbackTreatmentsConfiguration.new('on-gl/obal', {'feature' => "0" * 300}))
204+
expect(configs.fallback_treatments_configuration.global_fallback_treatment).to eq nil
205+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment).to eq Hash.new
206+
207+
configs = SplitIoClient::SplitConfig.new(fallback_treatments: SplitIoClient::Engine::Models::FallbackTreatmentsConfiguration.new(SplitIoClient::Engine::Models::FallbackTreatment.new('on-gl$#obal'), {"" => "treat"}))
208+
expect(configs.fallback_treatments_configuration.global_fallback_treatment).to eq nil
209+
expect(configs.fallback_treatments_configuration.by_flag_fallback_treatment).to eq Hash.new
210+
end
183211
end
184212
end

0 commit comments

Comments
 (0)