Class: AutomationObject::BluePrint::HashAdapter::Validators::ValidateAllowedKeys

Inherits:
Validate
  • Object
show all
Defined in:
lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_allowed_keys.rb

Overview

Validator that tests the composite hash for bad keys

Instance Attribute Summary

Attributes inherited from Validate

#error_messages

Instance Method Summary collapse

Methods inherited from Validate

#valid?

Constructor Details

#initialize(args) ⇒ ValidateAllowedKeys

Returns a new instance of ValidateAllowedKeys

Parameters:

  • args (Hash)

    arguments for the validation class



12
13
14
# File 'lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_allowed_keys.rb', line 12

def initialize(args)
  @allowed_keys = args.fetch :allowed_keys
end

Instance Method Details

#validate(composite_object) ⇒ nil

Validates the composite object and throws errors on failure

Parameters:

  • composite_object (Object)

    Composite object to be tested.

Returns:

  • (nil)

    no return on exceptions on failure



19
20
21
22
23
24
25
26
# File 'lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_allowed_keys.rb', line 19

def validate(composite_object)
  # Get the hash from the composite object
  target_hash = composite_object.hash

  target_hash.each_key do |key|
    error_messages.push("Invalid Key: #{key}, at: #{composite_object.location}. Allowed keys: #{@allowed_keys}.") unless @allowed_keys.include?(key)
  end
end