Schema to validate single object or collection

1,062 views
Skip to first unread message

Mathew

unread,
Nov 24, 2013, 2:12:41 PM11/24/13
to json-...@googlegroups.com, elhoucin...@lmco.com

 

I am new to JSON and JSON schema validation. I am using Draft-3 schema specification.
 
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"}
        }
    }
}
Is it possible to create one single schema (I mean file) that would validate both a single employee instance and an rray of the employee object?
 
 
Sample data to test schemas:
 
{
 "EmployeeID": 203,
 "FirstName": "Squidward",
 "LastName": "Tentacles",
 "JobTitle": "Cashier", 
                "Email": "Squidward.Tentacles&KrustyKrab.com",
 "PhoneNumber": "(212)306-5000",
                 "Address": {
  "AddressLine": "15 Spongebob Parkway",
  "City": "Bikini Bottom",
  "PostalCode": "72834",
  "StateProvinceName": "BB"
 },
 "CountryRegionName": "Down Under"
}
 
 
 
 
 

Geraint

unread,
Nov 25, 2013, 4:12:51 AM11/25/13
to json-...@googlegroups.com, elhoucin...@lmco.com
Are you familiar with using "$ref" to reference other schemas?

With "$ref", you can have something like this for your array:
{
    "type": "array",
    "items": {"$ref": "/schemas/path/to/employee"}
}

What I do very often is to embed an array definition like this inside the main schema, using "definitions":
{
    "$schema":"http://json-schema.org/draft-03/schema#",
    "title":"Employee Type Schema",
    "type":"object",
    "properties": {...},
    "definitions": {
        "array": {
            "type": "array",
            "items": {"$ref": "#"} // refers back to the root of the schema
        }
    }
}
Here, you can specify an array of items using "/schemas/path/to/employee#/definitions/array".  (There is nothing special about the "definitions" keyword, but it's reserved for this use, and it's a good convention.)

If you want something to be an array or a single item, then I would put a second entry in "definitions", using "oneOf":
{
    "$schema":"http://json-schema.org/draft-03/schema#",
    "title":"Employee Type Schema",
    "type":"object",
    "properties": {...},
    "definitions": {
        "array": {...},
        "singleOrArray": {
            "oneOf": [
                {"$ref": "#"}, // the root schema, defining the object
                {"$ref": "#/definitions/array"} // the array schema.
            ]
        }
    }
}

Hope that helps!

Geraint
Reply all
Reply to author
Forward
0 new messages