Objects type for theme settings

The objects type lets a theme setting hold a list of objects with a structure you define, such as links with a name and URL, or sections that each contain links. Admins edit the list in an object editor, and Discourse validates each object against your schema. It replaces the older json_schema type.

Define an objects setting

Add type: objects, a default and a schema to the setting:

links:
  type: objects
  default: []
  schema:
    name: link
    identifier: name
    properties:
      name:
        type: string
      url:
        type: string
  • default is the setting’s initial value: an empty list, or a list of objects that match the schema.
  • schema.name names a single object. The editor labels objects with it, for example “link 1”.
  • schema.identifier is optional. It names a property whose value labels each object in the editor instead, such as the link’s name.
  • schema.properties lists the properties each object can have. Every property needs a type.

Property types

  • string: Text. Set textarea: true to use an expanding text area:

    description:
      type: string
      textarea: true
      validations:
        max_length: 500
    
  • integer: A whole number.

  • float: A number that can have decimals.

  • boolean: true or false.

  • enum: One of the values listed in choices:

    size:
      type: enum
      choices:
        - small
        - medium
        - large
    
  • datetime: A date and time in ISO 8601 format with a timezone, such as 2026-01-01T10:00:00Z.

  • upload: An uploaded file. Theme code receives its URL.

  • icon: The name of an icon from the Discourse icon set. Selected icons are added to the sprite sheet automatically, so you don’t need to register them.

  • categories: A list of category IDs.

  • groups: A list of group IDs.

  • tags: A list of tag names.

  • objects: A nested list of objects. See “Nested objects” below.

For tags, the settings API also accepts tag IDs and stores them as names. In theme code, each object with a tags property also gets a tag_objects array with the id and name of each selected tag the visitor can see, so you can target tags by ID, for example in CSS.

Default value

Once the schema is defined, default can hold objects that match it:

links:
  type: objects
  default:
    - name: Guidelines
      url: /guidelines
    - name: FAQ
      url: /faq
  schema:
    name: link
    identifier: name
    properties:
      name:
        type: string
      url:
        type: string

Required properties

Properties are optional by default. Add required: true to make one required:

properties:
  name:
    type: string
    required: true
  url:
    type: string

Blank string, datetime and icon values and empty categories, groups and tags lists count as missing: required properties reject them, and optional properties skip their validations. false counts as set.

Validations

Some types accept a validations key:

properties:
  url:
    type: string
    required: true
    validations:
      max_length: 2048
      url: true

For string properties:

  • min_length: The minimum number of characters.
  • max_length: The maximum number of characters.
  • url: When true, the value must be a URL. Absolute http, https and ftp URLs, mailto: links, paths starting with / and # anchors are accepted.

For integer and float properties:

  • min: The minimum value.
  • max: The maximum value.

For tags, groups and categories properties:

  • min: The minimum number of selected items.
  • max: The maximum number of selected items.

Resolving group membership

Theme code often only needs to know whether the current user is in one of the selected groups. currentUser.groups can’t answer that reliably, because it only includes groups the user can see. Add resolve_group_membership: true to a groups property to resolve it on the server:

menu_sections:
  type: objects
  default:
    - name: Staff tools
      groups:
        - 3
  schema:
    name: menu section
    properties:
      name:
        type: string
      groups:
        type: groups
        resolve_group_membership: true

The admin UI and the stored value keep the groups list. In the frontend settings object, Discourse replaces the group IDs in each object with a boolean named after the property, prefixed with user_in_:

for (const section of settings.menu_sections) {
  if (section.user_in_groups) {
    // The user is in at least one of the selected groups.
  }
}

Within object schemas, the option only works on type: groups properties. It also works in nested objects and with automatic groups such as logged_in_users and anonymous_users.

Nested objects

A property with type: objects and its own schema holds a nested list:

sections:
  type: objects
  default:
    - name: Getting started
      links:
        - name: Guidelines
          url: /guidelines
        - name: FAQ
          url: /faq
  schema:
    name: section
    identifier: name
    properties:
      name:
        type: string
        required: true
      links:
        type: objects
        schema:
          name: link
          identifier: name
          properties:
            name:
              type: string
            url:
              type: string

Setting description and property labels

Describe the setting and label its properties in the theme’s locales/en.yml. For the sections setting above:

en:
  theme_metadata:
    settings:
      sections:
        description: Sections shown in the sidebar
        schema:
          properties:
            name:
              label: Name
              description: The section's heading
            links:
              name:
                label: Name
                description: The link text
              url:
                label: URL
                description: Where the link goes

Labels for a nested property go under the name of its collection, here links.

Translatable text

To let admins translate text they enter in the object editor, declare a required string translation_key property and add translatable: true to each text property:

links:
  type: objects
  default:
    - translation_key: guidelines
      label: Community guidelines
  schema:
    name: link
    properties:
      translation_key:
        type: string
        required: true
      label:
        type: string
        translatable: true

Each translatable field becomes a theme translation, such as links.guidelines.label, that admins manage in the admin UI. For key rules, nested objects, reading the text in code and updating existing components, see translatable settings.


This document is version controlled - suggest changes on github.

16 Likes

I remain to be convinced deprecation of the json schema style is a good idea.

Whilst these can get pretty complex and aren’t the most “developer friendly” of formats (so this is a great change in that regard), there are online tools to validate json schemas which is a really useful way to validate both the schema and against any default data.

e.g. https://www.jsonschemavalidator.net/

How will that work in this new world?

2 Likes

When uploading a theme, we will be validating the default data against the defined schema. That being said, we are not validating that the schema definition is valid now but it wouldn’t be hard for us to do so. Even for the json schema setting right now, I don’t think we are validating the default data against the defined schema.

Our current implementation of json schema type settings is kind of broken in many ways with the most obvious being the editor in the admin interface. We discussed this internally and decided that it is much easier for us to maintain a limited schema format defined by us instead of allowing all the possibilities that comes along with json schema.

2 Likes

Some cool features here:

  • you can get rid of JSON.parse and access the setting directly to the get object which is really nice.

  • the url validator!

:chefs_kiss: :chefs_kiss:

5 Likes

Is there any way for multiple lines to be respected in the editor?

This default works:

- name: markdown
  value: > 
    ## Heading
      * first bullet
      * second bullet

But once you edit this, the carriage returns are lost

Moreover, it would be nice to have a “text” type that could store more long-form data and perhaps expose a larger “text-area” editor

5 Likes

Here are some feedbacks:

2 Likes

I noticed this and it has been fixed in

https://github.com/discourse/discourse/commit/25bcee43c60c7b707a07934984563e565d04d242

1 Like

Will we be able to re-order items on the interface?

E.g. that’s the object settings editor on the easy footer theme component. I can’t re-arrange any items right now:

4 Likes

I wanted to request this feature as well! :+1:


On a side note, it would be helpful if the first post contained information about the identifier property.

Before looking at Nolo’s image above, I thought replacing the default child label with a property value was impossible. After looking at the code, I found the identifier property.

4 Likes

Reordering is certainly something that has been brought up internally as well. I’ll try to land that this week.

Noted. I’ll update the first post about the identifier property.

5 Likes

Yep to replace the (soon to be legacy?) json system it needs to match or exceed the old interface:

including the ordering.

1 Like

Hi, are there any plans to support other field types soon?

For example;

  • a long_string with markdown format; perhaps with customizable toolbar,
  • a date field (with validation rules),
  • a color field (with validation rules)?
1 Like

There’s no current plan, though I agree that it would be useful. I’d like an icon field myself.

8 Likes

In my experience, it seems to be working like saved presets. In this example the first 2 entries could benefit from these presets, but anything after that all new entries will come up blank initially.

This also means we cannot set default values for each field. for example, if I want to have a checkbox, to start at checked state I cannot have it.

links:
  type: objects
  default:
    - name: link 1
      title: link 1 title
    - name: link 2
      title: link 2 title
  schema:
    name: link
    properties:
      is_active:
        type: boolean
        default: true 

default: true will not work in there as expected.

Could there be a way to set the default values per field, for each entries that are created?

Is there a way to import object properties to variables in Sass?

You can always parse the string, but that doesn’t sound like a great idea to promote this way. :sweat_smile:

1 Like

Thank you for sharing the example! Though yeah.. it doesn’t look that tempting :upside_down_face:

1 Like

Out of interest, without spending too much time looking, where are we with this?

2 Likes

Yes, it isn’t very good, don’t do it. :smile:. It was more of an attempt to see if it was possible, but not a reasonable approach.
I agree with you; it would be nice to have a direct way! :+1:

I would like to know, too!
Also, if I’m right, that would be the only missing feature parity with the json_schema.

2 Likes

I was looking for the upload type to be available, but it’s not.
A quick look at the core shows that the topic, post, and upload types have been implemented server-side but not in the front end. Is there a specific reason for that? :thinking: