feat(openclaw): P1-6 config.schema RPC 写入校验 - 防小白改坏配置

OpenClaw 内核 config.set/patch 写入时已经会校验,但用户要等点保存才看到错误。
本 PR 把校验提前到前端,让用户改完字段立刻看到红字提示。

## 新增工具 src/lib/config-schema.js
- getFieldSchema(path) — 调内核 config.schema.lookup,带 5 分钟缓存
- validateField(path, value) — 拿 schema 后做基础约束校验
- validateFieldSync(schema, value, path) — 已有 schema 时同步校验
- clearSchemaCache(path?) — 清缓存(极少需要)

## 校验维度(不引入 ajv,保持 vanilla JS 体积)
- required — 必填空值检查
- type — string / number / integer / boolean / array / object / null
        (含「数字字符串当数字看」的容错)
- enum — 枚举白名单
- minimum / maximum — 数值范围
- pattern — 字符串正则

## 友好错误文案(i18n)
- common.error.schemaRequired / schemaType / schemaEnum / schemaMin / schemaMax / schemaPattern
- 6 个键 × 11 语言全覆盖
- 替换 schema 给的英文 path 后用户看到的是:
  「Gateway 端口不能小于 1024」「gateway.port 应该是「integer」类型」

## 集成示范:gateway.js
- saveConfig 开头先 validateField('gateway.port', port)
- 失败 → toast 红字 + focus 回 port 输入框 + early return
- 通过 → 走原流程
- 内核无该 schema 时降级放行(不阻断保存)

## 设计哲学
- 内核仍是最终守门人(config.set/patch 会再校验)
- 本模块的价值是「立即反馈」+「未来动态 UI 渲染」基础
- 容错优先:schema.lookup 失败时静默放行,不影响老内核兼容性

## 累计
- 1 新文件(config-schema.js)
- 2 修改(gateway.js / common.js)
- 6 个新 i18n 键 × 11 语言
- 后续可在 memory/dreaming/security/cron 等页面同样接入
This commit is contained in:
晴天
2026-05-14 04:30:23 +08:00
parent e717a7a098
commit c00b2dbf64
3 changed files with 200 additions and 0 deletions

View File

@@ -297,6 +297,15 @@ function bindConfigEvents(el) {
async function saveConfig(page, state) {
const port = parseInt(page.querySelector('#gw-port')?.value) || 18789
// P1-6: 用内核 config.schema.lookup 即时校验 port无 schema 时降级放行)
const portCheck = await validateField('gateway.port', port)
if (!portCheck.ok) {
toast(portCheck.message, 'error')
page.querySelector('#gw-port')?.focus()
return
}
const bindRadio = page.querySelector('input[name="gw-bind"]:checked')
const bind = bindRadio?.value || 'loopback'
const mode = 'local'