Objects and Dependencies

www.altova.com Expand/Collapse All Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > JSON, JSON Schema > JSON Schema View >

Objects and Dependencies

Within the definition of an object, you might want to specify that a certain property is to be present only if another property is present. The first property is said to be dependent on the second property. Here is a scenario containing a dependency. An object (named, say, member) has a property called credit_card, which is defined as optional. The object's billing_address property can be made dependent on the credit_card property: Only if the credit_card property is present will the billing_address property be present.

 

This kind of dependency can be specified in one of two ways:

 

as a property dependency (the dependent structure is a property)
as a schema dependency (the dependent structure is a schema)

 

Property dependencies

The screenshot below shows an object having a name property (required), a credit_card property (optional ), and a billing_address property (dependent). The billing_address property is dependent on the credit_card property. The code of this JSON object definition is listed below the screenshot. How to create a property dependency is described further below.

JSONPropDependencies01

Click to expand/collapseCode listing of a JSON object with a property dependency

{

   "type": "object",

   "properties": {

      "name": {

      "type": "string"

      },

      "credit_card": {

         "type": "number"

      },

      "billing_address": {

         "type": "string"

      }

   },

   "required": [ "name" ],

   "dependencies": {

      "credit_card": [ "billing_address" ]

   },

   "additionalProperties": false

}

 

 

To create a property dependency, do the following:

 

1.Right-click the property on which the dependency will be based. (In our example this is the credit_card property.)
2.In the context menu that appears, select Add Dependency | Dependent Property. A new property is added with an Occurrence value of Dependent.
3.Define the name and value of this property, and add any additional details or constraints you want.

 

To specify a property as being dependent on another property, do the following:

 

1.Right-click the property you want to make dependent on another property. (In our example this is the billing_address property.)
2.In the context menu that appears, select Dependent. Alternatively, in the Details entry helper, go to the Occurrence entry, and select Dependent (see screenshot above).
3.In the Details entry helper, click the dropdown list icon of the Dependent On entry. The dropdown list displays all the other properties of the object. Select the property on which you want the current property to depend.

 

Note:An icon appears in the boxes of both properties involved in a dependency (see screenshot above). Double-clicking the icon of one property takes you to the other property.

 

Note:A property can have multiple dependent properties.

 

Schema dependencies

The screenshot below shows an object that describes the same instance data structure as the object discussed in the previous section. The definitions of the two objects, however, are different. While the previous definition used a property dependency to define the billing_address property as being dependent on the credit_card property, the current definition uses a schema dependency to define this dependency. The code of this latter JSON object definition is listed below the screenshot. How to create a schema dependency is described further below

Click to expand/collapse

Click to expand/collapseCode listing of a JSON object with a schema dependency

{

   "type": "object",

   "properties": {

      "name": {

         "type": "string"

      },

      "credit_card": {

         "type": "integer"

      }

   },

   "required": [ "name" ],

   "dependencies": {

      "credit_card": {

         "properties": {

            "billing_address": {

               "type": "string"

            }

         },

         "required": [ "billing_address" ]

      }

   }

}

 

 

To create a schema dependency, do the following:

 

1.Right-click the property on which the dependency will be based. (In our example this is the credit_card property.)
2.In the context menu that appears, select Add Dependency | Schema Dependency. A new object definition is created. It will have the same name as the property on which it is dependent (in our example, credit_card), and it will have a child sub-schema.
3.Define the sub-schema the way you want it, adding any additional details or constraints you may want.

 

Note:An icon appears in the boxes of the property and object involved in a dependency (see screenshot above). Double-clicking the icon in one box takes you to the other box.

 

Note:If you wish to set multiple dependencies, do this within the dependent sub-schema (see screenshot above).

 


© 2019 Altova GmbH