当前位置: 动力学知识库 > 问答 > 编程问答 >

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?

网友答案:

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
        }
    }
    ]
}

Let me know if you have more questions.

分享给朋友:
您可能感兴趣的文章:
随机阅读: