6
votes

I am new to JSON and JSON schema validation.

I have the following schema to validate a single employee object:

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

and I have the following schema to validate an array of the same employee object:

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

Can you please show me how to merge them so that way I can use one single schema to validate both single employee object or an entire collection. Thanks.

1

1 Answers

2
votes

(Note: this question was also asked on the JSON Schema Google Group, and this answer is adapted from there.)

With "$ref", you can have something like this for your array:

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

If you want something to be an array or a single item, then you can use "oneOf":

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

The original Google Groups answer also contains some advice on using "definitions" to organise schemas so all these variants can exist in the same file.