Module: Sfn::Utils::StackParameterValidator
- Includes:
- Bogo::AnimalStrings
- Defined in:
- lib/sfn/utils/stack_parameter_validator.rb
Overview
Helper utility for validating stack parameters
Constant Summary collapse
- HEAT_CONSTRAINT_MAP =
HOT parameter mapping
{ 'MaxLength' => [:length, :max], 'MinLength' => [:length, :min], 'MaxValue' => [:range, :max], 'MinValue' => [:range, :min], 'AllowedValues' => [:allowed_values], 'AllowedPattern' => [:allowed_pattern] }
- GOOGLE_CONSTRAINT_MAP =
GCDM parameter mapping
{ 'AllowedPattern' => [:pattern], 'MaxValue' => [:maximum], 'MinValue' => [:minimum] }
- PARAMETER_DEFINITION_MAP =
Parameter mapping identifier and content
{ 'constraints' => HEAT_CONSTRAINT_MAP }
- PARAMETER_VALIDATIONS =
Supported parameter validations
[ 'allowed_values', 'allowed_pattern', 'max_length', 'min_length', 'max_value', 'min_value' ]
Instance Method Summary collapse
-
#allowed_pattern(value, pdef) ⇒ TrueClass, String
Parameter matches allowed pattern.
-
#allowed_values(value, pdef) ⇒ TrueClass, String
Parameter is within allowed values.
-
#list_type?(type) ⇒ TrueClass, FalseClass
Check if type is a list type.
-
#max_length(value, pdef) ⇒ TrueClass, String
Parameter length is less than or equal to max length.
-
#max_value(value, pdef) ⇒ TrueClass, String
Parameter value is less than or equal to max value.
-
#min_length(value, pdef) ⇒ TrueClass, String
Parameter length is greater than or equal to min length.
-
#min_value(value, pdef) ⇒ TrueClass, String
Parameter value is greater than or equal to min value.
-
#reformat_definition(pdef) ⇒ Hash
Reformat parameter definition with proper keys to allow validation for templates different parameter definition layout.
-
#validate_parameter(value, parameter_definition) ⇒ TrueClass, Array<String>
Validate a parameters.
Instance Method Details
#allowed_pattern(value, pdef) ⇒ TrueClass, String
Parameter matches allowed pattern
116 117 118 119 120 121 122 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 116 def allowed_pattern(value, pdef) if(value.match(%r{#{pdef}})) true else "Not a valid pattern. Must match: #{pdef}" end end |
#allowed_values(value, pdef) ⇒ TrueClass, String
Parameter is within allowed values
102 103 104 105 106 107 108 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 102 def allowed_values(value, pdef) if(pdef.include?(value)) true else "Not an allowed value: #{pdef.join(', ')}" end end |
#list_type?(type) ⇒ TrueClass, FalseClass
Check if type is a list type
184 185 186 187 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 184 def list_type?(type) type = type.downcase type.start_with?('comma') || type.start_with?('list<') end |
#max_length(value, pdef) ⇒ TrueClass, String
Parameter length is less than or equal to max length
130 131 132 133 134 135 136 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 130 def max_length(value, pdef) if(value.length <= pdef.to_i) true else "Value must not exceed #{pdef} characters" end end |
#max_value(value, pdef) ⇒ TrueClass, String
Parameter value is less than or equal to max value
158 159 160 161 162 163 164 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 158 def max_value(value, pdef) if(value.to_i <= pdef.to_i) true else "Value must not be greater than #{pdef}" end end |
#min_length(value, pdef) ⇒ TrueClass, String
Parameter length is greater than or equal to min length
144 145 146 147 148 149 150 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 144 def min_length(value, pdef) if(value.length >= pdef.to_i) true else "Value must be at least #{pdef} characters" end end |
#min_value(value, pdef) ⇒ TrueClass, String
Parameter value is greater than or equal to min value
172 173 174 175 176 177 178 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 172 def min_value(value, pdef) if(value.to_i >= pdef.to_i) true else "Value must not be less than #{pdef}" end end |
#reformat_definition(pdef) ⇒ Hash
Reformat parameter definition with proper keys to allow validation for templates different parameter definition layout
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 81 def reformat_definition(pdef) new_def = pdef PARAMETER_DEFINITION_MAP.each do |ident, mapping| if(pdef[ident]) new_def = Smash.new mapping.each do |new_key, current_path| if(pdef.get(*current_path)) new_def[new_key] = pdef.get(*current_path) end end end end new_def end |
#validate_parameter(value, parameter_definition) ⇒ TrueClass, Array<String>
Validate a parameters
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 54 def validate_parameter(value, parameter_definition) return [[:blank, 'Value cannot be blank']] if value.to_s.strip.empty? parameter_definition = reformat_definition(parameter_definition) value_list = list_type?(parameter_definition.fetch('Type', parameter_definition['type'].to_s)) ? value.to_s.split(',') : [value] result = PARAMETER_VALIDATIONS.map do |validator_key| valid_key = parameter_definition.keys.detect do |pdef_key| pdef_key.downcase.gsub('_', '') == validator_key.downcase.gsub('_', '') end if(valid_key) value_list.map do |value| res = self.send(validator_key, value, parameter_definition[valid_key]) res == true ? true : [validator_key, res] end else true end end.flatten(1) result.delete_if{|x| x == true} result.empty? ? true : result end |