fix: improve JSON validation with unified validation function

- Extract common validateJsonConfig function for reuse
- Apply unified validation to both main config and common config snippets
- Add real-time JSON validation to JsonEditor component using CodeMirror linter
- Simplify error handling without over-engineering error position extraction
This commit is contained in:
Jason
2025-09-18 08:35:09 +08:00
parent 19dcc84c83
commit efff780eea
6 changed files with 195 additions and 59 deletions

View File

@@ -77,6 +77,22 @@ export interface UpdateCommonConfigResult {
error?: string;
}
// 验证JSON配置格式
export const validateJsonConfig = (value: string, fieldName: string = "配置"): string => {
if (!value.trim()) {
return "";
}
try {
const parsed = JSON.parse(value);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return `${fieldName}必须是 JSON 对象`;
}
return "";
} catch {
return `${fieldName}JSON格式错误请检查语法`;
}
};
// 将通用配置片段写入/移除 settingsConfig
export const updateCommonConfigSnippet = (
jsonString: string,
@@ -99,22 +115,16 @@ export const updateCommonConfigSnippet = (
};
}
let snippet: Record<string, any>;
try {
const parsed = JSON.parse(snippetString);
if (!isPlainObject(parsed)) {
return {
updatedConfig: JSON.stringify(config, null, 2),
error: "通用配置片段必须是 JSON 对象",
};
}
snippet = parsed;
} catch (err) {
// 使用统一的验证函数
const snippetError = validateJsonConfig(snippetString, "通用配置片段");
if (snippetError) {
return {
updatedConfig: JSON.stringify(config, null, 2),
error: "通用配置片段格式错误,需为合法 JSON",
error: snippetError,
};
}
const snippet = JSON.parse(snippetString) as Record<string, any>;
if (enabled) {
const merged = deepMerge(deepClone(config), snippet);