@@ -4,6 +4,8 @@ module SplitIoClient
44 class Validators
55
66 Flagset_regex = /^[a-z0-9][_a-z0-9]{0,49}$/
7+ Fallback_treatment_regex = /^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$/
8+ Fallback_treatment_size = 100
79
810 def initialize ( config )
911 @config = config
@@ -68,7 +70,7 @@ def valid_flag_sets(method, flag_sets)
6870 log_invalid_flag_set_type ( method )
6971 elsif flag_set . is_a? ( String ) && flag_set . empty?
7072 log_invalid_flag_set_type ( method )
71- elsif !flag_set . empty? && string_match? ( flag_set . strip . downcase , method )
73+ elsif !flag_set . empty? && string_match? ( flag_set . strip . downcase , method , Flagset_regex , log_invalid_match )
7274 valid_flag_sets . add ( flag_set . strip . downcase )
7375 else
7476 log_invalid_flag_set_type ( method )
@@ -91,9 +93,9 @@ def number_or_string?(value)
9193 ( value . is_a? ( Numeric ) && !value . to_f . nan? ) || string? ( value )
9294 end
9395
94- def string_match? ( value , method )
95- if Flagset_regex . match ( value ) == nil
96- log_invalid_match ( value , method )
96+ def string_match? ( value , method , regex_exp , log_if_invalid )
97+ if regex_exp . match ( value ) == nil
98+ log_if_invalid ( value , method )
9799 false
98100 else
99101 true
@@ -326,5 +328,30 @@ def valid_properties?(properties)
326328
327329 true
328330 end
331+
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+
353+ def log_invalid_fallback_treatment ( key , method )
354+ @config . logger . warn ( "#{ method } : Invalid treatment #{ key } , Fallback treatment should match regex %s" , Fallback_treatment_regex )
355+ end
329356 end
330357end
0 commit comments