Как использовать существующую схему JSON для свойств?
Мой продукт JSON:
{
"id": 123,
"variant":{
"id":123,
"name":"variant 1"
}
}
У меня есть схема JSON для объекта Variant, и мне нужно создать схему для объекта 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"
]
}
2 ответа
Решение
$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"
]
}
Просто используйте $ ref. При условии, что ваша вариантная схема определена под определениями узлов в том же файле, это будет выглядеть так:
"variant": {"$ref": "#/definitions/variant"}