【问题标题】:If condition with a relative ref using JSON schema draft 7如果条件与使用 JSON 模式草案 7 的相对参考
【发布时间】:2021-03-06 19:42:26
【问题描述】:

当我引入条件 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 的实例在组合架构中的多个位置使用。

参考资料:

例子:

谢谢!

{
    "$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 使用完整的 URI。您不能将$ref 与draft-07 或更早版本中的其他关键字一起使用。 (嗯,你可以,但所有其他关键字都被忽略/替换为新对象。我们在 2019-09 草案中“修复”了这个问题。)试试看你的问题是否得到解决。
  • @Relequestual 相对 ID 在$id 中应该没问题。但是,关键字拼写错误,因此模式解析器根本看不到它们:)
  • 没错。我需要记住 2020-11 年草案的变化!哈。对不起。

标签: json schema jsonschema python-jsonschema


【解决方案1】:

将您的 "id" 关键字更改为 "$id" -- 在 JSON 架构草案 4 之后,该关键字的名称发生了更改。

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

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

此外,您不能在 $ref 中使用相对 JSON 指针,因此像 "1/system" 这样的引用将无法解析任何内容(鉴于您在此处发布的内容)。因此,将该 ref 更改为 #/definitions/ContactPoint/properties/system,它应该会在架构中找到正确的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    相关资源
    最近更新 更多