Json 架构不同的输入格式
Json Schema different input formats
我正在 AWS API 网关中创建一些模型。我遇到了一个问题,我希望它接收 2 种输入格式:一种格式只是字典,另一种是字典数组:
{
"id":"",
"name":""
}
和
[
{
"id":"",
"Family":""
},
{
"id":"",
"Family":""
},
...
{
"id":"",
"Family":""
}
]
到目前为止,我创建的模型只接受字典方式:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
请给我一些创建字典数组的技巧。我做了一些研究,但一无所获,但我遵循关键字 oneOf 和 anyOf 的方式,但我不确定。
您 anyOf
的方向是正确的。您应该做什么取决于本身的对象(字典)与数组中的对象之间的相似性。它们在您的示例中看起来不同,所以我会以实物回答,然后展示如何简化它们,如果它们实际上是相同的。
要使用 anyOf
,您需要捕获定义字典的关键字
{
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
并将其包装在架构根级别的 anyOf
中
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"anyOf": [
{
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
]
}
要为同类对象的数组编写架构,您需要 items
关键字。
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string"},
"Family": { "type": "string"}
},
"required": ["id"]
}
}
将它作为第二个元素添加到 anyOf
数组中,你就成功了。
如果您的单独对象可以与您的数组元素对象具有相同的模式,那么您可以将该模式作为定义编写一次并在两个地方引用它。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"definitions": {
"myObject": {
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
},
"anyOf": [
{ "$ref": "#/definitions/myObject" },
{
"type": "array",
"items": { "$ref": "#/definitions/myObject" }
}
]
}
我正在 AWS API 网关中创建一些模型。我遇到了一个问题,我希望它接收 2 种输入格式:一种格式只是字典,另一种是字典数组:
{
"id":"",
"name":""
}
和
[
{
"id":"",
"Family":""
},
{
"id":"",
"Family":""
},
...
{
"id":"",
"Family":""
}
]
到目前为止,我创建的模型只接受字典方式:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
请给我一些创建字典数组的技巧。我做了一些研究,但一无所获,但我遵循关键字 oneOf 和 anyOf 的方式,但我不确定。
您 anyOf
的方向是正确的。您应该做什么取决于本身的对象(字典)与数组中的对象之间的相似性。它们在您的示例中看起来不同,所以我会以实物回答,然后展示如何简化它们,如果它们实际上是相同的。
要使用 anyOf
,您需要捕获定义字典的关键字
{
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
并将其包装在架构根级别的 anyOf
中
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"anyOf": [
{
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
]
}
要为同类对象的数组编写架构,您需要 items
关键字。
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string"},
"Family": { "type": "string"}
},
"required": ["id"]
}
}
将它作为第二个元素添加到 anyOf
数组中,你就成功了。
如果您的单独对象可以与您的数组元素对象具有相同的模式,那么您可以将该模式作为定义编写一次并在两个地方引用它。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"definitions": {
"myObject": {
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
},
"anyOf": [
{ "$ref": "#/definitions/myObject" },
{
"type": "array",
"items": { "$ref": "#/definitions/myObject" }
}
]
}