Module: Sbuilder::Utils::Validate

Defined in:
lib/utils/validate.rb

Class Method Summary collapse

Class Method Details

.oneOf(defintionHash, propList, min = 1, max = 1) ⇒ Object



29
30
31
32
# File 'lib/utils/validate.rb', line 29

def self.oneOf( defintionHash, propList, min=1, max=1 )
  props = propList.select {|prop| defintionHash.has_key?(prop) }.length
  raise ValidateException.new "Must give min #{min} - max #{max} #{propList} in #{defintionHash}" unless  props >= min && props <= max
end

.validateProperties(defintionHash, required, allowed = []) ⇒ Object

validate ‘defintionHash’ all ‘required’/only ‘allowed’ props set

Parameters:

  • hash (Hash)

    to validate

  • required (String:Array)

    array of mandatory properties

  • allowed (String:Array) (defaults to: [])

    optional list of allowed properties



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/utils/validate.rb', line 16

def self.validateProperties( defintionHash, required, allowed=[] )
  allowed = required unless allowed
  allowed = (required + allowed).uniq
  
  
  missingProps = required - defintionHash.keys
  raise ValidateException.new "Missing properties #{missingProps} in #{defintionHash} - required #{required}" if missingProps.any?

  invalidProps = defintionHash.keys - allowed 
  raise ValidateException.new "Unknown properties #{invalidProps} in #{defintionHash} - allowed #{allowed}" if invalidProps.any?

end