user8479984
user8479984

Reputation: 501

JSON is not getting validated based on schema

I must be missing something here, but the below JSON is not getting validated against the schema. For example, the required attribute from the Java/JavaScript object is never getting enforced as per the schema. (FYI- Every language object may have other attributes or nested object)

However, if I completely remove the definition and directly put under array items each separately, then it validates.

I want to use 'definitions' and gets validated.The reason I have to put all the object in definitions and later I may have to put different language object in oneOf/allOf for other certain validation check.

Online check: Schema and JSON

JSON

{
  "languages": [
    {
      "lang": "Java",
      "trainer": "Peter"
    },
    {
      "lang": "JavaScript",
      "enrolled": "42",
      "available": "5"
    }
  ]
}

and the Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "languages"
  ],
  "properties": {
    "languages": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object"
      },
      "anyOf": [
        {
          "$ref": "#/definitions/Java"
        },
        {
          "$ref": "#/definitions/JavaScript"
        }
      ]
    }
  },
  "definitions": {
    "Java": {
      "required": [
        "trainer"
      ],
      "properties": {
        "lang": {
          "enum": [
            "Java"
          ]
        },
        "trainer": {
          "type": "string"
        }
      }
    },
    "JavaScript": {
      "required": [
        "enrolled",
        "available"
      ],
      "properties": {
        "lang": {
          "enum": [
            "JavaScript"
          ]
        },
        "enrolled": {
          "type": "string"
        },
        "available": {
          "type": "string"
        }
      }
    }
  }
}

The fixed schema and it works now

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "languages"
  ],
  "properties": {
    "languages": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "anyOf": [
          {
            "$ref": "#/definitions/Java"
          },
          {
            "$ref": "#/definitions/JavaScript"
          }
        ]
      }
    }
  },
  "definitions": {
    "Java": {
      "required": [
        "trainer"
      ],
      "properties": {
        "lang": {
          "enum": [
            "Java"
          ]
        },
        "trainer": {
          "type": "string"
        }
      }
    },
    "JavaScript": {
      "required": [
        "enrolled",
        "available"
      ],
      "properties": {
        "lang": {
          "enum": [
            "JavaScript"
          ]
        },
        "enrolled": {
          "type": "string"
        },
        "available": {
          "type": "string"
        }
      }
    }
  }
}

Upvotes: 0

Views: 168

Answers (2)

Octavian
Octavian

Reputation: 444

I think that the "array" definition is not correct. The objects that can be added in the array must be refereed in the "items", after you define the type of the items. Something like this:

"languages": {
        "type": "array",
        "minItems": 1,
        "items": {
            "type": "object",
            "anyOf": [
                {"$ref": "#/definitions/Java"},
                {"$ref": "#/definitions/JavaScript"}
            ]
        }
    }

Upvotes: 2

user8479984
user8479984

Reputation: 501

I have fixed myself

What I did: I have placed the json object blocks under items section of array.

Upvotes: 0

Related Questions