Sha256: cb6ecf47ed5a80740c9b0ff95cdec9c2275696f0f6c96b698e17fa0bbee2d095

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

module Sbuilder
  
  module Utils

    module Validate

      # validate 'defintionHash' all 'required'/only 'allowed' props set
      #
      # @param [Hash] hash to validate
      #
      # @param [String:Array] required array of mandatory properties
      #
      # @param [String:Array] allowed optional list of allowed properties
      #
      # @exception when validation fails 
      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

      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

      
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tla-sbuilder-0.3.9 lib/utils/validate.rb
tla-sbuilder-0.3.8 lib/utils/validate.rb
tla-sbuilder-0.3.7 lib/utils/validate.rb
tla-sbuilder-0.3.4 lib/utils/validate.rb