当我引入条件 if / then 语句时,我想使用 json 模式将相对 JSON 指针引用与 $ref 模式结合起来。
在这种情况下,我想要求:
- 如果 system = Phone 则需要 usePhone 元素
- 如果 system = Email 则需要 useEmail 元素
当我使用模式进行验证时,模式正在生成错误 - 我怀疑if -> $ref / enum代码是问题的原因。json-schema 文档建议在定义的元素内嵌套所需的常量/枚举值,但是当我的元素是 $ref 位置时,我不确定如何执行此操作,例如:
https://json-schema.org/understanding-json-schema/reference/conditionals.html
"if": {
"properties": { "country": { "const": "United States of America" } }
}
需要相对模式是因为 ContactPoint 的实例在组合模式中的多个位置使用。
参考:
- https://json-schema.org/understanding-json-schema/reference/conditionals.html
- https://docs.opis.io/json-schema/1.x/pointers.html
- https://docs.opis.io/json-schema/1.x/conditional-subschemas.html
- https://docs.opis.io/json-schema/1.x/ref-keyword.html
- https://docs.opis.io/json-schema/1.x/multiple-subschemas.html
例子:
谢谢!
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "characteristic.entity.json",
"title": "characteristic.entity.schema.1.0",
"description": "Characteristic Objects Json Schema",
"definitions": {
"ContactPoint": {
"title": "ContactPoint",
"additionalProperties": true,
"properties": {
"id": {
"description": "",
"$ref": "primitive.entity.json#/definitions/string"
},
"type": {
"description": "The type of Contact.",
"enum": [
"Alternative",
"Primary"
]
},
"system": {
"description": "Telecommunications form for contact point - what communications system is required to make use of the contact.",
"enum": [
"Phone",
"Email",
"other"
]
},
"value": {
"description": "",
"$ref": "primitive.entity.json#/definitions/string"
},
"usePhone": {
"description": "Identifies the purpose of a Phone contact point.",
"enum": [
"Alternate",
"Business - Direct",
"Business - Main",
"Home",
"Mobile",
"Work"
]
},
"useEmail": {
"description": "Identifies the purpose of an Email contact point.",
"enum": [
"Person",
"Work",
"Business"
]
}
},
"allOf": [
{
"if": {
"$ref": "1/system",
"enum": [
"Phone"
]
},
"then": {
"required": [
"usePhone"
]
}
},
{
"if": {
"$ref": "1/system",
"enum": [
"Email"
]
},
"then": {
"required": [
"useEmail"
]
}
}
]
}
}
}