【问题标题】:How to get jsonschema to use a boolean of false OR attributes如何让 jsonschema 使用 false OR 属性的布尔值
【发布时间】:2018-01-21 14:14:41
【问题描述】:

我在 json 架构中创建了一个 Avatar,在我的应用程序中,如果我们想隐藏头像,我们将其作为 false 传递,否则,头像有图像和名称。

在json模式中,我指定头像为

const avatarSchema = {
    "type": ["object", "boolean"],
    "required": [],
    "additionalProperties": false,    
    "properties":{
       "name": {
           "type": "string"
        },
       "image": {
           "type": "string",
           "format": "url"
        }
    }
};

这不起作用,因为如果 Avatar = true, 'image' does not exist on type 'Avatar'. Property 'image' does not exist on type 'true'.

我不希望Avatar 都是true,要么是false 要么是{image, name},我如何告诉json schema 以这种方式操作?

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    您对架构不起作用的原因的解释不正确。 requiredadditionalPropertiesproperties 关键字仅适用于正在验证的数据是对象的情况。因此,架构应该可以按照您的意愿工作,只是它的值可以是“true”。

    如果您使用的验证器给出了与您的问题类似的错误消息,则它没有正确验证。您应该为该验证器提交错误报告。

    无论如何,您的问题的解决方案需要anyOf 关键字。

    {
      "anyOf": [
        { "enum": [false] },
        {
          "type": "object",
          "required": ["name", "image"],
          "additionalProperties": false,
          "properties": {
            "name": { "type": "string" },
            "image": {
              "type": "string",
              "format": "url"
            }
          }
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 2020-07-04
      • 2016-12-04
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多