Слияние двух схем Json

Я новичок в проверке схемы JSON и JSON.

У меня есть следующая схема для проверки одного объекта сотрудника:

{
    "$schema":"http://json-schema.org/draft-03/schema#",
    "title":"Employee Type Schema",
    "type":"object",
    "properties": 
    {
        "EmployeeID": {"type": "integer","minimum": 101,"maximum": 901,"required":true},
        "FirstName": {"type": "string","required":true},
        "LastName": {"type": "string","required":true},
        "JobTitle": {"type": "string"},
        "PhoneNumber": {"type": "string","required":true},
        "Email": {"type": "string","required":true},
        "Address": 
        {
            "type": "object",
            "properties": 
            {
                "AddressLine": {"type": "string","required":true},
                "City": {"type": "string","required":true},
                "PostalCode": {"type": "string","required":true},
                "StateProvinceName": {"type": "string","required":true}
            }
        },
        "CountryRegionName": {"type": "string"}
    }
}

и у меня есть следующая схема для проверки массива того же объекта сотрудника:

{
    "$schema": "http://json-schema.org/draft-03/schema#",
    "title": "Employee set",
    "type": "array",
    "items": 
    {
        "type": "object",
        "properties": 
        {
            "EmployeeID": {"type": "integer","minimum": 101,"maximum": 301,"required":true},
            "FirstName": {"type": "string","required":true},
            "LastName": {"type": "string","required":true},
            "JobTitle": {"type": "string"},
            "PhoneNumber": {"type": "string","required":true},
            "Email": {"type": "string","required":true},
            "Address": 
            {
                "type": "object",
                "properties": 
                {
                    "AddressLine": {"type": "string","required":true},
                    "City": {"type": "string","required":true},
                    "PostalCode": {"type": "string","required":true},
                    "StateProvinceName": {"type": "string","required":true}
                }
            },
            "CountryRegionName": {"type": "string"}
        }
    }
}

Не могли бы вы показать мне, как их объединить, чтобы я мог использовать одну единственную схему для проверки как отдельного объекта сотрудника, так и всей коллекции. Спасибо.


person user3015526    schedule 22.11.2013    source источник


Ответы (1)


(Примечание: этот вопрос также задавали в группе Google схемы JSON, и этот ответ адаптирован оттуда.)

С "$ref" вы можете получить что-то вроде этого для вашего массива:

{
    "type": "array",
    "items": {"$ref": "/schemas/path/to/employee"}
}

Если вы хотите, чтобы что-то было массивом или отдельным элементом, вы можете использовать "oneOf":

{
    "oneOf": [
        {"$ref": "/schemas/path/to/employee"}, // the root schema, defining the object
        {
            "type": "array", // the array schema.
            "items": {"$ref": "/schemas/path/to/employee"}
        }
    ]
}

Исходный ответ групп Google также содержит некоторые советы по использованию "definitions" для организации схем, чтобы все эти варианты могли существовать в одном файле.

person cloudfeet    schedule 02.12.2013
comment
этот ответ не о слиянии схем, не связан с заданным вопросом - person Pablo Pazos; 28.05.2020
comment
Заголовок довольно общий, но в теле вопроса запрашивается одна-единственная схема для проверки как отдельного объекта-сотрудника, так и всей коллекции, и это то, что дает ответ. - person cloudfeet; 06.06.2020