模型管理v2:schema 保存时统一将标量字段 value 置空,数据值不在模板中预设
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -602,8 +602,53 @@ const openMappingEditor = (key: string) => {
|
||||
jsonEditorVisible.value = true;
|
||||
};
|
||||
|
||||
/** 递归将 schema 中所有标量字段的 value 重置为 null。
|
||||
* 模型配置保存的是「结构模板」:保留 value 键与结构、defaultValue 等配置,但数据值不预设。
|
||||
* defaultValue 已有专门字段承载默认值,value 不再携带任何值,统一置 null。
|
||||
* 根节点经 nodeToValue 输出时不带 type/attrs 包装(普通对象),故需对无 type 节点也递归子键。
|
||||
*/
|
||||
function resetSchemaValues(schema: any): any {
|
||||
if (schema === null || schema === undefined) return schema;
|
||||
if (Array.isArray(schema)) {
|
||||
schema.forEach((item, i) => {
|
||||
schema[i] = resetSchemaValues(item);
|
||||
});
|
||||
return schema;
|
||||
}
|
||||
if (typeof schema !== 'object') return schema;
|
||||
if (typeof schema.type === 'string') {
|
||||
switch (schema.type) {
|
||||
case 'object':
|
||||
if (schema.attrs && typeof schema.attrs === 'object') {
|
||||
Object.keys(schema.attrs).forEach((k) => {
|
||||
schema.attrs[k] = resetSchemaValues(schema.attrs[k]);
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'array':
|
||||
if (Array.isArray(schema.attrs)) {
|
||||
schema.attrs = schema.attrs.map((item: any) => resetSchemaValues(item));
|
||||
}
|
||||
break;
|
||||
case 'string':
|
||||
case 'number':
|
||||
case 'boolean':
|
||||
case 'null':
|
||||
schema.value = null;
|
||||
break;
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
// 普通对象(如根 object 或未包装节点):递归子键
|
||||
Object.keys(schema).forEach((k) => {
|
||||
schema[k] = resetSchemaValues(schema[k]);
|
||||
});
|
||||
return schema;
|
||||
}
|
||||
|
||||
const confirmJsonEditor = () => {
|
||||
setMappingValue(editingMappingKey.value, { ...jsonEditorData.value });
|
||||
const cleaned = resetSchemaValues(JSON.parse(JSON.stringify(jsonEditorData.value)));
|
||||
setMappingValue(editingMappingKey.value, cleaned);
|
||||
jsonEditorVisible.value = false;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user