From cdae6f38f4be36ac8ff4094392ebf624e241667a Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Wed, 23 Jan 2013 14:54:47 +0000 Subject: [PATCH] [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. --- lib/fog/schema/data_validator.rb | 31 +++++++++++++------------ tests/helpers/formats_helper.rb | 4 +++- tests/helpers/schema_validator_tests.rb | 6 ++++- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/fog/schema/data_validator.rb b/lib/fog/schema/data_validator.rb index 108f23972..68a7d7a77 100644 --- a/lib/fog/schema/data_validator.rb +++ b/lib/fog/schema/data_validator.rb @@ -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) diff --git a/tests/helpers/formats_helper.rb b/tests/helpers/formats_helper.rb index 00caa1a85..3d5d39826 100644 --- a/tests/helpers/formats_helper.rb +++ b/tests/helpers/formats_helper.rb @@ -62,7 +62,9 @@ module Shindo # "id" => String, # "ram" => Integer, # "disks" => [ - # "size" => Float + # { + # "size" => Float + # } # ], # "dns_name" => Fog::Nullable::String, # "active" => Fog::Boolean, diff --git a/tests/helpers/schema_validator_tests.rb b/tests/helpers/schema_validator_tests.rb index d19e69fb9..8715af391 100644 --- a/tests/helpers/schema_validator_tests.rb +++ b/tests/helpers/schema_validator_tests.rb @@ -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