簡體   English   中英

如果條件與使用 JSON 模式草案 7 的相對引用

[英]If condition with a relative ref using JSON schema draft 7

當我引入條件 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"關鍵字更改為"$id" ——該關鍵字的名稱在 JSON Schema 草案 4 之后更改。

正如@Relequestual 所說,在草稿 7 或更早版本中,您不能有$ref同級關鍵字,因此您應該將$ref包裝在allOf (即"allOf": [ { "$ref": ... } ]

如果您使用的是draft-2019-09,您應該將definitions重命名為$defs

此外,您不能在$ref使用相對 JSON 指針,因此像"1/system"這樣的 ref 不會解析為任何內容(鑒於您在此處發布的內容)。 因此,將該引用更改為#/definitions/ContactPoint/properties/system ,它應該會在架構中找到正確的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM