voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.54k stars 243 forks source link

Item partial tuples #348

Closed jlfaber closed 8 years ago

jlfaber commented 8 years ago

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 commented 8 years ago

CI failing due to an issue fixed by #350.

jlfaber commented 8 years ago

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

RST-J commented 8 years ago

:+1:

rymndhng commented 8 years ago

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

jlfaber commented 8 years ago

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

iainbeeston commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

360 is merged

jlfaber commented 8 years ago

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

iainbeeston commented 8 years ago

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

iainbeeston commented 8 years ago

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 commented 8 years ago

My PR on the common test suite is https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/127

jlfaber commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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).