简体   繁体   English

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

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

I would like to use json schema to combine relative JSON pointer references, with a $ref schema, when I am introducing a conditional if / then statement.当我引入条件 if / then 语句时,我想使用 json 模式将相对 JSON 指针引用与 $ref 模式结合起来。

In this case I would like to require that:在这种情况下,我想要求:

  • If system = Phone then require usePhone element如果 system = Phone 那么需要 usePhone 元素
  • If system = Email then require useEmail element如果 system = Email 则需要 useEmail 元素

The schema is generating an error when I use it to validate - I suspect the if -> $ref / enum code is the cause of the issue.当我使用它进行验证时,模式生成错误 - 我怀疑if -> $ref / enum代码是问题的原因。 The json-schema documentation suggests nesting required constant / enum values inside the defined element but I am unsure how to do this when my element is a $ref location, eg: json-schema 文档建议在定义的元素中嵌套所需的常量/枚举值,但是当我的元素是 $ref 位置时,我不确定如何执行此操作,例如:

https://json-schema.org/understanding-json-schema/reference/conditionals.html https://json-schema.org/understanding-json-schema/reference/conditionals.html

"if": {
    "properties": { "country": { "const": "United States of America" } }
  }

The need for a relative schema is because the instance of ContactPoint is used in multiple locations in the combined schema.需要相对模式是因为 ContactPoint 的实例在组合模式中的多个位置使用。

References:参考:

Example:例子:

Thanks!谢谢!

{
    "$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"
                        ]
                    }
                }
            ]
        }
    }
}

Change your "id" keywords to "$id" -- the name of that keyword changed after JSON Schema draft 4.将您的"id"关键字更改为"$id" ——该关键字的名称在 JSON Schema 草案 4 之后更改。

As @Relequestual said, you can't have sibling keywords to $ref in drafts 7 or earlier, so you should wrap the $ref in an allOf (ie "allOf": [ { "$ref": ... } ] .正如@Relequestual 所说,在草稿 7 或更早版本中,您不能有$ref同级关键字,因此您应该将$ref包装在allOf (即"allOf": [ { "$ref": ... } ]

If you are using draft-2019-09, you should rename definitions to $defs .如果您使用的是draft-2019-09,您应该将definitions重命名为$defs

Also, you can't use relative JSON pointers in $ref , so a ref like "1/system" will not resolve to anything (given what you have posted here).此外,您不能在$ref使用相对 JSON 指针,因此像"1/system"这样的 ref 不会解析为任何内容(鉴于您在此处发布的内容)。 So change that ref to #/definitions/ContactPoint/properties/system and it should find the right position in the schema.因此,将该引用更改为#/definitions/ContactPoint/properties/system ,它应该会在架构中找到正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM