How to Specify Choice of Properties in JSON Schema

2,835 مرّة مشاهدة
التخطي إلى أول رسالة غير مقروءة

Ramakrishna chirra

غير مقروءة،
17‏/12‏/2015، 12:21:59 ص17‏/12‏/2015
إلى 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

غير مقروءة،
18‏/12‏/2015، 4:17:07 م18‏/12‏/2015
إلى 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

غير مقروءة،
07‏/06‏/2016، 7:00:16 ص7‏/6‏/2016
إلى 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
الرد على الكل
رد على الكاتب
إعادة توجيه
0 رسالة جديدة