@zhin.js/schema 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +188 -0
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# @zhin.js/schema
|
|
2
|
+
|
|
3
|
+
Zhin.js 的配置验证和 Schema 系统,提供类型安全的配置定义、验证和序列化能力。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @zhin.js/schema
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 基本用法
|
|
12
|
+
|
|
13
|
+
### 创建 Schema
|
|
14
|
+
|
|
15
|
+
使用静态工厂方法创建不同类型的 Schema:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Schema } from '@zhin.js/schema'
|
|
19
|
+
|
|
20
|
+
// 基础类型
|
|
21
|
+
const strSchema = Schema.string()
|
|
22
|
+
const numSchema = Schema.number()
|
|
23
|
+
const boolSchema = Schema.boolean()
|
|
24
|
+
const regSchema = Schema.regexp()
|
|
25
|
+
const dateSchema = Schema.date()
|
|
26
|
+
const anySchema = Schema.any()
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 对象 Schema
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
const configSchema = Schema.object({
|
|
33
|
+
port: Schema.number().default(8080).description('服务端口'),
|
|
34
|
+
host: Schema.string().default('localhost').description('服务地址'),
|
|
35
|
+
debug: Schema.boolean().default(false).description('调试模式'),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// 验证并填充默认值
|
|
39
|
+
const config = configSchema({ port: 3000 })
|
|
40
|
+
// => { port: 3000, host: 'localhost', debug: false }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 链式配置
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
Schema.number()
|
|
47
|
+
.required() // 标记为必填
|
|
48
|
+
.default(8080) // 设置默认值
|
|
49
|
+
.description('端口') // 添加描述
|
|
50
|
+
.min(1) // 最小值
|
|
51
|
+
.max(65535) // 最大值
|
|
52
|
+
.step(1) // 步长
|
|
53
|
+
.hidden() // 在表单中隐藏
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 复合类型
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// 列表
|
|
60
|
+
const listSchema = Schema.list(Schema.string())
|
|
61
|
+
|
|
62
|
+
// 字典
|
|
63
|
+
const dictSchema = Schema.dict(Schema.number())
|
|
64
|
+
|
|
65
|
+
// 元组
|
|
66
|
+
const tupleSchema = Schema.tuple([
|
|
67
|
+
Schema.string(),
|
|
68
|
+
Schema.number(),
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
// 联合类型
|
|
72
|
+
const unionSchema = Schema.union([
|
|
73
|
+
Schema.const('a'),
|
|
74
|
+
Schema.const('b'),
|
|
75
|
+
Schema.const('c'),
|
|
76
|
+
])
|
|
77
|
+
|
|
78
|
+
// 交叉类型
|
|
79
|
+
const intersectSchema = Schema.intersect([
|
|
80
|
+
Schema.object({ name: Schema.string() }),
|
|
81
|
+
Schema.object({ age: Schema.number() }),
|
|
82
|
+
])
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 选项列表
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
Schema.union([
|
|
89
|
+
Schema.const('sqlite'),
|
|
90
|
+
Schema.const('mysql'),
|
|
91
|
+
Schema.const('postgres'),
|
|
92
|
+
]).description('数据库类型')
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 序列化
|
|
96
|
+
|
|
97
|
+
Schema 支持 JSON 序列化,便于在网络传输或持久化:
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
// 序列化为 JSON
|
|
101
|
+
const json = schema.toJSON()
|
|
102
|
+
|
|
103
|
+
// 从 JSON 恢复
|
|
104
|
+
const restored = Schema.fromJSON(json)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 自定义类型扩展
|
|
108
|
+
|
|
109
|
+
通过 `Schema.extend()` 注册自定义类型格式化器:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
Schema.extend('myType', function (this: Schema, key: string, value: any) {
|
|
113
|
+
value = Schema.checkDefault(this, key, value)
|
|
114
|
+
// 自定义验证和转换逻辑
|
|
115
|
+
return value
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
使用 `Schema.resolve()` 获取已注册的格式化器:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const formatter = Schema.resolve('number')
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## 与 Zhin.js 集成
|
|
126
|
+
|
|
127
|
+
在插件中使用 `defineSchema` 定义配置(自动注册到 Web 控制台表单渲染):
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { usePlugin, Schema } from 'zhin.js'
|
|
131
|
+
|
|
132
|
+
const { defineSchema } = usePlugin()
|
|
133
|
+
|
|
134
|
+
const getConfig = defineSchema(Schema.object({
|
|
135
|
+
port: Schema.number().default(8080).description('服务端口'),
|
|
136
|
+
enabled: Schema.boolean().default(true).description('是否启用'),
|
|
137
|
+
}))
|
|
138
|
+
|
|
139
|
+
const config = getConfig()
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## 工具函数
|
|
143
|
+
|
|
144
|
+
- `isEmpty(value)` - 检查值是否为空(`undefined`、`null`、空字符串、空数组、空对象)
|
|
145
|
+
- `deepMerge(target, source)` - 深度合并对象
|
|
146
|
+
|
|
147
|
+
## API 参考
|
|
148
|
+
|
|
149
|
+
### 静态方法
|
|
150
|
+
|
|
151
|
+
| 方法 | 说明 |
|
|
152
|
+
|------|------|
|
|
153
|
+
| `Schema.number()` | 创建数字类型 Schema |
|
|
154
|
+
| `Schema.string()` | 创建字符串类型 Schema |
|
|
155
|
+
| `Schema.boolean()` | 创建布尔类型 Schema |
|
|
156
|
+
| `Schema.regexp()` | 创建正则表达式类型 Schema |
|
|
157
|
+
| `Schema.date()` | 创建日期类型 Schema |
|
|
158
|
+
| `Schema.dict(inner)` | 创建字典类型 Schema |
|
|
159
|
+
| `Schema.object(props)` | 创建对象类型 Schema |
|
|
160
|
+
| `Schema.list(inner)` | 创建列表类型 Schema |
|
|
161
|
+
| `Schema.tuple(items)` | 创建元组类型 Schema |
|
|
162
|
+
| `Schema.union(types)` | 创建联合类型 Schema |
|
|
163
|
+
| `Schema.intersect(types)` | 创建交叉类型 Schema |
|
|
164
|
+
| `Schema.const(value)` | 创建常量类型 Schema |
|
|
165
|
+
| `Schema.any()` | 创建任意类型 Schema |
|
|
166
|
+
| `Schema.extend(type, formatter)` | 注册自定义类型格式化器 |
|
|
167
|
+
| `Schema.resolve(type)` | 获取已注册的格式化器 |
|
|
168
|
+
| `Schema.fromJSON(json)` | 从 JSON 恢复 Schema 实例 |
|
|
169
|
+
|
|
170
|
+
### 实例方法
|
|
171
|
+
|
|
172
|
+
| 方法 | 说明 |
|
|
173
|
+
|------|------|
|
|
174
|
+
| `.required()` | 标记为必填字段 |
|
|
175
|
+
| `.hidden()` | 在表单中隐藏 |
|
|
176
|
+
| `.description(text)` | 添加字段描述 |
|
|
177
|
+
| `.default(value)` | 设置默认值 |
|
|
178
|
+
| `.option(label, value)` | 添加可选项 |
|
|
179
|
+
| `.multiple()` | 允许多选 |
|
|
180
|
+
| `.min(value)` | 设置最小值 |
|
|
181
|
+
| `.max(value)` | 设置最大值 |
|
|
182
|
+
| `.step(value)` | 设置步长 |
|
|
183
|
+
| `.component(name)` | 指定渲染组件 |
|
|
184
|
+
| `.toJSON()` | 序列化为 JSON |
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/schema",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "Configuration validation and schema system for Zhin.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"typescript": "^5.3.0",
|
|
16
16
|
"@types/node": "^24.3.0",
|
|
17
|
-
"zhin.js": "1.0.
|
|
17
|
+
"zhin.js": "1.0.28"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"registry": "https://registry.npmjs.org"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"zhin.js": "1.0.
|
|
29
|
+
"zhin.js": "1.0.28"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
32
|
"zhin",
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
],
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsc",
|
|
41
|
-
"clean": "
|
|
41
|
+
"clean": "rimraf lib"
|
|
42
42
|
}
|
|
43
43
|
}
|