【问题标题】:json-schema-validator not validating all items of an array but only the first onejson-schema-validator 不验证数组的所有项目,但只验证第一个
【发布时间】:2017-02-15 15:12:36
【问题描述】:

我有以下两种模式:

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

下面是针对 A.json 验证的 json 请求:

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

我的问题是,当我将 ArgumentInt 的值作为字符串传递时,它会失败,因为它接受整数值,我可以在报告消息中看到它。 但是,当我将 ArgumentString 的值作为整数传递时,它仍然失败,但我在消息中看不到由于输入错误类型而失败。 我猜只有ArgumentChoice 中的第一个数组元素会针对模式进行验证,因为如果我将ArgumentString 放在ArgumentInt 上方并且ArgumentString 中的值类型错误,它会失败。 我做错了吗?

【问题讨论】:

    标签: json schema


    【解决方案1】:

    我从 A.json 和 B.json 创建了组合模式以进行在线测试。 我也能收到第二种情况的错误消息。

    组合架构

    {
      "$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"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    使用的输入

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

    如果您还有其他问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 2018-07-05
      • 2021-04-18
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多