[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)
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
when Array
return false if value.is_a?(Hash)
@ -129,6 +114,22 @@ module Fog
value.respond_to? validator
when Hash
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|
Fog::Logger.write :debug, "[blue][DEBUG] #{key.inspect} against #{sub_validator.inspect}[/]\n"
validate_value(sub_validator, value[key], options)

View File

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

View File

@ -23,7 +23,7 @@ Shindo.tests('Fog::Schema::DataValidator', 'meta') do
end
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
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}])
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
validator.validate({}, [])
end