Skip to content

Commit e5c3a1a

Browse files
jlfaberiainbeeston
authored andcommitted
Item partial tuples (#348)
* Test for partial tuple validation * 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. * code review tweak
1 parent b16f10c commit e5c3a1a

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
- Made sure we really do clear the cache when instructed to
1010
- It's now possible to use reserved words in property names
1111
- Removed support for setting "extends" to a string (it's invalid json-schema - use a "$ref" instead)
12+
- Relaxed 'items' and 'allowedItems' validation to permit arrays to pass even
13+
when they contain fewer elements than the 'items' array. To require full tuples,
14+
use 'minItems'.
1215

1316
### Changed
1417
- Made all `validate*` methods on `JSON::Validator` ultimately call `validate!`

lib/json-schema/attributes/additionalitems.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
1111

1212
case schema['additionalItems']
1313
when false
14-
if schema['items'].length != data.length
14+
if schema['items'].length < data.length
1515
message = "The property '#{build_fragment(fragments)}' contains additional array elements outside of the schema when none are allowed"
1616
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
1717
end

lib/json-schema/attributes/items.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
1616

1717
when Array
1818
items.each_with_index do |item_schema, i|
19+
break if i >= data.length
1920
schema = JSON::Schema.new(item_schema, current_schema.uri, validator)
2021
schema.validate(data[i], fragments + [i.to_s], processor, options)
2122
end

test/support/array_validation.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def test_items_multiple_schemas
2525
assert_valid schema, ['b', 1]
2626
assert_valid schema, ['b', 1, nil]
2727
refute_valid schema, [1, 'b']
28-
refute_valid schema, []
28+
assert_valid schema, []
29+
assert_valid schema, ['b']
30+
assert_valid schema, ['b', 1, 25]
2931
end
3032

3133
def test_minitems
@@ -62,6 +64,8 @@ def test_additional_items_false
6264
}
6365

6466
assert_valid schema, [1, 'string']
67+
assert_valid schema, [1]
68+
assert_valid schema, []
6569
refute_valid schema, [1, 'string', 2]
6670
refute_valid schema, ['string', 1]
6771
end

0 commit comments

Comments
 (0)