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..a349c293 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| + 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 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