Files
cindy/scripts/shared/json-schema-lite.mjs
Dash 7e31b4f8b6 fix(i18n): 收窄标点豁免作用域、补 OAuth 页盲区,URL token 允许标点后空格
review 反馈 5 条,其中 2 条是我上一轮新增机制自身的问题:

1. punctuationExempt 一刀关掉了整个标点段(我上轮引入)
   那条豁免的理由只涉及「issue 模板里冒号必须半角」,与省略号无关;却让该 key 能在
   四种语言里悄悄引入 `...`,把刚加的省略号门禁从它身上摘掉。
   豁免作用域应当只覆盖它实际论证过的那条规则——已收窄为只豁免半角标点检查。
   负向验证:给该 key 加 `...` 仍会被拦下。

2. URL / 邮箱 token 的截断条件漏了「标点后有空格」(同上轮那批的遗漏)
   `访问 https://x.test, 返回` 里逗号不紧邻 CJK,token 把它吃掉,两条标点规则都失效。
   TOKEN_TAIL 与 normalizeForPunctuation 的两处 lookahead 一并允许空白,
   与刚改过的 findHalfWidthPunct 口径统一。六种情形都有断言。

3. 又一处扫描盲区:OAuth 回调结果页(`src/main/oauthResultPage.ts`)
   渲染在系统浏览器里,是用户完成第三方授权后看到的第一屏,失败时还要靠它说清
   下一步。既有测试只校验完整性与渲染结果,不校验术语与标点。
   新增 oauthResultPageGlossary.test.ts。它的文案分散在多个函数里且要传实参,
   用固定占位值求值后再扫;已确认摊平出每语言 15 条、四语齐全,并做了负向验证
   (注入半角逗号 + ASCII 省略号,两条规则都报)。
   过程中发现我一开始给 buildOAuthReturnAction 传错了参数(真实签名是
   (lang, source, brandName)),值里出现 undefined——靠 probe 打印摊平结果发现的。

4. personalization 说明里三处 session 全是 runtime session
   设置在 Agent 会话创建时被快照;终端直跑 / 其他 IDE 里的 Claude Code 根本不是
   Cindy 的产品对话,受 SessionStart hook 管。称它们「对话」等于凭空造出 Cindy
   之外的产品对话。已恢复「Agent 会话 / 会话」并加 exempt。

5. matchesType 的死分支(number 已在上一行 return)已删,并说明 integer/number
   为何要单独判。

验证:desktop 13328 测试全过、desktop typecheck 干净、术语表单测 54 passed、
check:i18n / check:i18n-glossary / check:dev-docs 通过。

Signed-off-by: Dash <dashhuang@gmail.com>
2026-07-26 02:04:48 +12:00

242 lines
9.0 KiB
JavaScript

/**
* JSON Schema draft-07 的**受限子集**校验器,零依赖。
*
* 为什么不用 ajv:本仓 scripts/ 一贯只用 node 内置模块(唯一例外是 ws)。ajv 目前能在
* 根 node_modules 里解析到,但它只是别的包提升上来的传递依赖——把 CI 阻断门禁架在
* 「碰巧被提升」的包上,lockfile 一变就会静默失效。
*
* 安全前提:**遇到不认识的关键字立即抛错**(compile 阶段),而不是忽略。
* 子集校验器最危险的失败模式是「schema 写了约束、校验器看不懂、于是默默放行」——
* 那正是本模块要解决的问题本身。宁可让加新关键字的人被迫来这里补实现。
*/
/**
* 本模块实现的关键字。不在此列的一律报错,详见文件头。
*
* 其中 $schema / $id / title / description 是纯注解,不产生约束;definitions 只被
* $ref 解引用时用到。它们列在这里是为了让 assertSupported 放行,校验逻辑本身
* 不需要看它们。
*/
const SUPPORTED = new Set([
'$schema',
'$id',
'$ref',
'title',
'description',
'type',
'required',
'properties',
'additionalProperties',
'items',
'enum',
'pattern',
'minItems',
'minLength',
'oneOf',
'definitions',
]);
/**
* 每个关键字的值必须是什么形状。
*
* 只查「关键字名认不认得」是不够的:形状写错时,校验器会以自己那套(而非 JSON Schema 的)
* 方式去解释它,约束就变成了别的意思——这恰恰违背本模块「不静默忽略」的安全前提。
* 典型的是 `type: ["string", "null"]`(draft-07 合法),本实现只支持字符串形态,
* 拿数组去比对会永远不等,于是那条 type 约束在「看似生效」的外表下彻底失效。
*/
const KEYWORD_SHAPE = {
type: 'string',
pattern: 'string',
$ref: 'string',
$schema: 'string',
$id: 'string',
title: 'string',
description: 'string',
minItems: 'number',
minLength: 'number',
required: 'array',
enum: 'array',
oneOf: 'array',
properties: 'object',
definitions: 'object',
};
/**
* 值本身是一个 schema 的关键字。
*
* 这些位置允许 boolean schema(draft-07 里 `true` 恒通过、`false` 恒失败),
* 所以不能按 'object' 死判——那会把合法的 `items: false` 误判成 schema 写错。
* 其余关键字(type / required / …)仍按 KEYWORD_SHAPE 严格校验。
*/
const SCHEMA_VALUED = new Set(['items', 'additionalProperties']);
function shapeOf(value) {
if (Array.isArray(value)) return 'array';
if (value === null) return 'null';
return typeof value;
}
/** 递归检查 schema 自身只用了已实现的关键字,且每个关键字的值形状也在支持范围内。 */
function assertSupported(node, path = '#') {
if (node === true || node === false) return;
if (typeof node !== 'object' || node === null) {
throw new Error(`schema ${path} 不是对象`);
}
for (const [key, value] of Object.entries(node)) {
if (!SUPPORTED.has(key)) {
throw new Error(
`schema ${path} 使用了未实现的关键字 "${key}"——请在 scripts/shared/json-schema-lite.mjs 里补上实现,` +
'不要让它被静默忽略',
);
}
if (SCHEMA_VALUED.has(key)) {
if (typeof value !== 'boolean' && shapeOf(value) !== 'object') {
throw new Error(
`schema ${path}/${key} 只支持 boolean 或 schema 对象,实际是 ${shapeOf(value)}`,
);
}
continue;
}
const expected = KEYWORD_SHAPE[key];
if (expected && shapeOf(value) !== expected) {
throw new Error(
`schema ${path} 的 "${key}" 应是 ${expected},实际是 ${shapeOf(value)}——` +
'本模块只实现了 JSON Schema 的一个子集,形状不符时约束会被错误解释而非报错',
);
}
}
for (const [key, child] of Object.entries(node.properties ?? {})) {
assertSupported(child, `${path}/properties/${key}`);
}
for (const [key, child] of Object.entries(node.definitions ?? {})) {
assertSupported(child, `${path}/definitions/${key}`);
}
if (typeof node.additionalProperties === 'object') {
assertSupported(node.additionalProperties, `${path}/additionalProperties`);
}
if (node.items !== undefined) assertSupported(node.items, `${path}/items`);
for (const [i, child] of (node.oneOf ?? []).entries()) {
assertSupported(child, `${path}/oneOf/${i}`);
}
}
function typeOf(value) {
if (Array.isArray(value)) return 'array';
if (value === null) return 'null';
if (Number.isInteger(value)) return 'integer';
return typeof value;
}
function matchesType(value, expected) {
// integer / number 单独判:typeOf 把整数报成 'integer',直接比对会让 `type: "number"`
// 拒掉整数值。
if (expected === 'integer') return Number.isInteger(value);
if (expected === 'number') return typeof value === 'number';
return typeOf(value) === expected;
}
/**
* 校验 value 是否符合 schema,返回错误信息数组(空数组=通过)。
* root 用于解析 `$ref`,只支持 `#/definitions/X` 这一种形态——够用,且不引入
* 远程引用这类需要网络的能力。
*/
function validateNode(value, schema, root, path, errors) {
// boolean schema 是合法 draft-07:true 恒通过,false 恒失败。
// assertSupported 已经放行它们,这里若不处理,`schema.$ref`/`schema.type` 全是 undefined,
// false 就被当成「无约束对象」——`items: false` 变成不检查,`oneOf` 里的 false 分支
// 甚至能算作唯一通过的分支,把本意「禁止一切」的约束翻转成「允许一切」。
if (schema === true) return;
if (schema === false) {
errors.push(`${path}: schema 为 false,此处不允许任何值`);
return;
}
if (schema.$ref) {
const m = /^#\/definitions\/([^/]+)$/.exec(schema.$ref);
if (!m) throw new Error(`只支持 #/definitions/X 形式的 $ref,收到 ${schema.$ref}`);
const target = root.definitions?.[m[1]];
if (!target) throw new Error(`$ref 指向不存在的定义:${schema.$ref}`);
validateNode(value, target, root, path, errors);
return;
}
if (schema.type && !matchesType(value, schema.type)) {
errors.push(`${path}: 期望 ${schema.type},实际 ${typeOf(value)}`);
return;
}
if (schema.enum && !schema.enum.includes(value)) {
errors.push(`${path}: 取值必须是 ${schema.enum.join(' / ')} 之一,实际 ${JSON.stringify(value)}`);
}
if (typeof value === 'string') {
if (schema.pattern) {
// schema 里的 pattern 写坏时,new RegExp 会抛出去、整个校验流程中断,调用方拿不到
// 带 path 的可读错误(check-i18n-glossary 会直接崩栈而不是走 fail())。
// 转成普通校验错误,并明确指向是 schema 自身的问题而非数据的问题。
let re;
try {
re = new RegExp(schema.pattern);
} catch (err) {
errors.push(`${path}: schema 的 pattern 不是合法正则(${schema.pattern}):${err.message}`);
re = null;
}
if (re && !re.test(value)) {
errors.push(`${path}: "${value}" 不匹配 ${schema.pattern}`);
}
}
if (schema.minLength !== undefined && value.length < schema.minLength) {
errors.push(`${path}: 长度需 ≥ ${schema.minLength}`);
}
}
if (Array.isArray(value)) {
if (schema.minItems !== undefined && value.length < schema.minItems) {
errors.push(`${path}: 至少需要 ${schema.minItems} 项`);
}
// 必须判 !== undefined:`items: false` 是合法 draft-07(禁止任何元素),
// 但它是 falsy,写 `if (schema.items)` 会把这条约束整个跳过。
if (schema.items !== undefined) {
value.forEach((item, i) => validateNode(item, schema.items, root, `${path}[${i}]`, errors));
}
}
if (schema.oneOf) {
const passed = schema.oneOf.filter((branch) => {
const sub = [];
validateNode(value, branch, root, path, sub);
return sub.length === 0;
});
if (passed.length !== 1) {
errors.push(`${path}: 需恰好匹配 oneOf 的一个分支,实际匹配 ${passed.length} 个`);
}
}
if (typeOf(value) === 'object') {
for (const key of schema.required ?? []) {
if (!(key in value)) errors.push(`${path}: 缺少必填字段 "${key}"`);
}
const known = new Set(Object.keys(schema.properties ?? {}));
for (const [key, child] of Object.entries(value)) {
const childPath = `${path}.${key}`;
if (known.has(key)) {
validateNode(child, schema.properties[key], root, childPath, errors);
} else if (schema.additionalProperties === false) {
errors.push(
`${childPath}: 未知字段 "${key}"——拼错的字段名(如 forbiden)会让整条规则静默失效`,
);
} else if (typeof schema.additionalProperties === 'object') {
validateNode(child, schema.additionalProperties, root, childPath, errors);
}
}
}
}
/** 校验 data 是否符合 schema。返回人读错误信息数组,空数组=通过。 */
export function validateAgainstSchema(data, schema) {
assertSupported(schema);
const errors = [];
validateNode(data, schema, schema, '$', errors);
return errors;
}