How to Specify Choice of Properties in JSON Schema

2,922 views
Skip to first unread message

Ramakrishna chirra

unread,
Dec 17, 2015, 12:21:59 AM12/17/15
to JSON Schema
Hi,
   I am new to JSON. I am trying too build a JSON schema which can validate a JSON data file. I want to have only 1 property specified out of a bunch of properties which are available in the object. How can i describe a schema for this?

eg:
I have my Schema like mentioned below
{
    "type": "object",
    "properties": {
        "street_address": {
            "type": "string"
        },
        "city": {
            "type": "string"
        },
        "state": {
            "type": "string"
        }
    },
    "required": [
        "street_address"
    ],
    "additionalProperties": false
}

In above schema i want to create a choice between city and state. That is either city or state can come in json. So that below json would be invalid

{
    "street_address": "abc",
    "city": "anv",
    "state": "opi"
}

and below one should be valid one

{
    "street_address": "abc"
}

or

{
    "street_address": "abc",
    "city": "anv"
}

or

{
    "street_address": "abc",
    "state": "opi"
}

Can some one please help me to modify above schema to accomplish the goal.


- Thanks

Ramakrishna

Joe McIntyre

unread,
Dec 18, 2015, 4:17:07 PM12/18/15
to JSON Schema
Ramakrishna,

The following schema fits your description.


  "type": "object",
    "properties": {
      "street_address": {
        "type": "string"
      },
      "city": {
        "type": "string"
      },
      "state": {
        "type": "string"
      }
    },
    "required": [
      "street_address"
    ],
    "oneOf": [
      {"required": ["city"]},
      {"required": ["state"]}
    ],
    "additionalProperties": false
}

However, in your description you ask for 1 of a selection, but in your examples you include an example that contains zero of the selection (no city or state). The schema shown fits the description requested, so the example with no selection specified will be invalid.

The addition to your original schema is to add the "oneOf" constraint to the set of "required" constraints. This will make the choice of one, and only one, element from this list be required in the data for validation to pass, in addition to the always required "street address". 

Joe

jamidar...@gmail.com

unread,
Jun 7, 2016, 7:00:16 AM6/7/16
to JSON Schema
Hi,

I am trying to create a JSON schema where I want 1 or 2 fields as mandatory. 
For example, I want user to pass either "contactId" or "firstName and lastName". 

"privateContact": {
            "type": [ "object", "null" ],
            "properties": {
                
                "firstName": {
                    "type": "string",
                    "pattern": "^[^<>!@#$%^&*]+$",
                    "minLength": 2,
                    "maxLength": 30,
                    "title": "firstName"
                },
                "lastName": {
                    "type": "string",
                    "pattern": "^[^<>!@#$%^&*]+$",
                    "minLength": 2,
                    "maxLength": 30,
                    "title": "lastName"
                },
                
                "contactId": {
                    "type": [ "string", "null" ],
                    "title": "contactId",
                    "maxLength": 100,

                }
            },
            "additionalProperties": false
Reply all
Reply to author
Forward
0 new messages