【问题标题】:why doesn't json schema validate definitions defined in required attribute为什么 json 模式不验证必需属性中定义的定义
【发布时间】:2018-11-29 23:03:12
【问题描述】:

我正在尝试创建一个 json 架构,该架构根据对象的类型对其进行验证。它选择正确的定义,但是,它不会验证所选定义中的必需属性。这是我正在尝试的 json 架构:

{
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "literal": {
            "type": "object",
            "properties": {
                "raw": { "type": "string" }
            },
            "required": ["raw"],
            "additionalProperties": false
        },
        "identifier": {
            "type": "object",
            "properties": {
                "name": { "type": "string" }
            },
            "required": ["name"],
            "additionalProperties": false
        }
    },

    "type": "object",
    "oneOf": [
        {
            "type": "object",
            "properties": {
                "type": {
                    "enum": ["Literal"]
                },
                "content": { "$ref": "#/definitions/literal" }
            }
        },
        {
            "type": "object",
            "properties": {
                "type": {
                    "enum": ["Identifier"]
                },
                "content": { "$ref": "#/definitions/identifier" }
            }
        }
    ],
    "required": ["type"]
};

以下架构是有效的,即使它缺少“原始”属性: { "type" : "Literal" }

谢谢

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    JSON Schema 规范中没有关键字 content

    一旦您在根架构中声明了"type":"object",就无需在子架构中再次执行此操作。

    为了将对象type枚举值与关联的扩展定义结合起来,您需要allOf关键字。

    同样在定义中,如果你使用"additionalProperties": false,你必须列出对象的所有属性(见"type": {})。对于之前定义/验证的属性,您可以只使用许可模式:{}true

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
    
      "definitions": {
        "literal": {
          "properties": {
            "type": {},
            "raw": { "type": "string" }
          },
          "required": ["raw"],
          "additionalProperties": false
        },
        "identifier": {
          "properties": {
            "type": {},
            "name": { "type": "string" }
          },
          "required": ["name"],
          "additionalProperties": false
        }
      },
    
      "type": "object",
      "oneOf": [
        {
          "allOf": [
            {
              "properties": {
                "type": {
                  "enum": ["Literal"]
                }
              }
            },
            {"$ref": "#/definitions/literal"}
          ]
        },
        {
          "allOf": [
            {
              "properties": {
                "type": {
                  "enum": ["Identifier"]
                }
              }
            },
            {"$ref": "#/definitions/identifier" }
          ]
        }
      ],
      "required": ["type"]
    }
    

    【讨论】:

    • 很想知道"content" 的来源。 OP你能解释一下吗?
    • 谢谢,这似乎已经解决了:) @Relequestual 我可能从无效示例中复制粘贴了“内容”属性。几个小时都在研究这个问题,所以这里复制的最终结果有多个修订
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2023-03-20
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多