Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two Json Schemas

Tags:

jsonschema

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.

like image 653
user3015526 Avatar asked Nov 22 '13 21:11

user3015526


People also ask

What is JSON Schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

How does JSON Schema match?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

What is oneOf in JSON?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid.

What is $ref in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.


1 Answers

(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.

like image 57
cloudfeet Avatar answered Jan 04 '23 12:01

cloudfeet