我的产品json是:
{
"id": 123,
"variant":{
"id":123,
"name":"variant 1"
}
}
我有 Variant 对象的json架构,我需要为 Product 对象创建架构。如何在产品架构中使用现有的变体架构?
实施例:
产品(未优化)架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://example.com/scheme/product",
"type": "object",
"title": "Product",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "Product",
"properties": {
"id": {
"id": "http://example.com/scheme/product/id",
"type": "integer",
"title": "Id schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "id"
},
"variant": {
"id": "http://example.com/scheme/product/variant",
"type": "object",
"title": "Variant schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "variant",
"properties": {
"id": {
"id": "http://example.com/scheme/product/variant/id",
"type": "integer",
"title": "Id schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "id"
},
"name": {
"id": "http://example.com/scheme/product/variant/name",
"type": "string",
"title": "Name schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "name"
}
}
}
},
"required": [
"id",
"variant"
]
}
变体架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://example.com/scheme/variant",
"type": "object",
"title": "Variant",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "Variant",
"properties": {
"id": {
"id": "http://example.com/scheme/variant/id",
"type": "integer",
"title": "Id schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "id"
},
"name": {
"id": "http://example.com/scheme/variant/name",
"type": "string",
"title": "Name schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "name"
}
},
"required": [
"id",
"name"
]
}
答案 0 :(得分:1)
$ref
救援。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://example.com/scheme/product",
"type": "object",
"title": "Product",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "Product",
"properties": {
"id": {
"id": "http://example.com/scheme/product/id",
"type": "integer",
"title": "Id schema.",
"description": "An explanation about the puropose of this instance described by this schema.",
"name": "id"
},
"variant": {
"$ref": "http://example.com/scheme/product/variant"
}
},
"required": [
"id",
"variant"
]
}
答案 1 :(得分:0)
只需使用$ref即可。假设您的变体模式是在同一文件中的节点定义下定义的,它将是这样的:
"variant": {"$ref": "#/definitions/variant"}