【问题标题】:Correct JSON Schema for an array of items of different type为不同类型的项目数组正确的 JSON Schema
【发布时间】:2013-03-02 00:48:08
【问题描述】:

我有一个无序的 JSON 项目数组。根据规范https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-03#section-5.5,下面的 json 模式将仅验证数组中的对象是否按该顺序出现。我不想指定顺序,只需验证数组中的对象,无论对象的顺序或数量如何。从规范来看,我似乎无法理解这是如何完成的。

"transactions" : {
    "type" : "array",
    "items" : [
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BUILD", "REASSIGN"]
                }
            }
        },
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BREAK"]
                }
            }
        }
    ]
}

【问题讨论】:

  • 好吧,这个 JSON 从一开始就不是有效的。
  • 你能指出具体的无效部分吗?这是一个更大的 JSON 模式文件的摘录,它本身可以很好地通过 json lint。也许有一个我看不到的错字?我认为这不值得投反对票 - 你可以提出修改建议。
  • 发现无效 - 当我从较大的文件中摘录 JSON 时的副作用。
  • 最好将固定版本发布为您的答案(如果这解决了您的问题),以防将来有人提出这个问题。
  • 您也可以使用此站点检查您的 JSON 是否存在问题:jsonlint.com

标签: json validation jsonschema


【解决方案1】:

就我而言,我希望数组中的第一个元素具有特定格式,其余元素具有另一种格式。这是我的解决方案:

my_schema = {
    "type": "object",
    "properties": {
        "token": {"type": "string"},
        "service_id": {"type": "string"},
        "promo_code": {"type": "string"},
        "path": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"}
                    },
                    "required": ["address", "lat", "lng"]
                },
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"},
                        "district_id": {"type": "number"},
                        "ward_code": {"type": "number"},
                        "weight": {"type": "number"}
                    },
                    "required": ["address","lat", "lng","ward_code", 
                                 "district_id", "weight"]
                }
            ]
        }
    },
    "required": ["token", "service_id", "path"]
}

上述模式表示从路径的第二个元素开始,我需要 District_id、ward_code、权重

【讨论】:

  • 对我来说,这并没有将模式应用于路径数组中第二个对象之后的对象。
【解决方案2】:

作为对用户 Vdex 的回应: 这并不等同,您所写的意思是数组元素在数组中以此特定顺序 出现。

如果您使用this schema validator,则需要正确的实现。

使用此架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": [
    {
      "type": "boolean"
    },
    {
      "type": "number"
    },
    {
      "type": "string"
    }
  ]
}

将验证此 JSON:

[
  true,
  5,
  "a",
  "6",
  "a",
  5.2
]

但不是这个:

[
  5,
  true,
  "a",
  "6",
  "a",
  5.2
]

因此,目标与“oneOf”等关键字完全不同。

【讨论】:

  • This JSON will be validated。但是您如何设置该文件的架构?您不能使用对象并设置"$schema": "./my.schema.json",否则它不再有效,因为它是对象而不是数组。省略这个,IDE 没有文件的架构...
【解决方案3】:

我在 JSON schema google group 上问过同样的问题,很快就得到了回答。用户 fge 要求我在此处发布他的回复:

你好,

当前的规范是草案 v4,而不是草案 v3。更多的 具体来说,验证规范在这里:

https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00

网站不是最新的,我不知道为什么...我会提交一个拉 请求。

有了草稿 v4,你可以使用这个:

{
    "type": "array",
    "items": {
        "oneOf": [
            {"first": [ "schema", "here" ] }, 
            {"other": [ "schema": "here" ] }
        ]
    }  
}

例如,这是一个数组的模式,其中项目可以是 字符串或整数(不过可以用更简单的方式编写):

{
    "type": "array",
    "items": {
        "oneOf": [
            {"type": "string"},
            {"type": "integer"}
        ]
    }
}

这是正确的答案。我更正的架构现在包括:

"transactions" : {
    "type" : "array",
    "items" : {
        "oneOf" : [
            {
                "type" : "object",
                "properties" : {
                    "type" : {
                        "type" : "string",
                        "enum" : ["BUILD", "REASSIGN"]
                    }
                }
            },
            {
               "type" : "object",
               "properties" : {
                 "type" : {
                   "type" : "string",
                   "enum" : ["BREAK"]
                  }
               }
            }
        ]
    }
}

【讨论】:

    【解决方案4】:

    对于任何坚持草案 3 架构的人。有一个“Type”关键字相当于草案4中的“anyOf”:

    所以你可以使用

    {
        "fooBar" : {
            "type" : "array",
            "items" : {
                "type" : [{
                        "type" : "object",
                        "properties" : {
                            "foo" : {                           
                                "type" : "string"
                            }
                        }
                    }, {
                        "type" : "object",
                        "properties" : {
                            "bar" : {
                                "type" : "string"
                            }
                        }
                    }
                ]
            }
        }
    }
    

    【讨论】:

      【解决方案5】:

      我也研究了很长时间。但一直未能找到可行的解决方案。如果您只有一个架构,例如,它可以正常工作。

      "transactions" : {
                "type" : "array",
                "items" : 
                {
                  "type" : "object",
                  "properties" : {
                    "type" : {
                      "type" : "string",
                      "enum" : ["BREAK"]
                    },
                }
      }
      

      然后您只需跳过数组括号,并使用一个对象。但是,如果你想做你正在做的事情,似乎没有可靠的答案。这是我目前发现的唯一东西:http://the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-09
        • 1970-01-01
        • 2012-11-02
        • 1970-01-01
        • 2014-09-05
        • 2017-08-06
        • 2016-04-24
        相关资源
        最近更新 更多