class Aws::ParamValidator
@api private
Public Class Methods
new(rules, options = {})
click to toggle source
@param [Seahorse::Model::Shapes::ShapeRef] rules @option options [Boolean] :validate_required (true)
# File lib/aws-sdk-core/param_validator.rb, line 16 def initialize(rules, options = {}) @rules = rules @validate_required = options[:validate_required] != false end
validate!(rules, params)
click to toggle source
@param [Seahorse::Model::Shapes::ShapeRef] rules @param [Hash] params @return [void]
# File lib/aws-sdk-core/param_validator.rb, line 10 def self.validate!(rules, params) new(rules).validate!(params) end
Public Instance Methods
validate!(params)
click to toggle source
@param [Hash] params @return [void]
# File lib/aws-sdk-core/param_validator.rb, line 23 def validate!(params) errors = [] structure(@rules, params, errors, 'params') if @rules raise ArgumentError, error_messages(errors) unless errors.empty? end
Private Instance Methods
correct_type?(ref, value, errors, context)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 121 def correct_type?(ref, value, errors, context) case value when Hash then true when ref[:struct_class] then true else errors << "expected #{context} to be a hash" false end end
error_messages(errors)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 137 def error_messages(errors) if errors.size == 1 errors.first else prefix = "\n - " "parameter validator found #{errors.size} errors:" + prefix + errors.join(prefix) end end
io_like?(value)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 131 def io_like?(value) value.respond_to?(:read) && value.respond_to?(:rewind) && value.respond_to?(:size) end
list(ref, values, errors, context)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 60 def list(ref, values, errors, context) # ensure the value is an array unless values.is_a?(Array) errors << "expected #{context} to be an array" return end # validate members member_ref = ref.shape.member values.each.with_index do |value, index| shape(member_ref, value, errors, context + "[#{index}]") end end
map(ref, values, errors, context)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 74 def map(ref, values, errors, context) return unless correct_type?(ref, values, errors, context) key_ref = ref.shape.key value_ref = ref.shape.value values.each do |key, value| shape(key_ref, key, errors, "#{context} #{key.inspect} key") shape(value_ref, value, errors, context + "[#{key.inspect}]") end end
shape(ref, value, errors, context)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 87 def shape(ref, value, errors, context) case ref.shape when StructureShape then structure(ref, value, errors, context) when ListShape then list(ref, value, errors, context) when MapShape then map(ref, value, errors, context) when StringShape unless value.is_a?(String) errors << "expected #{context} to be a string" end when IntegerShape unless value.is_a?(Integer) errors << "expected #{context} to be an integer" end when FloatShape unless value.is_a?(Float) errors << "expected #{context} to be a float" end when TimestampShape unless value.is_a?(Time) errors << "expected #{context} to be a Time object" end when BooleanShape unless [true, false].include?(value) errors << "expected #{context} to be true or false" end when BlobShape unless io_like?(value) or value.is_a?(String) errors << "expected #{context} to be a string or IO object" end else raise "unhandled shape type: #{ref.shape.class.name}" end end
structure(ref, values, errors, context)
click to toggle source
# File lib/aws-sdk-core/param_validator.rb, line 31 def structure(ref, values, errors, context) # ensure the value is hash like return unless correct_type?(ref, values, errors, context) shape = ref.shape # ensure required members are present if @validate_required shape.required.each do |member_name| if values[member_name].nil? param = "#{context}[#{member_name.inspect}]" errors << "missing required parameter #{param}" end end end # validate non-nil members values.each_pair do |name, value| unless value.nil? if shape.member?(name) member_ref = shape.member(name) shape(member_ref, value, errors, context + "[#{name.inspect}]") else errors << "unexpected value at #{context}[#{name.inspect}]" end end end end