Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "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"
    }
  }
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading