Joe Shanahan
Joe Shanahan

Reputation: 816

Do not understand this type definition in a valid JSON Schema

For the definitions of Cats and Dogs in:

{
  "Boxes": {
        "type":"object",
        "properties":   {
                        "Cats": {"type":["integer","null"]},
                        "Dogs": {"type":["integer","null"]}
                        }
        }
}

What restraints does "type": [ "integer", "null" ] impose?

The following JSON validates against this schema: { "Boxes": { "Cats": [2, 3, 4, null, "hi"] } }. Since Cats contains an array which in turn contains ints, strings and null, I would have assumed that the validation would have failed.

Upvotes: 0

Views: 58

Answers (1)

cloudfeet
cloudfeet

Reputation: 12863

Firstly, if you want your data to have a top-level property named "Boxes", then you need to define the top-level data to be an object with that property, e.g.

{
    "type": "object",
    "properties": {
        "Boxes": {
            "type":"object",
            "properties": {
                "Cats": {"type":["integer","null"]},
                "Dogs": {"type":["integer","null"]}
            }
        }
    }
}

With the schema you've written, the top-level "Boxes" is ignored because it's not a keyword, so the schema effectively has no constraints at all.

If you used the above construction, then the "Cats" and "Dogs" properties would be constrained to be either an integer or null.

Arrays would not be allowed - if you want an array then you should define "type":"array" for these properties and then constrain the array items using items.

Upvotes: 2

Related Questions