OneOf validations

499 views
Skip to first unread message

Roman Fominych

unread,
Jan 5, 2017, 9:33:25 AM1/5/17
to JSON Schema
I have following JSON:
{
  "events": {
    "version": "0",
    "event": [
      {
        "eventid": "405069",
        "market": {
          "marketid": "472362",
          "marketNumber": "20",
        }
      },
      {
        "eventid": "405626",
        "market": [
          {
            "marketid": "473349",
            "marketNumber": "35"
          },
          {
            "marketid": "473350",
            "marketNumber": "36"

          }
        ]
      }
    ]
  }
}

and validating it against this schema:
{
  "description": "",
  "definitions": {
    "market": {
      "properties": {
        "marketid": {
          "type": "string",
          "minLength": 1
        },
        "marketNumber": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false,
      "required": [
        "marketid",
        "marketNumber"
      ]}
  },
  "type": "object",
  "properties": {
    "events": {
      "type": "object",
      "properties": {
        "version": {
          "type": "string",
          "minLength": 1
        },
        "event": {
          "type": "array",
          "uniqueItems": true,
          "minItems": 1,
          "items": {
            "additionalProperties": false,
            "required": [
              "eventid"
            ],
            "properties": {
              "eventid": {
                "type": "string",
                "minLength": 1
              },
              "market": {
                "oneOf": [
                  {"type": "object",
                   "$ref": "#/definitions/market"},
                  {
                    "type": "array",
                    "items": {"$ref": "#/definitions/market"}
                  }
                ]
              }
            }
          }
        }
      },
      "additionalProperties": false,
      "required": [
        "version",
        "event"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "events"
  ]
}

I validate using:

and get following error:
Found 1 error(s)
Message:
JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1.
Schema path:
#/properties/events/properties/event/items/0/properties/market/oneOf

Why both are valid here? Can element be Object and Array at the same time?

Roman Fominych

unread,
Jan 5, 2017, 10:23:49 AM1/5/17
to JSON Schema
Pay attention to "market" element:

in JSON it's "object" or "array", so in schema it's described as oneOf, "object" or "array"

but somehow validation triggers "true" on both of them at the same time

Roman Fominych

unread,
Jan 5, 2017, 10:50:18 AM1/5/17
to JSON Schema
simplified version, turns out it has nothing to do with oneOf, but with "definitions"

when this JSON

{
  "events": {
    "version": "0",
    "event": [
      {
        "eventid": "405626",
        "market": [ 
          {
            "marketid": "473349",
            "marketNumber": "35"
          },
          {
            "marketid": "473350",
            "marketNumber": "36"

          }
        ]
      }
    ]
  }
}

is validated against this schema:
                  {"type": "object",
                    "$ref": "#/definitions/market"}
             }
          }
        }
      },
      "additionalProperties": false,
      "required": [
        "version",
        "event"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "events"
  ]
}


then surprise - "No errors found. JSON validates against the schema"

Roman Fominych

unread,
Jan 5, 2017, 10:55:45 AM1/5/17
to JSON Schema
solved, "allOf" should be added:

              "market": { "allOf": [
                  {"type": "object" } ,
                  {"$ref": "#/definitions/market"} ]}
             }

then JSON

{
  "events": {
    "version": "0",
    "event": [
      {
        "eventid": "405626",
        "market": [ 
          {
            "marketid": "473349",
            "marketNumber": "35"
          },
          {
            "marketid": "473350",
            "marketNumber": "36"

          }
        ]
      }
    ]
  }
}


validation will fail with:
  Found 2 error(s)
Message:
JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0.
Schema path:
#/properties/events/properties/event/items/0/properties/market/allOf
Message:
Invalid type. Expected Object but got Array.
Schema path:
#/properties/events/properties/event/items/0/properties/market/allOf/0/type
Reply all
Reply to author
Forward
0 new messages