【问题标题】:jsonschema ref as direct parent schema fieldsjsonschema ref 作为直接父模式字段
【发布时间】:2017-11-09 22:57:10
【问题描述】:

我有两个 json 模式:

//person schema
{
  "id": "/person",
  "type": "object",
  "properties": {
    "name": {"type": "string"},
    "baseFields": {"$ref": "/baseFields"}
  },
  "additionalProperties": false
}


//baseFields schema
{
  "id": "/baseFields",
  "type": "object",
  "properties": {
    "age": {"type": "string"},
    "hobby": {"type": "string"}
  },
  "additionalProperties": false
}

以下对象将通过“person scema”验证:

{
    "name":"person1",
    "baseFields":{
    "age":"33",
    "hobby":"diving"
    }
}

我需要的是下面的对象来通过'person scema'验证:

{
    "name":"person1",
    "age":"33",
    "hobby":"diving"
}

我需要它,因为我很少有与几个不同架构相关的字段

谢谢

【问题讨论】:

    标签: schema jsonschema json-schema-validator


    【解决方案1】:

    您正在尝试做的是继承。但是 JSON 模式中没有继承。

    您可以使用“allOf”关键字。但它有一些陷阱。有关示例和更多信息,请参见 here(查看第三个示例,其中包含“street_address、city 和 state”字段)。

    还有check this answer in so

    【讨论】:

    • 我在你的第一个链接中看到他们将发布一个继承解决方案:“它不像面向对象语言中的继承。在下一个版本中有一些解决这个问题的建议JSON模式规范”。下一个版本即将发布时,我现在该怎么办?
    • 我会对该答案添加评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多