Class: AWS::Core::Options::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/options/validator.rb

Overview

Given a hash of validation rules, a validator validate request options. Validations support:

  • rejecting unknown options

  • ensuring presence of required options

  • validating expected option types (e.g. hash, array, string, integer, etc).

After validating, a hash of request options is returned with with normalized values (with converted types).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • rules (Hash)

    A hash of option rules to validate against.



31
32
33
# File 'lib/aws/core/options/validator.rb', line 31

def initialize rules
  @rules = rules
end

Instance Attribute Details

#rulesHash (readonly)

Returns:

  • (Hash)


36
37
38
# File 'lib/aws/core/options/validator.rb', line 36

def rules
  @rules
end

Instance Method Details

#validate!(request_options) ⇒ Hash

Parameters:

  • request_options (Hash)

    The hash of options to validate.

Returns:

  • (Hash)

Raises:

  • (ArgumentError)

    Raised when the options do not validate.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aws/core/options/validator.rb', line 42

def validate! request_options, rules = @rules

  # Verify all required options are present.
  rules.each_pair do |opt_name, opt_rules|
    if opt_rules[:required]
      unless request_options.key?(opt_name)
        raise ArgumentError, "missing required option :#{opt_name}"
      end
    end
  end

  request_options.inject({}) do |options, (opt_name, value)|

    # Ensure this is a valid/accepted option
    unless rules.key?(opt_name)
      raise ArgumentError, "unexpected option #{opt_name.inspect}"
    end

    # Validate and convert the value
    valid_value = validate_value(rules[opt_name], value, opt_name)

    options.merge(opt_name => valid_value)

  end
end