我们引入了一种新的 type: objects,用于主题设置支持的类型,它可以替代我们计划很快弃用的现有 json_schema 类型。
定义 objects 类型主题设置
要创建 objects 类型的主题设置,首先像定义任何主题设置那样定义一个顶层键,该键将用作设置名称。
links: ...
接下来,向设置添加 type、default 和 schema 关键字。
links:
type: objects
default: []
schema: ...
type: objects 表示这是一个 objects 类型设置,而 default: [] 注释将设置的默认值设置为空数组。请注意,默认值也可以设置为对象数组,我们将在定义好 schema 后演示这一点。
要定义架构,首先像下面这样定义架构的 name:
links:
type: objects
default: []
schema:
name: link
接下来,我们将向架构添加 properties 关键字,这将允许我们定义和验证每个对象应有的样子。
links:
type: objects
default: []
schema:
name: link
properties:
name: ...
在上面的示例中,我们声明 link 对象有一个 name 属性。为了定义期望的数据类型,每个属性都需要定义 type 关键字。
links:
type: objects
default: []
schema:
name: link
properties:
name:
type: string
上面的架构定义声明 link 对象有一个类型为 string 的 name 属性,这意味着该属性只接受字符串值。目前支持以下类型:
-
string:属性值存储为字符串。设置textarea: true以使用扩展文本区域:description: type: string textarea: true validations: max_length: 500 -
integer:属性值存储为整数。 -
float:属性值存储为浮点数。 -
boolean:属性值为true或false。 -
upload:属性值是附件 URL。 -
enum:属性值必须是choices关键字中定义的值之一。links: type: objects default: [] schema: name: link properties: name: type: enum choices: - name 1 - name 2 - name 3 -
categories:属性值是有效类别 ID 的数组。 -
groups:属性值是有效组 ID 的数组。 -
tags:属性值是有效标签名称的数组。 -
icon:属性值是 Discourse 图标集中单个图标的名称。选定的图标会自动添加到精灵图中,因此无需单独注册即可渲染。
定义好架构后,可以通过在 yaml 中定义数组来设置默认的设定值,如下所示:
links:
type: objects
default:
- name: link 1
title: link 1 title
- name: link 2
title: link 2 title
schema:
name: link
properties:
name:
type: string
title:
type: string
必填属性
默认情况下,定义的所有属性都是可选的。要将属性标记为必填,只需使用 required: true 注释该属性。也可以使用 required: false 注释属性将其标记为可选。
links:
type: objects
default: []
schema:
name: link
properties:
name:
type: string
required: true
title:
type: string
required: false
空的 string、datetime 和 icon 值以及空的 categories、groups 和 tags 列表被视为缺失值:必填属性会拒绝它们,而可选属性会跳过其验证。false 视为已设置。
自定义验证
对于某些属性类型,内置支持通过 validations 关键字注释属性来声明自定义验证。
links:
type: objects
default: []
schema:
name: link
properties:
name:
type: string
required: true
validations:
min_length: 1
max_length: 2048
url: true
string 类型的验证
min_length:属性的最小长度。关键字的值必须是整数。max_length:属性的最大长度。关键字的值必须是整数。url:验证属性是否为有效 URL。关键字的值可以是true/false。
integer 和 float 类型的验证
min:属性的最小值。关键字的值必须是整数。max:属性的最大值。关键字的值必须是整数。
tags、groups 和 categories 类型的验证
min:属性的最少记录数。关键字的值必须是整数。max:属性的最多记录数。关键字的值必须是整数。
解析组成员资格
对象设置可以将 type: groups 属性解析为当前用户的布尔值。当主题代码只需要知道当前用户是否位于配置的组之一时,这很有用,因为 currentUser.groups 仅包含对用户可见的组。
向 groups 属性添加 resolve_group_membership: true:
menu_sections:
type: objects
default:
- name: section 1
groups:
- 1
- 3
schema:
name: menu section
properties:
name:
type: string
groups:
type: groups
resolve_group_membership: true
管理界面和存储的设置值仍然使用原始的 groups 数组。在前端运行时的 settings 对象中,Discourse 会从每个对象中移除组 ID,并添加一个具有相同属性名称并以 user_in_ 为前缀的布尔值:
for (const section of settings.menu_sections) {
if (section.user_in_groups) {
// 用户位于该部分至少一个选定的组中。
}
}
此选项仅适用于 type: groups 的对象架构属性。它也适用于嵌套对象架构以及自动组,如 logged_in_users 和 anonymous_users。
嵌套对象结构
对象也可以包含一个包含对象数组的属性。为了创建嵌套对象结构,属性也可以使用 type: objects 和相关的 schema 定义进行注释。
sections:
type: objects
default:
- name: section 1
links:
- name: link 1
url: /some/url
- name: link 2
url: /some/other/url
schema:
name: section
properties:
name:
type: string
required: true
links:
type: objects
schema:
name: link
properties:
name:
type: string
url:
type: string
设置描述和本地化
要为 en 区域设置添加设置描述,请创建一个 locales/en.yml 文件,对于以下 objects 类型主题设置,使用以下格式。
sections:
type: objects
default:
- name: section 1
links:
- name: link 1
url: /some/url
- name: link 2
url: /some/other/url
schema:
name: section
properties:
name:
type: string
required: true
links:
type: objects
schema:
name: link
properties:
name:
type: string
url:
type: string
en:
theme_metadata:
settings:
sections:
description: 这是 sections 主题设置的描述
schema:
properties:
name:
label: 名称
description: 属性的描述
links:
name:
label: 名称
description: 属性的描述
url:
label: URL
description: 属性的描述
此文档使用版本控制 - 请在 github 上建议更改。



