voxpupuli / json-schema

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

Schema nesting with id key is not validating as expected #270

Closed pelted closed 8 years ago

pelted commented 8 years ago

Given the simple schema bellow it is expected to not validate because of the foo required key in the nested attributes object but it in fact does validate when the response does not contain foo.

Doing some digging, it appears the reason may have to do with the id property being returned in the response.

Schema

{
  "$schema": "http://json-schema.org/draft-04/hyper-schema",
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "required": ["id", "type", "attributes"],
      "properties": {
        "id" : { "type" : "string" },
        "type" : { "type" : "string" },
        "attributes" : { "type" : "object" }
      },
      "attributes": {
        "type": "object",
        "required": ["user_name", "email", "auth_token", "foo"]
      }
    }
  }
}

Response

{
  "data":{
    "id": "1",
    "type": "users",
    "attributes": {
      "user_name": "admin", 
      "email":"admin@example.com"
    }
  }
}
RST-J commented 8 years ago

I think you confuse the top level attributes property definition with the nested attributes within the data property. Your response is valid w.r.t. to the schema because the only thing it requires for a property of name attributes within the data property is to be an object, which it is.

Your expectation would hold if you had a response like this:

{
  "data":{
    "id": "1",
    "type": "users"
  },
  "attributes": {
    "user_name": "admin", 
    "email":"admin@example.com"
  }
}
pelted commented 8 years ago

Complete fail on my part it would appear. Many examples I found seem to show the schema nested properties outside of the object but your response pretty much cleared it all up for me. I wonder then if the new reference to #178 from 3 days ago is something similar.