【问题标题】:validate several JSONs with a single JSONSchema使用单个 JSONSchema 验证多个 JSON
【发布时间】:2018-08-29 22:14:22
【问题描述】:

我想知道是否可以使用单个 JSONSchema (draft-04) 来验证多个 JSON,例如:

JSON 1:

{   
  "Credentiales": {   
    "Name": "123456",   
    "Password": "word"
  },
  "Reference": "1"
}

JSON 2:

{
    "ConsumerInfo": {
        "Reference": "1",
        "Consumer": "89",
        "FirstName": "Ern",
        "LastName": "Torres",
        "Address": "White Street 50",
        "City": "Ges",
        "State": "Santa",
        "PhoneNumber": "+12354569874",
        "ConfirmedEmailingDate": "2017-02-15 03:10:55"
    }
}

感谢您的帮助,给您带来的不便深表歉意

【问题讨论】:

  • 欢迎您!您可以使用相同的 JSON Schema 文件来验证任意数量的 JSON 实例。我不确定你在这里问什么。您能否更详细地解释您正在尝试做的事情,也许请提供一些代码?
  • 非常感谢您的回答。是不是我需要验证几个不同操作的 JSON(REST 服务),我想用一个文件 Json Schema 来做,这是我的想法。
  • 好的。到目前为止,您有什么架构?您是否为每个示例创建了一个模式并且想要组合它们?如果是,请提供这两个架构。
  • 您只想使用一个 JSON Schema 的原因是什么?通常,您为每个要验证的 JSON 响应类型创建一个。查看您的示例,您有非常不同的数据模型,这很正常。

标签: json jsonschema


【解决方案1】:

是的,你可以。这个想法是使用oneOf 关键字和$ref 来重用定义。此 JSON 模式验证其中一个(我没有指定所有 CustomerInfo 属性,但您明白了,所以请填写空白)

--编辑 2018-09-01--

Draft-04 JSON 架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "JSON schema of Credentiales and CustomerInfo",
    "definitions": {
        "Credentiales": {
            "type": "object",
                        "properties": {
                "Name": {"type": "string"},
                "Password": {"type": "string"}
            }
        },
        "Reference": {
            "type": "string"
        },
        "CustomerInfo": {
            "type": "object",
            "properties": {
                "Reference": {"$ref": "#/definitions/Reference"},
                "Consumer": {"type": "string"},
                "FirstName": {"type": "string"}
                !!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
            },
            "additionalProperties": {"not": {}}
        }
    },

    "oneOf": [
          {
           "type": "object",
           "properties": {
               "Credentiales": {
                    "$ref": "#/definitions/Credentiales"
                },
               "Reference": {
                    "$ref": "#/definitions/Reference"
                },
               "additionalProperties": {"not": {}}
           }
          },
          {"$ref":"#/definitions/CustomerInfo"}
        ]
}

--编辑 2018-08-31--

Draft-06 JSON 架构版本:

{  
    "$schema": "http://json-schema.org/draft-06/schema",
    "$id": "http://example.com/root.json",
    "title": "JSON schema of Credentiales and CustomerInfo",
    "definitions": {
        "Credentiales": {
            "type": "object",
                        "properties": {
                "Name": {"type": "string"},
                                "Password": {"type": "string"}
            }
        },
        "Reference": {
            "type": "string"
        },
        "CustomerInfo": {
            "type": "object",
            "properties": {
                "Reference": {"$ref": "#/definitions/Reference"},
                "Consumer": {"type": "string"},
                "FirstName": {"type": "string"}
              !!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
            },
            "additionalProperties": false
        }
    },

    "oneOf": [
          {
           "type": "object",
           "properties": {
               "Credentiales": {
                    "$ref": "#/definitions/Credentiales"
                },
               "Reference": {
                    "$ref": "#/definitions/Reference"
                },
               "additionalProperties": false
           }
          },
          {"$ref":"#/definitions/CustomerInfo"}
        ]
}

【讨论】:

  • 非常感谢,它真的对我有用,这就是我想要的。虽然我还有一些疑问:1.那个文件是draft-06的定义,用draft-04的定义有问题吗? 2. 可以在文档中使用更多限制,例如,在 JSON 文件中,只能使用模式中提到的元素(以及在定义了强制和可选元素的 XSD 中,不能再使用更多)添加在要分析的xml文件中)。非常感谢。
  • 我添加了一个兼容draft-04的版本。 如果这个答案对你来说没问题,你可以投票并接受答案。 对于草案 04 的兼容性,主要区别是将 additionalProperties: false 替换为 additionalProperties: {"not": {}}。检查 draft06 release notes 以了解从 draft04 的更改。至于限制,additionalProperties: false 表示除了架构中提到的属性之外,您不能拥有其他属性。
  • 你也可以使用required关键字来强制一些属性。
猜你喜欢
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2022-01-04
相关资源
最近更新 更多