使用JSON模式草案7使用相对引用的If条件
在引入条件if/then语句时,我希望使用json模式将相对json指针引用与$ref模式结合起来 在这种情况下,我想要求:使用JSON模式草案7使用相对引用的If条件,json,schema,jsonschema,python-jsonschema,Json,Schema,Jsonschema,Python Jsonschema,在引入条件if/then语句时,我希望使用json模式将相对json指针引用与$ref模式结合起来 在这种情况下,我想要求: 如果系统=电话,则需要使用电话元素 如果系统=电子邮件,则需要使用电子邮件元素 当我使用架构进行验证时,它正在生成错误-我怀疑if->$ref/enum代码是问题的原因。json模式文档建议在定义的元素中嵌套所需的常量/枚举值,但当我的元素是$ref位置时,我不确定如何进行嵌套,例如: 需要相对模式是因为ContactPoint实例在组合模式中的多个位置使用 参考资
- 如果系统=电话,则需要使用电话元素
- 如果系统=电子邮件,则需要使用电子邮件元素
{
"$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"
]
}
}
]
}
}
}
将您的“id”
关键字更改为“$id”
——该关键字的名称在JSON模式草案4之后更改
正如@Relequestual所说,在草稿7或更早版本中,$ref
不能有兄弟关键字,因此您应该将$ref
包装在allOf
中(即“allOf”:[{“$ref”:…}]
如果您使用的是draft-2019-09,则应将定义重命名为$defs
此外,您不能在$ref
中使用相对JSON指针,因此类似“1/system”
的ref将不会解析为任何内容(考虑到您在此处发布的内容)。因此,请将该ref更改为#/definitions/ContactPoint/properties/system
,它应该在架构中找到正确的位置。更改“id”
关键字到“$id”
——该关键字的名称在JSON模式草案4之后更改
正如@Relequestual所说,在草稿7或更早版本中,$ref
不能有兄弟关键字,因此您应该将$ref
包装在allOf
中(即“allOf”:[{“$ref”:…}]
如果您使用的是draft-2019-09,则应将定义重命名为$defs
此外,您不能在$ref
中使用相对JSON指针,因此类似“1/system”
的ref将不会解析为任何内容(考虑到您在此处发布的内容)。因此,将该引用更改为#/definitions/ContactPoint/properties/system
,它应该在架构中找到正确的位置。两个初始事项,以查看您的问题是否得到解决。您需要对$id
使用完整URI。您不能将$ref
与draft-07或更早版本中的其他关键字一起使用。(当然可以,但所有其他关键字都被忽略/替换为新对象。我们在2019-09草案中“修复”了此问题。)尝试一下,看看您的问题是否得到修复。@Relequestual relative id在$id
中应该可以。但是,该关键字拼写错误,因此架构解析器根本看不到它们:)没错。我需要记住2020-11草案中的更改!哈。对不起。首先要看两件事是否解决了您的问题。您需要为$id
使用完整的URI。您不能将$ref
与草案-07或之前版本中的其他关键字一起使用。(当然,您可以,但所有其他关键字都被忽略/替换为新对象。我们“修复”了2019-09草案中的问题。)试试看你的问题是否得到了解决。@relequestualrelativeid在$id
中应该可以。但是,关键字拼写错误,所以架构解析器根本看不到:)是的。我需要记住2020-11草案中的更改!哈。对不起。
{
"$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"
]
}
}
]
}
}
}