fix: improved JSON validation for comments and placeholders
This commit is contained in:
@@ -41,13 +41,55 @@ export function ConfigTemplateEditor({ config, onSaved }: ConfigTemplateEditorPr
|
||||
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
|
||||
// Step 1: Remove /* */ multi-line comments (safe, can't appear in strings)
|
||||
let sanitized = value.replace(/\/\*[\s\S]*?\*\//g, '');
|
||||
|
||||
JSON.parse(sanitizedValue);
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user