Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item partial tuples #348

Merged
merged 3 commits into from
Sep 29, 2016
Merged

Conversation

jlfaber
Copy link
Contributor

@jlfaber jlfaber commented Jul 25, 2016

Currently, when 'items' is an array of schemas, validation will fail if the target array has fewer elements than the 'items' array. This conflicts with the spec, which allows for partial (or even empty) tuples to pass validation unless 'minItems' is also set.

See the relevant section of Understanding JSON Schema for examples of this. Note specifically the statement: "It's okay to not provide all the items."

This PR modifies the array_validation tests to check for this condition and fixes the corresponding code in the attributes directory.

@jlfaber
Copy link
Contributor Author

jlfaber commented Jul 26, 2016

CI failing due to an issue fixed by #350.

@jlfaber jlfaber force-pushed the item_partial_tuples branch from 91f328e to acd777d Compare July 27, 2016 14:35
@jlfaber
Copy link
Contributor Author

jlfaber commented Jul 27, 2016

CI now passes. Also updated the common test suite ref.
@iainbeeston, this should be ready to go.

@@ -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
Copy link
Contributor

@RST-J RST-J Jul 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be replaced by break if i >= data.length?

@RST-J
Copy link
Contributor

RST-J commented Jul 27, 2016

👍

@rymndhng
Copy link

@jlfaber any chance you can fix up this PR -- I'd love to get this fix in :)

@jlfaber jlfaber force-pushed the item_partial_tuples branch from ab384f4 to 0cd6ec0 Compare September 17, 2016 03:02
@jlfaber
Copy link
Contributor Author

jlfaber commented Sep 17, 2016

I've rebased, but this is now failing (again) due to another unrelated issue. #357

@iainbeeston
Copy link
Contributor

It looks like something changed in the common test suite, that's also causing master to fail. I'll take a closer look at that issue

@iainbeeston
Copy link
Contributor

@jlfaber I've figured out why the tests are failing on master. Once #360 is merged, and you rebase on top of that, the tests here should pass

@RST-J
Copy link
Contributor

RST-J commented Sep 28, 2016

#360 is merged

Joe Faber and others added 3 commits September 28, 2016 18:06
When 'items' is an array of schemas, allow 'items' and 'additionalItems'
validations to pass even when the target array has fewer elements than items.
@jlfaber jlfaber force-pushed the item_partial_tuples branch from 0cd6ec0 to d9b0e59 Compare September 28, 2016 22:08
@jlfaber
Copy link
Contributor Author

jlfaber commented Sep 28, 2016

@iainbeeston tests now passing -- ready to merge. Thx for the fix in #360 !

@iainbeeston
Copy link
Contributor

Great - I'll take one last look at this today and hopefully get it merged and released

@iainbeeston iainbeeston merged commit e5c3a1a into voxpupuli:master Sep 29, 2016
@iainbeeston
Copy link
Contributor

Thanks @jlfaber I've merged that, and I'll try to get it released soon.

Interestingly the original json-schema spec is very vague about how this should work. I'm going to add some extra tests to the common test suite so we can be sure we are doing the right thing

@iainbeeston
Copy link
Contributor

My PR on the common test suite is json-schema-org/JSON-Schema-Test-Suite#127

@jlfaber jlfaber deleted the item_partial_tuples branch September 29, 2016 12:18
@jlfaber
Copy link
Contributor Author

jlfaber commented Sep 29, 2016

Thanks @iainbeeston for the merge.

The current schema validation spec (http://json-schema.org/latest/json-schema-validation.html#anchor37) is pretty specific about how items should work. Note the example 5.3.1.3 which shows this schema:

{
  "items": [ {}, {}, {} ],
  "additionalItems": false
}

and these examples of valid instances:

[]
[ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ]
[ 1, 2, 3 ]

The valid arrays have zero, two, and three elements, respectively.

@iainbeeston
Copy link
Contributor

Yes that's true, but what I was thinking of was that it doesn't explain how the schemas in items should be used to validate the contents of the array. Here's the best I can find:

if the value of "additionalItems" is boolean value false and the value of "items" is an array, the instance is valid if its size is less than, or equal to, the size of "items".

It seems safe to assume that each consecutive array element should match the schema in items at the same position, but then what happens when there are more elements in the array than there are items in the schema? (only relevant if additionalItems is false)

@jlfaber
Copy link
Contributor Author

jlfaber commented Sep 29, 2016

Did you mean when additionalItems is true? If it's false (meaning additional items are not allowed) and there are more elements in the array than there are in the schema, then that's a clear failure.

This section (http://json-schema.org/latest/json-schema-validation.html#anchor131) offers a bit more guidance about validating the elements of an array against an items schema:

8.2.3.2. If "items" is an array
In this situation, the schema depends on the index: if the index is less than, or equal to, the size of "items", the child instance must be valid against the corresponding schema in the "items" array; otherwise, it must be valid against the schema defined by "additionalItems".

So to summarize: items can be either a single schema object, which is used to validate all elements of the subject array, or an array of schema objects, each of which is used to validate the corresponding element of the subject array if it exists. The items attribute itself imposes no restrictions on the length of the subject array.

When items is not present or is a single schema object, additionalItems is disregarded.

When items is an array, additionalItems may be either true (the default, which imposes no restrictions at all), false (which requires that the subject array length be no larger than the length of the items array), or a single schema object (which is used to validate any subject array elements that do not correspond to a schema in the items array).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants