voxpupuli / json-schema

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

Support for "not"? #250

Open krobi64 opened 9 years ago

krobi64 commented 9 years ago

I ran into the following circumstance to try to define a json-schema for the following. You can see the full conversation here: https://groups.google.com/forum/#!topic/json-schema/w7nb2WA0n_w

Any of the following json objects are valid:

{ "id": 23, "foo": [1,2,3], "bar":[4,5,6] }
{ "id": 42, "foo": [1, 2, 3] }
{ "id": 42, "bar": [1] }

However the following are invalid:

{ "id": 11 }
{ "id": 11, "foo": [] } or  { "id": 12, "bar": [] }
{ "id": 2, "foo": [], "bar": [] }

The first proposed solution was:

{
  "type": "object",
  "properties": {
    "id": { "type": "integer" },
    "foo": { "type": "array" },
    "bar": { "type": "array" }
  },
  "additionalProperties": false,
  "not":{
    "properties": {
      "foo": { "maxItems": 0 },
      "bar": { "maxItems": 0 }
    }
  }
}

This works on the following validation site: https://json-schema-validator.herokuapp.com/

However, this library allows the following object to incorrectly pass: { "id": 11 }

The website fails it correctly with:

[ {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : ""
  },
  "instance" : {
    "pointer" : ""
  },
  "domain" : "validation",
  "keyword" : "not",
  "message" : "instance matched a schema which it should not have"
} ]

I notice that there is not a test for the not keyword.

iainbeeston commented 9 years ago

There are some tests for not in test/test_jsonschema_draft4.rb and also test/test-suite/tests/draft4/not.json, however, none test the combination of not at the root level with maxItems.

Your detailed examples are very helpful - if we can write some tests that reproduce them we stand a chance of fixing the error.