Skip to content

Commit 08cfd3f

Browse files
authored
feat: support collection field validation (#415)
1 parent 120a8f0 commit 08cfd3f

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

docs/config/handbook.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ export default [
272272
// subTitle: '@nocobase/plugin-data-source-external-mariadb',
273273
link: '/handbook/data-modeling/collection-fields',
274274
},
275+
{
276+
title: 'Field Validation',
277+
'title.fr-FR': 'Validation',
278+
'title.zh-CN': '字段验证',
279+
'title.ja-JP': '検証',
280+
link: '/handbook/data-modeling/collection-fields/validation',
281+
},
275282
{
276283
title: 'Basic',
277284
'title.fr-FR': 'Basic',
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Field Validation
2+
To ensure data accuracy,security and consistency in data tables, NocoBase provides field validation functionality. This feature consists of two main parts: rule configuration and validation rule apply.
3+
4+
## Rule Configuration
5+
![20250819181342](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819181342.png)
6+
7+
NocoBase system fields integrate [Joi](https://joi.dev/api/) rules, with support as follows:
8+
9+
### String
10+
Joi string types correspond to the following NocoBase field types: single line text, long text, phone, email, URL, password, UUID.
11+
#### Common Rules
12+
- Min length
13+
- Max length
14+
- Length
15+
- Pattern
16+
- Required
17+
18+
#### Email
19+
![20250819192011](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819192011.png)
20+
[View more options](https://joi.dev/api/?v=17.13.3#stringemailoptions)
21+
22+
#### URL
23+
![20250819192409](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819192409.png)
24+
[View more options](https://joi.dev/api/?v=17.13.3#stringurioptions)
25+
26+
#### UUID
27+
![20250819192731](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819192731.png)
28+
[View more options](https://joi.dev/api/?v=17.13.3#stringguid---aliases-uuid)
29+
30+
### Number
31+
Joi number types correspond to the following NocoBase field types: integer, number, percentage.
32+
#### Common Rules
33+
- Greater than
34+
- Less than
35+
- Max value
36+
- Min value
37+
- Multiple
38+
39+
#### Integer
40+
In addition to common rules, integer fields additionally support [integer validation](https://joi.dev/api/?v=17.13.3#numberinteger) and [unsafe integer validation](https://joi.dev/api/?v=17.13.3#numberunsafeenabled).
41+
![20250819193758](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819193758.png)
42+
43+
#### Number & Percent
44+
In addition to common rules, number and percentage fields additionally support [precision validation](https://joi.dev/api/?v=17.13.3#numberinteger).
45+
![20250819193954](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819193954.png)
46+
47+
### Date
48+
Joi date types correspond to the following NocoBase field types: date (with timezone), date (without timezone), date only, Unix timestamp.
49+
50+
Supported validation rules:
51+
- Greater than
52+
- Less than
53+
- Max value
54+
- Min value
55+
- Timestamp
56+
- Required
57+
58+
### Association Fields
59+
Association fields only support required validation. Note that required validation for association fields is currently not supported in sub-form or sub-table scenarios.
60+
![20250819184344](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819184344.png)
61+
62+
## Validation Rule Apply
63+
After configuring field rules, the corresponding validation rules will be triggered when adding or modifying data.
64+
![20250819201027](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819201027.png)
65+
66+
Validation rules also apply to sub-table and sub-form components:
67+
![20250819202514](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819202514.png)
68+
69+
![20250819202357](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819202357.png)
70+
71+
Note that in sub-form or sub-table scenarios, required validation for relationship fields does not take effect.
72+
![20250819203016](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819203016.png)
73+
74+
## Differences from Client-Side Field Validation
75+
[Client-side field validation](/handbook/ui/fields/field-settings/validation-rules) and server-side field validation are applied in different scenarios, with significant differences in implementation and rule trigger timing, so they need to be managed separately.
76+
77+
### Configuration Method Differences
78+
- **Client-side validation**: Configure rules in edit forms (as shown in the figure below)
79+
- **Server-side field validation**: Set field rules in Data Source → Collections Configuration
80+
81+
![20250819203836](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819203836.png)
82+
83+
![20250819203845](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819203845.png)
84+
85+
86+
### Validation Trigger Timing Differences
87+
- **Client-side validation**: Triggers validation in real-time as users fill in fields, displaying error messages immediately
88+
- **Server-side field validation**: Validates on the server side before data entry after data submission, with error messages returned through API responses
89+
- **Application scope**: Server-side field validation takes effect not only during form submission but also triggers in all scenarios involving data addition or modification, such as workflows and data imports
90+
- **Error messages**: Client-side validation supports custom error messages, while server-side validation does not currently support custom error messages
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 字段验证
2+
为了确保数据表的准确性、安全性和一致性,NocoBase 提供了字段校验功能。该功能主要分为两个部分:配置规则和校验规则。
3+
4+
## 配置规则
5+
![20250819181342](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819181342.png)
6+
7+
NocoBase 系统字段是集成了 [Joi](https://joi.dev/api/) 的规则,支持情况如下:
8+
9+
### 字符串类型
10+
Joi 字符串类型对应的 NocoBase 字段类型包括:单行文本、多行文本、手机号、邮箱、URL、密码、UUID。
11+
#### 通用规则
12+
- 最小长度
13+
- 最大长度
14+
- 长度
15+
- 正则表达式
16+
- 必填
17+
18+
#### 邮箱
19+
![20250819192011](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819192011.png)
20+
[查看更多选项](https://joi.dev/api/?v=17.13.3#stringemailoptions)
21+
22+
#### URL
23+
![20250819192409](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819192409.png)
24+
[查看更多选项](https://joi.dev/api/?v=17.13.3#stringurioptions)
25+
26+
#### UUID
27+
![20250819192731](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819192731.png)
28+
[查看更多选项](https://joi.dev/api/?v=17.13.3#stringguid---aliases-uuid)
29+
30+
### 数字类型
31+
Joi 数字类型对应的 NocoBase 字段类型包括:整数、数字、百分比。
32+
#### 通用规则
33+
- 大于
34+
- 小于
35+
- 最大值
36+
- 最小值
37+
- 整数倍
38+
39+
#### 整数
40+
除通用规则外,整数字段额外支持[整数验证](https://joi.dev/api/?v=17.13.3#numberinteger)[不安全整数验证](https://joi.dev/api/?v=17.13.3#numberunsafeenabled)
41+
![20250819193758](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819193758.png)
42+
43+
#### 数字与百分比
44+
除通用规则外,数字和百分比字段额外支持[精度验证](https://joi.dev/api/?v=17.13.3#numberinteger)
45+
![20250819193954](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819193954.png)
46+
47+
### 日期类型
48+
Joi 日期类型对应的 NocoBase 字段类型包括:日期(含时区)、日期(不含时区)、仅日期、Unix 时间戳。
49+
50+
支持的验证规则:
51+
- 大于
52+
- 小于
53+
- 最大值
54+
- 最小值
55+
- 时间戳格式验证
56+
- 必填
57+
58+
### 关系字段
59+
关系字段仅支持必填验证。需要注意的是,关系字段的必填验证暂不支持在子表单或子表格场景下应用。
60+
![20250819184344](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819184344.png)
61+
62+
## 校验规则应用
63+
配置字段规则后,在添加或修改数据时将触发相应的校验规则。
64+
![20250819201027](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819201027.png)
65+
66+
校验规则同样适用于子表格和子表单组件:
67+
![20250819202514](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819202514.png)
68+
69+
![20250819202357](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819202357.png)
70+
71+
需要注意的是,在子表单或子表格场景中,关系字段的必填验证暂不生效。
72+
![20250819203016](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819203016.png)
73+
74+
## 与客户端字段验证的区别
75+
[客户端字段验证](/handbook/ui/fields/field-settings/validation-rules)与服务端字段验证适用于不同的应用场景,两者在实现方式和规则触发时机上存在显著差异,因此需要分别进行管理。
76+
77+
### 配置方式差异
78+
- **客户端验证**:在编辑表单中配置规则(如下图所示)
79+
- **服务端字段验证**:在数据源 → 数据表配置中设置字段规则
80+
![20250819203836](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819203836.png)
81+
82+
![20250819203845](https://nocobase-docs.oss-cn-beijing.aliyuncs.com/20250819203845.png)
83+
84+
85+
### 校验触发时机差异
86+
- **客户端验证**:在用户填写字段时实时触发验证,并立即显示错误信息
87+
- **服务端字段验证**:在数据提交后,由服务端在数据入库前进行校验,错误信息通过 API 响应返回
88+
- **应用范围**:服务端字段验证除了在表单提交时生效外,还会在工作流、数据导入等所有涉及数据新增或修改的场景中触发
89+
- **错误信息**:客户端验证支持自定义错误信息,服务端验证暂不支持自定义错误信息

0 commit comments

Comments
 (0)