JSON Schema Grammar Not Validating Enums

I have defined the following JSON Schema Grammar which has to take "TypeA" type of elements and none other than that.

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "title": "Schema definition",
  "type": "object",
  "scname": "string",
  "properties": {
    "itemInfo": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "$ref": "#/definitions/TypeADefinition"
          }
        ]
      }
    }
},
  "required": [
    "itemInfo"
  ],
  "definitions": {
    "TypeADefinition": {
      "type": "object",
      "properties": {
        "elementOf": {
          "types": {
            "enum": [
              "TypeA"
            ]
          }
        },
        "elements": {
          "items": {
            "oneOf": [
              {
                "$ref": "#/definitions/TypeAElementDefinition"
              }
            ]
          },
          "type": "array"
        }
      }
    },
    "TypeAElementDefinition": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "maxLength": 128
        }
      },
      "required": [
        "name"
      ],
      "additionalProperties": false
    }
  }
}

JSON Object 1:

{
  "itemInfo": [
    {
      "elementOf": "TypeA",
      "elements": [
        {
          "name": "John Doe"
        }
      ]
    }
  ]
}

JSON Object 2:

{
  "itemInfo": [
    {
      "elementOf": "TypeB",
      "elements": [
        {
          "name": "John Doe"
        }
      ]
    }
  ]
}

Both of these JSON objects are getting validated by the JSON grammar that I have defined but only the first JSON object should be validated successfully by the grammar the second JSON should not validated as it has elementOf "TypeB".

Is there anything that is missing in my Schema Grammar?

>Solution :

There is a mistake in the definition of elementOf property in TypeADefinition. The types property should be type. Also, the enum value should be in square brackets like this:

"elementOf": {
  "type": "string",
  "enum": [
    "TypeA"
  ]
}

The corrected definition will look like this:

"TypeADefinition": {
  "type": "object",
  "properties": {
    "elementOf": {
      "type": "string",
      "enum": [
        "TypeA"
      ]
    },
    "elements": {
      "items": {
        "oneOf": [
          {
            "$ref": "#/definitions/TypeAElementDefinition"
          }
        ]
      },
      "type": "array"
    }
  }
},

Leave a Reply