user655321
user655321

Reputation: 1742

JSON Schema object with primitive type or $ref type?

I have a schema in which a particular property (child) can be either a string or it can be an object I already have defined (ChildClass). I'm having a hard time defining this in the schema, however:

{
  definitions": {
    "ChildClass": { ... },
    "ParentClass": {
      "description": "The parent object",
      "type": [ "object" ],
      "properties": {
        "child": {
          "anyOf": [
            { "$ref": "#/definitions/ChildClass" },
            "string"
          ]
        }
      }
    }
  }
}

I can use either the "string" definition or my referenced definition, but not both together (with anyOf). What's the appropriate syntax to allow the schema to understand that either one of these is valid?

Upvotes: 3

Views: 370

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

The anyOf keyword requires schemas as its items. "string" is not in itself a schema. Try using

{"type": "string"}

inside the anyOf alongside your ChildClass reference.

Upvotes: 2

Related Questions