Top Level Namespace

Defined Under Namespace

Modules: PxModule Classes: PxConfigurationException, PxCookieDecryptionException

Instance Method Summary collapse

Instance Method Details

#validate_hash_schema(hash, schema) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/perimeterx/internal/validators/hash_schema_validator.rb', line 1

def validate_hash_schema(hash, schema)
  hash.each do |key, value|
    if schema.key?(key) && value != nil
      # validate value types in hash are according to schema
      if !schema[key][:types].include?(value.class)
        raise PxConfigurationException.new("PerimeterX: Type of #{key} should be one of #{schema[key][:types]} but instead is #{value.class}")
      end
      
      # validate arrays elments types are according to schema
      if value.class == Array
        value.each do |element|
          if !schema[key][:allowed_element_types].include?(element.class)
            raise PxConfigurationException.new("PerimeterX: #{key} may only contain elements of the following types: #{schema[key][:allowed_element_types]} but includes element of type #{element.class}")
          end
        end
      end
    end
  end

  # validate required fields exist in hash
  schema.each do |key, value|
    if value[:required] && hash[key].nil?
      raise PxConfigurationException.new("PerimeterX: #{key} configuration is missing")
    end
  end
end