我们正在引入一种新的 type: objects,用于主题设置支持的数据类型,它可以用来替代现有的 json_schema 类型,我们计划很快弃用后者。
定义 objects 类型的主题设置
要创建一个 objects 类型的主题设置,首先像定义任何主题设置一样定义一个顶级键,该键将用作设置的名称。
links: ...
接下来,向该设置添加 type、default 和 schema 关键字。
links:
type: objects
default: []
schema: ...
type: objects 表示这将是一个 objects 类型的设置,而 default: [] 注释将设置的默认值设为空数组。请注意,默认值也可以设置为对象数组,我们将在定义 schema 后对此进行演示。
要定义模式(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:属性值存储为字符串。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 3categories:属性值是一个包含有效分类 ID 的数组。groups:属性值是一个包含有效群组 ID 的数组。tags:属性值是一个包含有效标签名称的数组。icon:属性值是 Discourse 图标集中单个图标的名称。选定的图标会自动添加到雪碧图(sprite sheet)中,因此无需单独注册即可渲染。
定义好模式后,现在可以通过在 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
自定义验证
对于某些属性类型,内置了对自定义验证的支持,可以通过在属性上添加 validations 关键字来声明。
links:
type: objects
default: []
schema:
name: link
properties:
name:
type: string
required: true
validations:
min: 1
max: 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: This is a description for the sections theme setting
schema:
properties:
name:
label: Name
description: The description for the property
links:
name:
label: Name
description: The description for the property
url:
label: URL
description: The description for the property
本文档受版本控制 - 请在 github 上建议更改。



