如果条件与使用JSON模式草案7的相对引用



当我引入条件if/then语句时,我想使用json模式将相对的json指针引用与$ref模式相结合。

在这种情况下,我想要求:

  • 如果system=Phone,则需要usePhone元素
  • 如果system=Email,则需要useEmail元素

当我使用架构进行验证时,它正在生成一个错误-我怀疑if->ref/enum代码是导致此问题的原因。json模式文档建议在定义的元素中嵌套所需的常量/枚举值,但当我的元素是$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"
]
}
}
]
}
}
}

"id"关键字更改为"$id"——该关键字的名称在JSON Schema草案4之后更改。

正如@Relequestual所说,在草案7或更早版本中,你不能有$ref的同级关键字,所以你应该将$ref封装在allOf中(即"allOf": [ { "$ref": ... } ].

如果使用的是draft-2019-09,则应将definitions重命名为$defs

此外,您不能在$ref中使用相对的JSON指针,因此像"1/system"这样的ref不会解析为任何内容(考虑到您在这里发布的内容(。因此,将该ref更改为#/definitions/ContactPoint/properties/system,它应该会在模式中找到正确的位置。

相关内容

  • 没有找到相关文章

最新更新