voxpupuli / json-schema

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

typeOf array doesn't validate references #281

Closed thewatts closed 8 years ago

thewatts commented 8 years ago

Payload:

{
  "event": {
    "id": 1,
    "name": "Block Party"
  }
}

Example Schema:

// base
{
  "type": "object",
  "required": [
    "event"
  ],
  "properties": {
    "event": {
      "typeOf": [
        {"type": "null"},
        {"$ref": "event-simple.json"}
      ]
    }
  }
}
// event-simple.json
{
  "type":"object",
  "required":[
    "id",
    "name",
    "starts_at"
  ],
  "properties": {
    "id": { "type": "fixnum" },
    "name": { "type": "string" },
    "starts_at": { "type": "string" }
  }
}

The reference event-simple.json isn't validated against with the json payload. (( using strict mode ))

thewatts commented 8 years ago

If one of the contribs could give me a quick rundown of how validations happen with the project, I don't mind rolling up my sleeve and debugging / bug fixing / feature adding.

iainbeeston commented 8 years ago

I'm a bit confused by what you're doing in your schema, did you mean allOf instead of typeOf? (So far as I'm aware, typeOf isn't a recognised property in json schema)

thewatts commented 8 years ago

@iainbeeston -- Hmm... I must have misread something somewhere about stating that an item is either one thing or the other.

Would the correct alternative be to use oneOf instead?

{
  "type": "object",
  "required": [
    "event"
  ],
  "properties": {
    "event": {
      "oneOf": [
        {"type": "null"},
        {"$ref": "event-simple.json"}
      ]
    }
  }
}
iainbeeston commented 8 years ago

But isn't that the same as saying that event is not required? Could you just remove the required property and define the event property as

"event": { "$ref": "event-simple.json" }

If you need any help with json schema I recommend Understanding JSON Schema

https://spacetelescope.github.io/understanding-json-schema/

iainbeeston commented 8 years ago

I'm going to close this for now. If you need any questions about the json schema format itself I recommend checking the json schema Google group

https://groups.google.com/forum/m/#!forum/json-schema

thewatts commented 8 years ago

Thanks, @iainbeeston! I think part of the problem is that I had been validating with strict mode enabled.