voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 242 forks source link

2.7.0 fails validating not required fields #364

Closed ktaras closed 7 years ago

ktaras commented 7 years ago

After updating from 2.6.2 to 2.7.0 it fails validating not required fields. For example, I have following scheme:

{
  "type": "object",
  "required": ["data"],
  "properties": {
    "data": {
      "type": "object",
      ...
    },
    "included": {
      "type": "array"
    }
  }
}

So, as I understand the key included shouldn't be required, but when I run tests I get an error:

The property '#/' did not contain a required property of 'included'
iainbeeston commented 7 years ago

Sorry I'm having trouble reproducing that with 2.7.0, can you give me a code example that reproduces the error?

Here's what I'm doing:

require "json-schema"

schema = {
  "type" => "object",
  "required" => ["data"],
  "properties" => {
    "data" => {
      "type" => "object"
    },
    "included": {
      "type": "array"
    }
  }
}

JSON::Validator.validate!(schema, { "data" => {} }) # returns true
ktaras commented 7 years ago

I have strict: true and it fails:

JSON::Validator.validate!(schema, { "data" => {} }, strict: true)

It seems like I got it - if you add strict: true it doesn't matter which fields specified as required in scheme - it requires all of them and just ignores that line. And if you don't use strict: true you can specify exact fields in scheme which are required, am I right?

iainbeeston commented 7 years ago

Yes, then that's expected behaviour (and it should have been the case in json-schema 2.6.2 as well?).

This is what it says in the readme:

with the :strict option, all properties are condisidered to have "required": true and all objects "additionalProperties": false

ktaras commented 7 years ago

Yes, sorry, my fault. But in 2.6.2 it worked fine, strange thing :)

iainbeeston commented 7 years ago

We did fix a lot of bugs in the last release, possibly it was working because of a bug