fix: JSON validation for unquoted @placeholders@

This commit is contained in:
sokol
2026-02-19 00:14:56 +03:00
parent 529b506612
commit 0c97c4421c
3 changed files with 163 additions and 8 deletions

View File

@@ -35,9 +35,10 @@ export function ConfigTemplate(props: ConfigTemplateProps) {
}
function handleSave() {
// Validate JSON before saving
// Validate JSON before saving (with placeholder support)
try {
JSON.parse(draftContent);
const sanitizedValue = draftContent.replace(/@[^@]+@/g, '1');
JSON.parse(sanitizedValue);
setJsonError(null);
props.onSaved(draftContent);
setMode('view');
@@ -52,12 +53,8 @@ export function ConfigTemplate(props: ConfigTemplateProps) {
try {
if (value.trim()) {
// Replace @placeholders@ with valid JSON values for validation
// Handle both quoted "@placeholder@" and unquoted @placeholder@
let sanitizedValue = value
// Replace quoted placeholders: "@host@" -> "__PLACEHOLDER__"
.replace(/"@[^"]+@"/g, '"__PLACEHOLDER__"')
// Replace unquoted placeholders between : and , or } : @port@ -> "__PLACEHOLDER__"
.replace(/:\s*@[^,\s}]+@/g, ': "__PLACEHOLDER__"');
// Strategy: Replace ALL @...@ patterns with "1" (valid for both string and numeric contexts)
const sanitizedValue = value.replace(/@[^@]+@/g, '1');
JSON.parse(sanitizedValue);
setJsonError(null);