Class: AWS::Core::Options::Validator
- Inherits:
-
Object
- Object
- AWS::Core::Options::Validator
- 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
- #rules ⇒ Hash readonly
Instance Method Summary collapse
-
#initialize(rules) ⇒ Validator
constructor
A new instance of Validator.
- #validate!(request_options) ⇒ Hash
Constructor Details
#initialize(rules) ⇒ Validator
Returns a new instance of Validator.
31 32 33 |
# File 'lib/aws/core/options/validator.rb', line 31 def initialize rules @rules = rules end |
Instance Attribute Details
#rules ⇒ Hash (readonly)
36 37 38 |
# File 'lib/aws/core/options/validator.rb', line 36 def rules @rules end |
Instance Method Details
#validate!(request_options) ⇒ Hash
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! , rules = @rules # Verify all required options are present. rules.each_pair do |opt_name, opt_rules| if opt_rules[:required] unless .key?(opt_name) raise ArgumentError, "missing required option #{opt_name.inspect}" end end end .inject({}) do |, (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) .merge(opt_name => valid_value) end end |