Typebox 类型定义
Typebox 提供 JSON Schema 风格的类型定义系统。
主要功能
1. 类型定义
typescript
import { Type } from '@sinclair/typebox';
const UserSchema = Type.Object({
id: Type.String(),
name: Type.String({ minLength: 1 }),
email: Type.String({ format: 'email' }),
age: Type.Optional(Type.Integer({ minimum: 0 })),
roles: Type.Array(Type.String())
});2. 验证
typescript
import { Value } from '@sinclair/typebox';
const result = Value.Check(UserSchema, data);
if (!result) {
console.log(Value.Errors(UserSchema, data));
}3. 编译为 JSON Schema
typescript
const schema = Type.Strict(UserSchema);
console.log(JSON.stringify(schema, null, 2));OpenClaw 中的使用
工具参数定义
typescript
const SendMessageTool = {
name: 'send_message',
parameters: Type.Object({
channel: Type.String({ enum: ['whatsapp', 'telegram'] }),
message: Type.String({ maxLength: 4096 }),
recipient: Type.String()
})
};配置验证
typescript
const ConfigSchema = Type.Object({
api_key: Type.String(),
timeout: Type.Integer({ minimum: 1, maximum: 300 }),
debug: Type.Boolean()
});相关概念
- 工具 (Tools) - 工具开发
- 技能 (Skills) - 技能开发