我试图找到如何定义一个可以由以下对象之一表示的模型:
{
"name": "John Doe",
"additional_info": {
"scheme": {
"id": 1
},
"source": "direct"
}
}
{
"name": "John Doe",
"additional_info": {
"scheme": {
"id": 1
},
"source": "direct",
"optional_key": "something"
}
}
{
"name": "John Doe",
"additional_info": {}
}
所以我需要确保additional_info
对象OR可能是空的,OR必须包含至少两个必需的键scheme
(和scheme
对象必须包含id
键)和source
, AND可以包含任何可选的键。
我尝试了以下方案:
model:
description: ''
content:
application/json:
schema:
allOf:
- type: object
properties:
name:
type: string
- oneOf:
- type: object
properties:
additional_info:
type: object
properties:
scheme:
type: object
required:
- id
properties:
id:
type: integer
source:
type: string
required:
- scheme
- source
- type: object
properties:
additional_info:
type: object
additionalProperties: false
examples:
example-1:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
example-2:
name: John Doe
additional_info: {}
example-3:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
optional_key: something
但我不确定它是否正确。
我尝试了不同的选项,并找到了正确的一个,它的工作。也许这是多余的,并且在某些地方您不需要使用minProperties和maxProperties关键字。
在这里:
model:
description: ''
content:
application/json:
schema:
allOf:
- type: object
properties:
name:
type: string
required:
- name
- oneOf:
- type: object
properties:
additional_info:
type: object
minProperties: 2
properties:
scheme:
type: object
required:
- id
properties:
id:
type: integer
source:
type: string
required:
- scheme
- source
- type: object
properties:
additional_info:
type: object
additionalProperties: false
minProperties: 0
maxProperties: 0
examples:
example-1:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
example-2:
name: John Doe
additional_info: {}
example-3:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
optional_key: something