Class: CloudFormula::Validator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(parameter_name, validations) ⇒ Validator

Returns a new instance of Validator

Parameters:

  • validations (Hash)

    The rules defined for the given parameter_name


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_nameObject

Returns the value of attribute parameter_name


27
28
29
# File 'lib/cloudformula/validator.rb', line 27

def parameter_name
  @parameter_name
end

#validationsObject

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

Parameters:

  • value (Object)

    The value to check against

Returns:

  • (Array)

    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