Shobhit Ranjan
Shobhit Ranjan

Reputation: 83

json-schema-validator not validating all items of an array but only the first one

I have below two schemas :

A.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type":"object",
    "properties":
    {
        "ArgumentChoice":{
            "type" : "array",
            "items" : {"$ref" : "B.json"}
        }
    }
}

B.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title" : "ArgumentChoiceType",
    "type":"object",
    "properties":{
        "ArgumentInt" : {
            "type" : "object",
            "properties":{
                "Value":{
                    "type" : "integer"
                }
            }
        },
        "ArgumentString" : {
            "type" : "object",
            "properties":{
                "Value":{
                    "type" : "string"
                }
            }
        }
    }
}

Below is the json request that is validated against A.json :

{
    "ArgumentChoice" : [
    {
        "ArgumentInt" : {
            "Value" : 1
        }
    },
    {
        "ArgumentString" :
        {
            "Name" : "JOB_NAME",
            "Value" : "test"
        }
    }
    ]
}

My problem is that when I pass Value of ArgumentInt as string, it fails because it accepts integer value and I can see it in the report message. But when I pass Value of ArgumentString as integer it still fails, but I cannot see in the message that it failed due to wrong type entered. I guess only the first array element in ArgumentChoice is getting validated against the schema because it fails if I place ArgumentString above ArgumentInt with the wrong value type in ArgumentString. Am I doing something wrong?

Upvotes: 0

Views: 1051

Answers (1)

Ganesh
Ganesh

Reputation: 6127

I created the combined schema from A.json and B.json to test online. I am able to get the error message for the 2nd case as well.

Combined Schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "ArgumentChoice": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "ArgumentInt": {
            "type": "object",
            "properties": {
              "Value": {
                "type": "integer"
              }
            }
          },
          "ArgumentString": {
            "type": "object",
            "properties": {
              "Value": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

Input Used

{
    "ArgumentChoice" : [
    {
        "ArgumentInt" : {
            "Value" : "test"
        }
    },
    {
        "ArgumentString" :
        {
            "Name" : "JOB_NAME",
            "Value" : 1
        }
    }
    ]
}

enter image description here Let me know if you have more questions.

Upvotes: 1

Related Questions