【问题标题】:How to use definitions in JSON schema (draft-04)如何在 JSON 模式中使用定义 (draft-04)
【发布时间】:2013-08-22 08:41:36
【问题描述】:

我正在使用的其余服务响应类似于以下示例,我在这里只包含了 3 个字段,但还有更多:

{
    "results": [
        {
            "type": "Person",
            "name": "Mr Bean",
            "dateOfBirth": "14 Dec 1981"
        },
        {
            "type": "Company",
            "name": "Pi",
            "tradingName": "Pi Engineering Limited"
        }
    ]
}

我想为上面(draft-04)编写一个 JSON 模式文件,它将明确指定:

if type == Person then list of required properties is ["type", "name", "dateOfBirth", etc] 
OR
if type == "Company" then list of required properties is ["type", "name", "tradingName", etc]

但是我找不到任何文档或如何做到这一点的示例。

目前我的 JSON 架构如下所示:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "required": ["results" ],
    "properties": {
        "results": {
            "type": "array",
            "items": {
                "type": "object",
                "required": ["type", "name"],
                "properties": {
                    "type": { "type": "string" },
                    "name": { "type": "string" },
                    "dateOfBirth": { "type": "string" },
                    "tradingName": { "type": "string" }
                }
            }
        }
    }
}

我应该如何处理的任何指针/示例。

【问题讨论】:

  • 我认为您应该用继承来描述这一点:Person 类型扩展 MyObject,Company 类型扩展 MyObject,并在您的主模式中定义一个包含 MyObject 类型项目的数组
  • 不知道 JSONSchema 与 XSD 有多么相似,但据我所知,关于 XSD,您的对象不需要通用超类。您宁愿在架构文件中继承。例如,您的主架构中的抽象占位符,这将允许人员架构和公司架构取代它。
  • 我看到依赖项也在草稿 3 中。谁能解释草稿 3 和草稿 4 之间的依赖关系有何不同?
  • 我认为 Bean 先生不是 81 年 12 月 14 日出生的。

标签: json jsonschema


【解决方案1】:

我认为推荐的方法是Json-Schema web, Example2 中显示的方法。您需要使用枚举“按值”选择模式。在你的情况下,它会是这样的:

{
    "type": "object",
    "required": [ "results" ],
    "properties": {
        "results": {
            "type": "array",
            "items": {
                "oneOf": [
                    { "$ref": "#/definitions/person" },
                    { "$ref": "#/definitions/company" }
                ]
            }
        }
    },
    "definitions": {
        "person": {
            "properties": {
                "type": { "enum": [ "person" ] },
                "name": {"type": "string" },
                "dateOfBirth": {"type":"string"}
            },
            "required": [ "type", "name", "dateOfBirth" ],
            "additionalProperties": false
        },
        "company": {
            "properties": {
                "type": { "enum": [ "company" ] },
                . . . 
            }        
        }
    }
}

【讨论】:

  • 如果你只知道结果数组属性的内容是一些未知的子类型,比如“派对”,你会用什么来代替 oneOf?相关问题张贴在这里stackoverflow.com/questions/19416873/… :)
【解决方案2】:

对不起,

我不明白这一点。问题是关于“依赖项”关键字,它是最后一个 JSON Schema 规范的一部分,对吧?

我在接受的答案中没有找到“依赖项” (?)

在最后的草稿中有简要说明。 但是http://usingjsonschema.com在书中解释了属性和定义的依赖关系:

http://usingjsonschema.com/assets/UsingJsonSchema_20140814.pdf

从第 29 页开始(参见第 30 页的解释)

"dependencies": {
     "shipTo":["shipAddress"],
     "loyaltyId":["loyaltyBonus"]
}

【讨论】:

  • 嗨 Sebastian,使用 dependencies 是否可以实现上述用例。 [请点击提问] (stackoverflow.com/questions/36830827/…) .是否可以根据另一个属性的值使该字段成为必填字段。如果您能回答链接中的问题,那就太好了。谢谢,哈利。
  • @Harry 我真的需要吗?几分钟后,“杰森”问的答案是完美的! (看你的问题)
  • @SebastianLasse,可能标题比较混乱,但是请阅读which will explicitly specify that之后的原题文字,你会发现答案解决了题主的要求。这个问题实际上与使用 json-schema dependencies 关键字无关。随意改进标题。
猜你喜欢
  • 2017-07-18
  • 2017-06-24
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多