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

Empty array (incorrectly?) triggering ValidationError #311

Closed
jnardone opened this issue Feb 25, 2016 · 3 comments
Closed

Empty array (incorrectly?) triggering ValidationError #311

jnardone opened this issue Feb 25, 2016 · 3 comments

Comments

@jnardone
Copy link

I am trying to validate a schema which should have a required key but may either have a populated array (of objects!) OR an empty array. (Using 2.6.0)

Here's my simple repro case. Schema first. Note that my array, if it had elements, would be an array of objects and not simple elements.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "x": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "a": {
              "type": "string"
            }
          },
          "required": ["a"],
          "additionalProperties": false
        }
      ]
    }  
  },
  "required": [ "x" ],
  "additionalProperties": false
}

The populated case succeeds: {"x": [ {"a":"foo"} ] }

However, the empty array case of {"x": [] }' fails with
JSON::Schema::ValidationError: The property '#/x/0' of type NilClass did not match the following type: object in schema file:///zzz/schemas/schema.json
from /zzz/vendor/bundle/ruby/2.3.0/gems/json-schema-2.6.0/lib/json-schema/attribute.rb:18:in `validation_error'

Am I making a fundamental error with my schema, or is this a bug? I'm new at this but the way I read the spec, the empty array should be allowed in this case...

@jnardone
Copy link
Author

Ok, I'm still learning here.
I think my solution is to define the inner array as allowing both object and null as choices, e.g.

    "x": {
      "type": "array",
      "items": [
        {
          "type": ["object", "null"],

though I wouldn't mind someone validating that...

@RST-J
Copy link
Contributor

RST-J commented Feb 26, 2016

The problem is that you specified the array items within an array, you need to specify it in an object.
Specifying an object schema here means that all of the 0..n elements within the array must adhere to the given schema (no element is always valid to any schema).
But if you specify an array of schemas, the elements at the respective index position must adhere to the schema at the same position. http://json-schema.org/latest/json-schema-validation.html#anchor36

@jnardone
Copy link
Author

Oh, I see! Thanks!

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

No branches or pull requests

2 participants