使用单个JSONSchema验证多个JSON

时间:2018-08-29 22:14:22

标签: json jsonschema

我想知道是否可以使用一个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"
    }
}

感谢您的帮助,不便之处,敬请谅解

1 个答案:

答案 0 :(得分:0)

是的,可以。这个想法是使用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"}
        ]
}