@lark-project/cli
Version:
飞书项目插件开发工具
59 lines (58 loc) • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validatePointTypeAllowed = void 0;
/**
* 按当前插件的 app_type 校验本地点位配置是否合规——schema 只能保证字段形态,
* 但"AI 插件只能配自己专属点位、且全工程恰好一个"是 schema 之外的语义约束,
* 必须在任何写出本地配置的入口(local-config set / preflight)都跑一遍硬卡,
* 防止用户从普通插件复制粘贴 yaml 进 AI 工程导致后端拒绝时只能看到一行报错。
*
* 规则:
* - normal ⇒ 不允许出现 aiNode / aiField
* - ai_node ⇒ 只允许 aiNode;aiNode 必须是单对象(写成数组阻断);其它顶层 key 阻断
* - ai_field ⇒ 只允许 aiField;同上单对象约束
*
* 这里只识别"AI 形态独占"的语义,不替代 schema 校验。形态合法 + 字段不合规仍由
* schema 拦下。
*/
const AI_ONLY_KEYS = new Set(['aiNode', 'aiField']);
function validatePointTypeAllowed(config, appType) {
const errors = [];
if (!config || typeof config !== 'object') {
return { ok: true, errors };
}
const presentKeys = Object.keys(config).filter(k => config[k] !== undefined && config[k] !== null);
if (appType === 'normal') {
for (const key of presentKeys) {
if (AI_ONLY_KEYS.has(key)) {
errors.push(`Point type "${key}" is only available for AI plugins (app_type=ai_node | ai_field). ` +
`Current plugin is normal. Remove "${key}" or recreate the plugin with the matching --app-type.`);
}
}
return { ok: errors.length === 0, errors };
}
// AI 分支:先确定本插件的"专属 key",再要求恰好它存在 + 形态正确
const requiredKey = appType === 'ai_node' ? 'aiNode' : 'aiField';
const forbiddenAIKey = appType === 'ai_node' ? 'aiField' : 'aiNode';
for (const key of presentKeys) {
if (key === requiredKey)
continue;
if (key === forbiddenAIKey) {
errors.push(`Point type "${key}" cannot coexist with app_type=${appType}. ` +
`An AI plugin's point type is locked at creation; remove "${key}" or recreate the plugin.`);
continue;
}
if (AI_ONLY_KEYS.has(key))
continue;
// 任何非 AI 顶层 key 在 AI 工程里都禁
errors.push(`Point type "${key}" is not allowed in an AI plugin (app_type=${appType}). ` +
`Only "${requiredKey}" may be configured.`);
}
// 单对象约束:写成数组也阻断(schema 已经拦了,但保险再校一次,便于 stderr 用专属文案)
const required = config[requiredKey];
if (required !== undefined && Array.isArray(required)) {
errors.push(`"${requiredKey}" must be a single object, not an array. AI plugins have exactly one ${requiredKey} point.`);
}
return { ok: errors.length === 0, errors };
}
exports.validatePointTypeAllowed = validatePointTypeAllowed;