moduleSeahorsemoduleClientclassParamValidator# @param [Model::Shapes::Shape] shape# @param [Hash] params# @return [void]defself.validate!(shape,params)new(shape).validate!(params)end# @param [Model::Shapes::Shape] shape# @option options [Boolean] :validate_required (true)definitialize(shape,options={})@shape=shape||Seahorse::Model::Shapes::Structure.new@validate_required=options[:validate_required]!=falseend# @param [Hash] params# @return [void]defvalidate!(params)errors=[]shape(@shape,params,errors,context='params')raiseArgumentError,error_messages(errors)unlesserrors.empty?endprivatedefstructure(structure,values,errors,context)# ensure the value is hash likereturnunlesshash?(values,errors,context)# ensure required members are presentif@validate_requiredstructure.required.eachdo|member_name|ifvalues[member_name].nil?param="#{context}[#{member_name.inspect}]"errors<<"missing required parameter #{param}"endendend# validate non-nil membersvalues.eachdo|name,value|unlessvalue.nil?ifstructure.member?(name)member_shape=structure.member(name)shape(member_shape,value,errors,context+"[#{name.inspect}]")elseerrors<<"unexpected value at #{context}[#{name.inspect}]"endendendenddeflist(list,values,errors,context)# ensure the value is an arrayunlessvalues.is_a?(Array)errors<<"expected #{context} to be an array"returnend# validate membersvalues.each.with_indexdo|value,index|shape(list.member,value,errors,context+"[#{index}]")endenddefmap(map,values,errors,context)returnunlesshash?(values,errors,context)values.eachdo|key,value|shape(map.key,key,errors,"#{context}#{key.inspect} key")shape(map.value,value,errors,context+"[#{key.inspect}]")endenddefshape(shape,value,errors,context)caseshapewhenModel::Shapes::Structurestructure(shape,value,errors,context)whenModel::Shapes::Listlist(shape,value,errors,context)whenModel::Shapes::Mapmap(shape,value,errors,context)whenModel::Shapes::Stringunlessvalue.is_a?(String)errors<<"expected #{context} to be a string"endwhenModel::Shapes::Integerunlessvalue.is_a?(Integer)errors<<"expected #{context} to be an integer"endwhenModel::Shapes::Floatunlessvalue.is_a?(Float)errors<<"expected #{context} to be a float"endwhenModel::Shapes::Timestampunlessvalue.is_a?(Time)errors<<"expected #{context} to be a Time object"endwhenModel::Shapes::Booleanunless[true,false].include?(value)errors<<"expected #{context} to be true or false"endwhenModel::Shapes::Blobunlessio_like?(value)orvalue.is_a?(String)errors<<"expected #{context} to be a string or IO object"endendenddefhash?(value,errors,context)ifvalue.is_a?(Hash)trueelseerrors<<"expected #{context} to be a hash"falseendenddefio_like?(value)value.respond_to?(:read)&&value.respond_to?(:rewind)&&value.respond_to?(:size)enddeferror_messages(errors)iferrors.size==1errors.firstelseprefix="\n - ""parameter validator found #{errors.size} errors:"+prefix+errors.join(prefix)endendendendend