我们正在向 主题设置支持的类型 中引入新的 type: objects,它将取代我们打算很快弃用的现有 json_schema 类型。
定义对象类型主题设置
要创建对象类型主题设置,首先像定义任何主题设置一样定义一个顶层键,该键将用作设置的名称。
links: ...
接下来,向设置添加 type、default 和 schema 关键字。
links:
type: objects
default: []
schema: ...
type: objects 表示这将是一个对象类型设置,而 default: [] 注释将设置的默认值设置为一个空数组。请注意,默认值也可以设置为对象数组,我们将在定义 schema 后进行演示。
要定义 schema,首先像这样定义 schema 的 name:
links:
type: objects
default: []
schema:
name: link
接下来,我们将添加 properties 关键字到 schema 中,这将允许我们定义和验证每个对象的外观。
links:
type: objects
default: []
schema:
name: link
properties:
name: ...
在上面的示例中,我们声明 link 对象有一个 name 属性。要定义预期的数据类型,每个属性都需要定义 type 关键字。
links:
type: objects
default: []
schema:
name: link
properties:
name:
type: string
上面的 schema 定义声明 link 对象有一个类型为 string 的 name 属性,这意味着该属性只接受字符串值。目前支持以下类型:
string: 属性值存储为字符串。integer: 属性值存储为整数。float: 属性值存储为浮点数。boolean: 属性值为true或false。uploads: 属性值为附件 URLenum: 属性值必须是choices关键字中定义的值之一。links: type: objects default: [] schema: name: link properties: name: type: enum choices: - name 1 - name 2 - name 3categories: 属性值为有效类别 ID 的数组。groups: 属性值为有效组 ID 的数组。tags: 属性值为有效标签名称的数组。
定义了 schema 后,现在可以通过如下方式定义一个 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: 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,给定以下对象类型主题设置,格式如下。
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 上建议更改。



