json 架构 - 根据另一个字段值需要字段
json schema - field is required based on another field value
我正在创建 JsonSchema(v4)。
我正在尝试根据另一个 属性 的值从 parent.
中获取一个 属性
Parent
- 用户
- 亚型
- 地址
Child
- 地址
- 第 1 行
- 第 2 行
- companyName(如果用户子类型是公司则需要)
这是怎么做到的?
我现在有这样的东西...
{
"User": {
"title": "User",
"type": "object",
"id": "#User",
"properties": {
"subtype": {
"type": "string"
},
"address": {
"$ref": "Address"
}
}
}
"Address": {
"title": "Address",
"type": "object",
"id": "#Address",
"properties": {
"line1": {
"type": "string"
},
"line2": {
"type": "string"
},
"companyName": {
"type": "string"
}
},
"required": ["line1", "line2"]
}
}
子类型是任意字符串,因此无法获得不同子类型的完整列表。
将此添加到您的用户模式。本质上它的意思是:要么 "subtype" 不是 "company" 要么 "address" 需要 "companyName".
"anyOf": [
{
"not": {
"properties": {
"subtype": { "enum": ["company"] }
}
}
},
{
"properties": {
"address": {
"required": ["companyName"]
}
}
}
]
我正在创建 JsonSchema(v4)。 我正在尝试根据另一个 属性 的值从 parent.
中获取一个 属性Parent
- 用户
- 亚型
- 地址
Child
- 地址
- 第 1 行
- 第 2 行
- companyName(如果用户子类型是公司则需要)
这是怎么做到的? 我现在有这样的东西...
{
"User": {
"title": "User",
"type": "object",
"id": "#User",
"properties": {
"subtype": {
"type": "string"
},
"address": {
"$ref": "Address"
}
}
}
"Address": {
"title": "Address",
"type": "object",
"id": "#Address",
"properties": {
"line1": {
"type": "string"
},
"line2": {
"type": "string"
},
"companyName": {
"type": "string"
}
},
"required": ["line1", "line2"]
}
}
子类型是任意字符串,因此无法获得不同子类型的完整列表。
将此添加到您的用户模式。本质上它的意思是:要么 "subtype" 不是 "company" 要么 "address" 需要 "companyName".
"anyOf": [
{
"not": {
"properties": {
"subtype": { "enum": ["company"] }
}
}
},
{
"properties": {
"address": {
"required": ["companyName"]
}
}
}
]