fix: improved JSON validation for comments and placeholders
This commit is contained in:
@@ -40,14 +40,56 @@ export function ConfigTemplateEditor({ config, onSaved }: ConfigTemplateEditorPr
|
|||||||
setJsonError(null);
|
setJsonError(null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip comments (// single-line and /* */ multi-line) and placeholders
|
// Step 1: Remove /* */ multi-line comments (safe, can't appear in strings)
|
||||||
const sanitizedValue = value
|
let sanitized = value.replace(/\/\*[\s\S]*?\*\//g, '');
|
||||||
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* */ comments
|
|
||||||
.replace(/\/\/.*$/gm, '') // Remove // comments
|
// Step 2: Process line by line to handle // comments and placeholders
|
||||||
.replace(/@[^@]+@/g, '1'); // Replace placeholders
|
// Only remove // comments that are OUTSIDE of quoted strings
|
||||||
|
const lines = sanitized.split('\n');
|
||||||
JSON.parse(sanitizedValue);
|
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);
|
setJsonError(null);
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user