Class: CloudFormula::Validator
- Inherits:
-
Object
- Object
- CloudFormula::Validator
- Defined in:
- lib/cloudformula/validator.rb
Overview
Contains logic to perform basic validation of parameters defined in a JsonErb template.
Available validations:
:exclusion => ['val1'] Ensures a value is not in the exclusion list
:inclusion => ['val1', 'val2'] Ensures a value is in the inclusion list
:length => { Ensures value length
:minimum => n,
:maximum => n,
:in|:within => x..y,
:is => n
}
:format => /regex/ Ensures a value matches the regex
:presence => true|false Ensures a value exists and has a length greater than 0
Defined Under Namespace
Classes: Rule
Instance Attribute Summary collapse
-
#parameter_name ⇒ Object
Returns the value of attribute parameter_name.
-
#validations ⇒ Object
Returns the value of attribute validations.
Instance Method Summary collapse
-
#initialize(parameter_name, validations) ⇒ Validator
constructor
A new instance of Validator.
-
#validate(value) ⇒ Array
An Array of Strings describing the failure(s) if any checks fail, or an empty Array otherwise.
Constructor Details
#initialize(parameter_name, validations) ⇒ Validator
Returns a new instance of Validator.
30 31 32 33 |
# File 'lib/cloudformula/validator.rb', line 30 def initialize(parameter_name, validations) @parameter_name = parameter_name @validations = validations end |
Instance Attribute Details
#parameter_name ⇒ Object
Returns the value of attribute parameter_name.
27 28 29 |
# File 'lib/cloudformula/validator.rb', line 27 def parameter_name @parameter_name end |
#validations ⇒ Object
Returns the value of attribute validations.
27 28 29 |
# File 'lib/cloudformula/validator.rb', line 27 def validations @validations end |
Instance Method Details
#validate(value) ⇒ Array
Returns An Array of Strings describing the failure(s) if any checks fail, or an empty Array otherwise.
37 38 39 40 41 42 43 44 |
# File 'lib/cloudformula/validator.rb', line 37 def validate(value) errors = [] @validations.each do |rule_name, rule_value| rule_result = check_rule Rule.new(rule_name, rule_value), value errors += rule_result end errors end |