The default keyword

The default keyword allows you to set the default value of an item. You can use default with strings, numbers, booleans and lists, as well as content and image links.

On this page we'll provide an overview of the default keyword and include some examples of its use.

Using the default keyword

To use default, include it in a property definition. The property shown below is a number between 0 and 100. We want to set the default value to 90. The property will be set to this value when content created from this schema is first opened in the content form. The user can change the value if they wish.

   "properties":{
      "numberValue":{
         "title":"title",
         "description":"description",
         "type":"number",
         "minimum":0,
         "maximum":100,
         "default":90
      }
   }

The default value is also shown in the schema editor content form preview, as shown in the image below.

A default value defined for the numberValue property
A default value defined for the numberValue property

Validation

Note that your schema will be valid, and can still be saved in the Schema Editor, if you set a default value that does not meet the validation rules defined for the property. So, if we set the default value to 102 in the example property above, a warning message will be displayed in the content form. The content cannot be saved until the property is within the allowed range.

If the default value doesn't meet the validation rules, an error is displayed in the content form or in the schema editor preview
If the default value doesn't meet the validation rules, an error is displayed in the content form or in the schema editor preview

Default and const

If you use the const keyword to define a value for a property that cannot be changed, then the default keyword will not override what's defined in the constant.

For example, in the property shown below, constProperty will always be set to "value" and cannot be changed.

"properties":{
      "constProperty":{
         "type":"string",
         "const":"value",
         "default":"notvalue"
      }
   }

Examples

String

The default value for a string property is easy to set up, as shown in the example property below.

Note that the maximum length of the string is 20 characters. If we specify a default that is greater than 20 characters in length, then the schema can be saved and registered, but when the content form is opened, a warning message is displayed in the stringProperty field and the content cannot be saved until the field contains less than 20 characters.

"properties":{
      "stringProperty":{
         "title":"A string with a default value",
         "type":"string",
         "minLength":0,
         "maxLength":20,
         "default":"Some example text"
      }
   }

Rich text

The default value for rich text is defined in the same way as a string, as shown in the example property below.

"properties":{
      "product_description":{
         "title":"product description",
         "type":"string",
         "format":"markdown",
         "default":"Lorem ipsum dolor sit amet…"
      }
   }

Enum

In the example below, the "sofa" string from the enum will be displayed initially in the content form.

"properties":{
      "product":{
         "type":"string",
         "enum":[
            "chair",
            "sofa",
            "bed",
            "table",
            "lamp"
         ],
         "default":"sofa"
      }
   }

Array

To specify the default value for items in an array, the values are specified as an array, as shown in the property below. "string" will be the default for the first item, "string2" for the second item and so on.

"properties":{
      "stringArray":{
         "title":"title",
         "description":"description",
         "type":"array",
         "items":{
            "type":"string"
         },
         "default":[
            "string",
            "string1",
            "string2"
         ]
      }
   }

You can also include default values for arrays of images, content links and numbers.

Note that the default keyword cannot currently be used with arrays of type object or with nested arrays

Color picker

An example of how to set up a default value for RGB color is shown below.

"properties":{
      "color":{
         "title":"color of the product",
         "format":"color",
         "type":"string",
         "default":"rgb(255, 102, 102)"
      }
   }

Boolean

Setting the default for the a boolean property is simple. In the example property below, the value of the "in_stock" property defaults to false.

 "properties":{
      "in_stock":{
         "title":"In Stock",
         "type":"boolean",
         "default":false
      }
   }

You can set up an image as the default value for an image link property. In the example property shown below, you can see that the default value includes all the properties returned by an image-link object, including the id, name, endpoint and default host. The easiest way to find all these values is to publish a content item containing the image and view the item in JSON format from a link in the "Content delivery" section of the content form.

For the product-image property, the "blue-tie" image will be shown when the content form is opened. The user may choose another image instead.

properties":{
      "product_image":{
         "allOf":[
            {
               "$ref":"http://bigcontent.io/cms/schema/v1/core#/definitions/image-link"
            }
         ],
         "default":{
            "_meta":{
               "schema":"http://bigcontent.io/cms/schema/v1/core#/definitions/image-link"
            },
            "id":"286e4760-1812-4e3a-81ab-d2e3bf32b9b5",
            "name":"blue-tie",
            "endpoint":"ampproduct",
            "defaultHost":"cdn.media.amplience.net"
         }
      }
   }

You can also include a default value for a video-link item in the same way as an image-link.

Note, no validation is performed on the id or other properties of the image to ensure that the image exists on the specified account.

A default value can be included for a content-link. In the example property shown below, a default value is defined including the contentType and content id. You can find the content id for a content item from the "Content delivery" section of the content form or by choosing "Get content id" from the item's menu in the Content Library.

 "properties":{
      "contentlink":{
         "allOf":[
            {
               "$ref":"http://bigcontent.io/cms/schema/v1/core#/definitions/content-link"
            },
            {
               "properties":{
                  "contentType":{
                     "enum":[
                        "http://docexample.com/tutorialbanner.json"
                     ]
                  }
               }
            }
         ],
         "default":{
            "_meta":{
               "schema":"http://bigcontent.io/cms/schema/v1/core#/definitions/content-link"
            },
            "contentType":"http://docexample.com/tutorialbanner.json",
            "id":"12058184-97de-42e7-93b5-29b8705a871d"
         }
      }
   }

A default value for a content-reference can be defined in the same way.

 "properties":{
      "contentlink":{
         "allOf":[
            {
               "$ref":"http://bigcontent.io/cms/schema/v1/core#/definitions/content-reference"
            },
            {
               "properties":{
                  "contentType":{
                     "enum":[
                        "http://docexample.com/tutorialbanner.json"
                     ]
                  }
               }
            }
         ],
         "default":{
            "_meta":{
               "schema":"http://bigcontent.io/cms/schema/v1/core#/definitions/content-reference"
            },
            "contentType":"http://docexample.com/tutorialbanner.json",
            "id":"12058184-97de-42e7-93b5-29b8705a871d"
         }
      }
   }

Note no validation is performed on the content type content id to ensure that the content item exists.

Usage notes

  • Default is not currently supported for arrays on object or nested arrays.

  • Default is not currently supported for localized fields.

  • Currently a slot item containing a default content link cannot be added an edition, because in this context a snapshot id is expected rather than a content id.

Image links, content links and references

Creating localized content types

Generic keywords

results matching ""

    No results matching ""