Class: Sfn::Utils::StackParameterValidator
- Inherits:
-
Object
- Object
- Sfn::Utils::StackParameterValidator
- Extended by:
- 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] }
- 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' ]
Class Method Summary collapse
-
.allowed_pattern(value, pdef) ⇒ TrueClass, String
Parameter matches allowed pattern.
-
.allowed_values(value, pdef) ⇒ TrueClass, String
Parameter is within allowed values.
-
.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(value, parameter_definition) ⇒ TrueClass, Array<String>
Validate a parameters.
Class Method Details
.allowed_pattern(value, pdef) ⇒ TrueClass, String
Parameter matches allowed pattern
107 108 109 110 111 112 113 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 107 def allowed_pattern(value, pdef) if(value.match(/#{pdef}/)) true else "Not a valid pattern. Must match: #{pdef}" end end |
.allowed_values(value, pdef) ⇒ TrueClass, String
Parameter is within allowed values
93 94 95 96 97 98 99 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 93 def allowed_values(value, pdef) if(pdef.include?(value)) true else "Not an allowed value: #{pdef.join(', ')}" end end |
.max_length(value, pdef) ⇒ TrueClass, String
Parameter length is less than or equal to max length
121 122 123 124 125 126 127 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 121 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
149 150 151 152 153 154 155 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 149 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
135 136 137 138 139 140 141 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 135 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
163 164 165 166 167 168 169 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 163 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 72 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(value, parameter_definition) ⇒ TrueClass, Array<String>
Validate a parameters
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sfn/utils/stack_parameter_validator.rb', line 48 def validate(value, parameter_definition) return [[:blank, 'Value cannot be blank']] if value.to_s.strip.empty? parameter_definition = reformat_definition(parameter_definition) 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) res = self.send(validator_key, value, parameter_definition[valid_key]) res == true ? true : [validator_key, res] else true end end result.delete_if{|x| x == true} result.empty? ? true : result end |