1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[tests] Fixes schema validator for arrays

Arrays should not fail if empty. They should only fail if any member of
the array does not match the schema.
This commit is contained in:
Paul Thornthwaite 2013-01-23 14:54:47 +00:00
parent 5f63112640
commit cdae6f38f4
3 changed files with 24 additions and 17 deletions

View file

@ -106,21 +106,6 @@ module Fog
def validate_value(validator, value, options) def validate_value(validator, value, options)
Fog::Logger.write :debug, "[yellow][DEBUG] #{value.inspect} against #{validator.inspect}[/]\n" Fog::Logger.write :debug, "[yellow][DEBUG] #{value.inspect} against #{validator.inspect}[/]\n"
# When being strict values not specified in the schema are fails
unless options[:allow_extra_keys]
if validator.respond_to?(:empty?) && value.respond_to?(:empty?)
# Validator is empty but values are not
return false if !value.empty? && validator.empty?
end
end
unless options[:allow_optional_rules]
if validator.respond_to?(:empty?) && value.respond_to?(:empty?)
# Validator has rules left but no more values
return false if value.empty? && !validator.empty?
end
end
case validator case validator
when Array when Array
return false if value.is_a?(Hash) return false if value.is_a?(Hash)
@ -129,6 +114,22 @@ module Fog
value.respond_to? validator value.respond_to? validator
when Hash when Hash
return false if value.is_a?(Array) return false if value.is_a?(Array)
# When being strict values not specified in the schema are fails
unless options[:allow_extra_keys]
if value.respond_to?(:empty?)
# Validator is empty but values are not
return false if !value.empty? && validator.empty?
end
end
unless options[:allow_optional_rules]
if value.respond_to?(:empty?)
# Validator has rules left but no more values
return false if value.empty? && !validator.empty?
end
end
validator.all? do |key, sub_validator| validator.all? do |key, sub_validator|
Fog::Logger.write :debug, "[blue][DEBUG] #{key.inspect} against #{sub_validator.inspect}[/]\n" Fog::Logger.write :debug, "[blue][DEBUG] #{key.inspect} against #{sub_validator.inspect}[/]\n"
validate_value(sub_validator, value[key], options) validate_value(sub_validator, value[key], options)

View file

@ -62,7 +62,9 @@ module Shindo
# "id" => String, # "id" => String,
# "ram" => Integer, # "ram" => Integer,
# "disks" => [ # "disks" => [
# "size" => Float # {
# "size" => Float
# }
# ], # ],
# "dns_name" => Fog::Nullable::String, # "dns_name" => Fog::Nullable::String,
# "active" => Fog::Boolean, # "active" => Fog::Boolean,

View file

@ -23,7 +23,7 @@ Shindo.tests('Fog::Schema::DataValidator', 'meta') do
end end
returns(true, 'when collection is empty although schema covers optional members') do returns(true, 'when collection is empty although schema covers optional members') do
validator.validate([], [{"key" => String}], {:allow_optional_rules => true}) validator.validate([], [{"key" => String}])
end end
returns(true, 'when additional keys are passed and not strict') do returns(true, 'when additional keys are passed and not strict') do
@ -78,6 +78,10 @@ Shindo.tests('Fog::Schema::DataValidator', 'meta') do
validator.validate([{"key" => "Value"}, {"key" => 5}], [{"key" => String}]) validator.validate([{"key" => "Value"}, {"key" => 5}], [{"key" => String}])
end end
returns(false, 'when collection has multiple schema patterns') do
validator.validate([{"key" => "Value"}], [{"key" => Integer}, {"key" => String}])
end
returns(false, 'when hash and array are compared') do returns(false, 'when hash and array are compared') do
validator.validate({}, []) validator.validate({}, [])
end end