class AWS::Core::Options::Validator

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

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

Attributes

rules[R]

@return [Hash]

Public Class Methods

new(rules) click to toggle source

@param [Hash] rules A hash of option rules to validate against.

# File lib/aws/core/options/validator.rb, line 31
def initialize rules
  @rules = rules
end

Public Instance Methods

validate!(request_options, rules = @rules) click to toggle source

@overload validate!(request_options)

@param [Hash] request_options The hash of options to validate.
@raise [ArgumentError] Raised when the options do not validate.
@return [Hash]
# 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

Protected Instance Methods

format_error(description, opt_name, context) click to toggle source
# File lib/aws/core/options/validator.rb, line 132
def format_error description, opt_name, context
  context = context || "option :#{opt_name}"
  raise ArgumentError, "expected #{description} for #{context}"
end
validate_array(rules, value, opt_name, context = nil) click to toggle source

Ensures the value is an array (or at least enumerable) and that the yielded values are valid.

# File lib/aws/core/options/validator.rb, line 86
def validate_array rules, value, opt_name, context = nil
  unless value.respond_to?(:each)
    format_error('enumerable value', opt_name, context)
  end
  values = []
  value.each do |v|
    context = "member #{values.size} of :#{opt_name}"
    values << validate_value(rules[:members], v, opt_name, context)
  end
  values
end
validate_boolean(rules, value, opt_name, context = nil) click to toggle source

Ensures the value is a boolean.

# File lib/aws/core/options/validator.rb, line 111
def validate_boolean rules, value, opt_name, context = nil
  unless [true, false].include?(value)
    format_error('true or false', opt_name, context)
  end
  value
end
validate_hash(rules, value, opt_name, context = nil) click to toggle source

Ensures the value is a hash and validates the hash context.

# File lib/aws/core/options/validator.rb, line 77
def validate_hash rules, value, opt_name, context = nil
  unless value.respond_to?(:to_hash)
    format_error('hash value', opt_name, context)
  end
  validate!(value.to_hash, rules[:members])
end
validate_integer(rules, value, opt_name, context = nil) click to toggle source

Ensures the value is an integer.

# File lib/aws/core/options/validator.rb, line 119
def validate_integer rules, value, opt_name, context = nil
  unless value.respond_to?(:to_int)
    format_error('integer value', opt_name, context)
  end
  value.to_int
end
validate_string(rules, value, opt_name, context = nil) click to toggle source

Ensures the value is a string.

# File lib/aws/core/options/validator.rb, line 99
def validate_string rules, value, opt_name, context = nil

  unless value.respond_to?(:to_str)
    format_error('string value', opt_name, context)
  end

  rules[:lstrip] ?
    value.to_str.sub(%r^#{rules[:lstrip]}/, '') :
    value.to_str
end
validate_timestamp(rules, value, opt_name, context = nil) click to toggle source

Ensures the value is a timestamp.

# File lib/aws/core/options/validator.rb, line 127
def validate_timestamp rules, value, opt_name, context = nil
  # TODO : add validation to timestamps values
  value.to_s
end
validate_value(*args) click to toggle source

Proxies calls to the correct validation method based on the rules.

# File lib/aws/core/options/validator.rb, line 72
def validate_value *args
  send("validate_#{args.first[:type]}", *args)
end