From 016d02ef67c79c68362ebdebf5854a7098ea625a Mon Sep 17 00:00:00 2001 From: Joe Faber Date: Mon, 25 Jul 2016 18:51:35 -0400 Subject: [PATCH 1/3] Test for partial tuple validation --- test/support/array_validation.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/support/array_validation.rb b/test/support/array_validation.rb index 3d6877f9..a9edb6c0 100644 --- a/test/support/array_validation.rb +++ b/test/support/array_validation.rb @@ -25,7 +25,9 @@ def test_items_multiple_schemas assert_valid schema, ['b', 1] assert_valid schema, ['b', 1, nil] refute_valid schema, [1, 'b'] - refute_valid schema, [] + assert_valid schema, [] + assert_valid schema, ['b'] + assert_valid schema, ['b', 1, 25] end def test_minitems @@ -62,6 +64,8 @@ def test_additional_items_false } assert_valid schema, [1, 'string'] + assert_valid schema, [1] + assert_valid schema, [] refute_valid schema, [1, 'string', 2] refute_valid schema, ['string', 1] end From 06709b0be790cb28522c021dac8b33bc304e8869 Mon Sep 17 00:00:00 2001 From: Joe Faber Date: Mon, 25 Jul 2016 18:28:04 -0400 Subject: [PATCH 2/3] Fix validation of partial tuples When 'items' is an array of schemas, allow 'items' and 'additionalItems' validations to pass even when the target array has fewer elements than items. --- CHANGELOG.md | 3 +++ lib/json-schema/attributes/additionalitems.rb | 2 +- lib/json-schema/attributes/items.rb | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd1b1f34..b240a46c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Made sure we really do clear the cache when instructed to - It's now possible to use reserved words in property names - Removed support for setting "extends" to a string (it's invalid json-schema - use a "$ref" instead) +- Relaxed 'items' and 'allowedItems' validation to permit arrays to pass even + when they contain fewer elements than the 'items' array. To require full tuples, + use 'minItems'. ### Changed - Made all `validate*` methods on `JSON::Validator` ultimately call `validate!` diff --git a/lib/json-schema/attributes/additionalitems.rb b/lib/json-schema/attributes/additionalitems.rb index a6c2ef9e..004c5f84 100644 --- a/lib/json-schema/attributes/additionalitems.rb +++ b/lib/json-schema/attributes/additionalitems.rb @@ -11,7 +11,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options case schema['additionalItems'] when false - if schema['items'].length != data.length + if schema['items'].length < data.length message = "The property '#{build_fragment(fragments)}' contains additional array elements outside of the schema when none are allowed" validation_error(processor, message, fragments, current_schema, self, options[:record_errors]) end diff --git a/lib/json-schema/attributes/items.rb b/lib/json-schema/attributes/items.rb index 7c3df6b6..b9084643 100644 --- a/lib/json-schema/attributes/items.rb +++ b/lib/json-schema/attributes/items.rb @@ -16,6 +16,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options when Array items.each_with_index do |item_schema, i| + next unless i < data.length schema = JSON::Schema.new(item_schema, current_schema.uri, validator) schema.validate(data[i], fragments + [i.to_s], processor, options) end From d9b0e593f5e2944cc2d65ff42db05cfa59428be8 Mon Sep 17 00:00:00 2001 From: Joe Faber Date: Wed, 27 Jul 2016 14:33:32 -0400 Subject: [PATCH 3/3] code review tweak --- lib/json-schema/attributes/items.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json-schema/attributes/items.rb b/lib/json-schema/attributes/items.rb index b9084643..a349c293 100644 --- a/lib/json-schema/attributes/items.rb +++ b/lib/json-schema/attributes/items.rb @@ -16,7 +16,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options when Array items.each_with_index do |item_schema, i| - next unless i < data.length + break if i >= data.length schema = JSON::Schema.new(item_schema, current_schema.uri, validator) schema.validate(data[i], fragments + [i.to_s], processor, options) end