Ben Yitzhaki
Ben Yitzhaki

Reputation: 1426

why doesn't json schema validate definitions defined in required attribute

I'm trying to create a json schema that validates an object depending on its type. It picks the right definition, however, it doesn't validate the required attributes in the selected definition. Here is the json schema i am trying:

{
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "literal": {
            "type": "object",
            "properties": {
                "raw": { "type": "string" }
            },
            "required": ["raw"],
            "additionalProperties": false
        },
        "identifier": {
            "type": "object",
            "properties": {
                "name": { "type": "string" }
            },
            "required": ["name"],
            "additionalProperties": false
        }
    },

    "type": "object",
    "oneOf": [
        {
            "type": "object",
            "properties": {
                "type": {
                    "enum": ["Literal"]
                },
                "content": { "$ref": "#/definitions/literal" }
            }
        },
        {
            "type": "object",
            "properties": {
                "type": {
                    "enum": ["Identifier"]
                },
                "content": { "$ref": "#/definitions/identifier" }
            }
        }
    ],
    "required": ["type"]
};

the following schema is valid, even tho its missing the "raw" property: { "type" : "Literal" }

thanks

Upvotes: 0

Views: 583

Answers (1)

vearutop
vearutop

Reputation: 4072

There is no keyword content in JSON Schema spec.

Once you have asserted "type":"object" in root schema, there is no need to do it again in subschema.

In order to combine object type enumerated value with associated extended definition, you need allOf keyword.

Also in definitions if you use "additionalProperties": false you have to list all properties of the object (see "type": {}). For previously defined/validated properties you can just use permissive schema: {} or true.

{
  "$schema": "http://json-schema.org/draft-07/schema#",

  "definitions": {
    "literal": {
      "properties": {
        "type": {},
        "raw": { "type": "string" }
      },
      "required": ["raw"],
      "additionalProperties": false
    },
    "identifier": {
      "properties": {
        "type": {},
        "name": { "type": "string" }
      },
      "required": ["name"],
      "additionalProperties": false
    }
  },

  "type": "object",
  "oneOf": [
    {
      "allOf": [
        {
          "properties": {
            "type": {
              "enum": ["Literal"]
            }
          }
        },
        {"$ref": "#/definitions/literal"}
      ]
    },
    {
      "allOf": [
        {
          "properties": {
            "type": {
              "enum": ["Identifier"]
            }
          }
        },
        {"$ref": "#/definitions/identifier" }
      ]
    }
  ],
  "required": ["type"]
}

Upvotes: 2

Related Questions