fix: improved JSON validation for comments and placeholders

This commit is contained in:
sokol
2026-02-20 16:55:34 +03:00
parent 9f51379df9
commit 28fc3f9e21

View File

@@ -40,14 +40,56 @@ export function ConfigTemplateEditor({ config, onSaved }: ConfigTemplateEditorPr
setJsonError(null);
return true;
}
// Strip comments (// single-line and /* */ multi-line) and placeholders
const sanitizedValue = value
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* */ comments
.replace(/\/\/.*$/gm, '') // Remove // comments
.replace(/@[^@]+@/g, '1'); // Replace placeholders
JSON.parse(sanitizedValue);
// Step 1: Remove /* */ multi-line comments (safe, can't appear in strings)
let sanitized = value.replace(/\/\*[\s\S]*?\*\//g, '');
// Step 2: Process line by line to handle // comments and placeholders
// Only remove // comments that are OUTSIDE of quoted strings
const lines = sanitized.split('\n');
const processedLines = lines.map(line => {
// Find // that's outside quotes
// Strategy: split by quotes, only process odd-indexed segments (outside quotes)
let result = '';
let inQuote = false;
let quoteChar = '';
for (let i = 0; i < line.length; i++) {
const char = line[i];
const prevChar = i > 0 ? line[i - 1] : '';
// Check for quote start/end (not escaped)
if ((char === '"' || char === "'") && prevChar !== '\\') {
if (!inQuote) {
inQuote = true;
quoteChar = char;
} else if (char === quoteChar) {
inQuote = false;
quoteChar = '';
}
result += char;
}
// Check for // comment start (only outside quotes)
else if (!inQuote && char === '/' && line[i + 1] === '/') {
// Rest of line is comment, stop processing
break;
}
else {
result += char;
}
}
return result;
});
sanitized = processedLines.join('\n');
// Step 3: Replace unquoted @placeholders@ with dummy values
// Placeholders in JSON values are typically quoted: "@param@"
// We replace all @param@ with 1 (works for both quoted and unquoted)
sanitized = sanitized.replace(/@[^@]+@/g, '1');
JSON.parse(sanitized);
setJsonError(null);
return true;
} catch (e) {