проверить несколько JSON с помощью одной JSONSchema - PullRequest
0 голосов
/ 30 августа 2018

Я хотел бы знать, возможно ли использовать одну 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 голосов
/ 31 августа 2018

Да, вы можете. Идея состоит в том, чтобы использовать oneOf ключевое слово и $ref для повторного использования определений. Эта схема JSON проверяет одно или другое (я не указал все свойства CustomerInfo, но вы поняли идею, поэтому, пожалуйста, заполните пробелы)

- РЕДАКТИРОВАТЬ 2018-09-01 -

Схема JSON проекта-04:

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

Версия схемы JSON Draft-06:

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